Total noob question
-
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.
-
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.
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
islong*
,sizeof(ptr)
is 4.ptr[0]
is along
, andsizeof(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 -
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.
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
-
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
islong*
,sizeof(ptr)
is 4.ptr[0]
is along
, andsizeof(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 ZombieI 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[]) { ... }
-
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[]) { ... }
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. -
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.Right!!!! I forgot about the "call by" rules!!! Thanks.