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. Total noob question

Total noob question

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
6 Posts 3 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.
  • G Offline
    G Offline
    georgiek50
    wrote on last edited by
    #1

    This is something I haven't mastered for some reason. I am trying to count the number of elements in a one-dimensional array to loop through it so I figured it would be something like sizeof(array) / sizeof(array[0]) Unfortunately this always returns 1. The array consists of 12 longs (but it is empty to begin with and during the calculation. Do different rules apply to empty arrays, and what is the workaround) Thanks.

    M A 2 Replies Last reply
    0
    • G georgiek50

      This is something I haven't mastered for some reason. I am trying to count the number of elements in a one-dimensional array to loop through it so I figured it would be something like sizeof(array) / sizeof(array[0]) Unfortunately this always returns 1. The array consists of 12 longs (but it is empty to begin with and during the calculation. Do different rules apply to empty arrays, and what is the workaround) Thanks.

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      The sizeof trick only works for staticly allocated arrays. It sounds like you're doing something like:

      long* ptr = new long[12];

      Since the type of ptr is long*, sizeof(ptr) is 4. ptr[0] is a long, and sizeof(long) is also 4. To do what you want, you would have to write:

      long arr[12];

      Now sizeof(arr) == 12*sizeof(long) == 48. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0! | RightClick-Encrypt "Linux is good. It can do no wrong. It is open source so must be right. It has penguins. I want to eat your brain."   -- Paul Watson, Linux Zombie

      G 1 Reply Last reply
      0
      • G georgiek50

        This is something I haven't mastered for some reason. I am trying to count the number of elements in a one-dimensional array to loop through it so I figured it would be something like sizeof(array) / sizeof(array[0]) Unfortunately this always returns 1. The array consists of 12 longs (but it is empty to begin with and during the calculation. Do different rules apply to empty arrays, and what is the workaround) Thanks.

        A Offline
        A Offline
        Andrew Walker
        wrote on last edited by
        #3

        Alternatively you could use the Standard Template Library Vector. Which means you don't have to worry about memory allocations or keeping track of sizes of arrays. // Declare a vector std::vector data(12); // Access Elements data[0] = ... data.at(0) = ... // Add element data.push_back(...); // Retrieve size of vector data.size()


        If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling

        1 Reply Last reply
        0
        • M Michael Dunn

          The sizeof trick only works for staticly allocated arrays. It sounds like you're doing something like:

          long* ptr = new long[12];

          Since the type of ptr is long*, sizeof(ptr) is 4. ptr[0] is a long, and sizeof(long) is also 4. To do what you want, you would have to write:

          long arr[12];

          Now sizeof(arr) == 12*sizeof(long) == 48. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0! | RightClick-Encrypt "Linux is good. It can do no wrong. It is open source so must be right. It has penguins. I want to eat your brain."   -- Paul Watson, Linux Zombie

          G Offline
          G Offline
          georgiek50
          wrote on last edited by
          #4

          I have declared the array staticaly in one function and then passed this empty array in another function (to fill it up) and it is in the second function that sizeof(arr) == 1...I don't understand it! The function parameter list (2nd one goes something like this) void(..., ..., long arr[]) { ... }

          M 1 Reply Last reply
          0
          • G georgiek50

            I have declared the array staticaly in one function and then passed this empty array in another function (to fill it up) and it is in the second function that sizeof(arr) == 1...I don't understand it! The function parameter list (2nd one goes something like this) void(..., ..., long arr[]) { ... }

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            That's because when you pass an array as function parameter, the function always receives a pointer just as if it were declared long* arr. You can't pass an entire array in C. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0! | RightClick-Encrypt There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true.

            G 1 Reply Last reply
            0
            • M Michael Dunn

              That's because when you pass an array as function parameter, the function always receives a pointer just as if it were declared long* arr. You can't pass an entire array in C. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0! | RightClick-Encrypt There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true.

              G Offline
              G Offline
              georgiek50
              wrote on last edited by
              #6

              Right!!!! I forgot about the "call by" rules!!! Thanks.

              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