Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Pointer to a vector

Pointer to a vector

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdata-structuresquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Oliver
    wrote on last edited by
    #1

    Hello, I previously wrote the contents of an array of floats to a file, in binary, using the following; OutputFile.write((char*)Z, sizeof(float) * n); //write n elements of array Z Instead of an array, if Z is defined as a Vector of floats, is there an equivalent of the above. Can I obtain a pointer to the start of the vector? Thanks, John

    J J 2 Replies Last reply
    0
    • J John Oliver

      Hello, I previously wrote the contents of an array of floats to a file, in binary, using the following; OutputFile.write((char*)Z, sizeof(float) * n); //write n elements of array Z Instead of an array, if Z is defined as a Vector of floats, is there an equivalent of the above. Can I obtain a pointer to the start of the vector? Thanks, John

      J Offline
      J Offline
      John Oliver
      wrote on last edited by
      #2

      Think I've got the answer - dereference the vector... (char*) &Z

      R 1 Reply Last reply
      0
      • J John Oliver

        Hello, I previously wrote the contents of an array of floats to a file, in binary, using the following; OutputFile.write((char*)Z, sizeof(float) * n); //write n elements of array Z Instead of an array, if Z is defined as a Vector of floats, is there an equivalent of the above. Can I obtain a pointer to the start of the vector? Thanks, John

        J Offline
        J Offline
        Joaquin M Lopez Munoz
        wrote on last edited by
        #3

        If v is the name of the vector, then &v[0] points to the underlying buffer. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        G 1 Reply Last reply
        0
        • J John Oliver

          Think I've got the answer - dereference the vector... (char*) &Z

          R Offline
          R Offline
          Rama Krishna Vavilala
          wrote on last edited by
          #4

          John Oliver wrote: Think I've got the answer - dereference the vector... (char*) &Z That will be wrong. You have to dereference the first element like &Z[0]. ..this is a VB Programmers' world and we all are just visitors - Someone in a MSJ article

          1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            If v is the name of the vector, then &v[0] points to the underlying buffer. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            G Offline
            G Offline
            gazihan
            wrote on last edited by
            #5

            what's the difference between a vector and a one-dimensional array in C++? as far as i know, nothing. so, this is an equivalence class: &V[0] &(*(V+0)) &(*(V)) V therefore, you can use V if you want.

            C 1 Reply Last reply
            0
            • G gazihan

              what's the difference between a vector and a one-dimensional array in C++? as far as i know, nothing. so, this is an equivalence class: &V[0] &(*(V+0)) &(*(V)) V therefore, you can use V if you want.

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              The difference is that an array is a wrapper around the data, therefore, the correct answer remains &v[0];. A vector of int's is a block of ints, the first one being at &v[0];. v has nothing to do with int's or memory, it is an instance of the vector class. In fact, if you want a pointer to an element, you must dereference an iterator, then ask for the address, exactly as you have put it: &(*(V)). VC6 wrongly will accept the iterator as a pointer, VC7 corrects this. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups