To determine the size of your array in bytes, you can use the sizeof
operator:
int a[17];
size_t n = sizeof(a);
On my computer, ints are 4 bytes long, so n is 68.
To determine the number of elements in the array, we can divide
the total size of the array by the size of the array element.
You could do this with the type, like this:
int a[17];
size_t n = sizeof(a) / sizeof(int);
and get the proper answer (68 / 4 = 17), but if the type of
a changed you would have a nasty bug if you forgot to change
the sizeof(int) as well.
So the preferred divisor is sizeof(a[0]), the size of the
zeroeth element of the array.
int a[17];
size_t n = sizeof(a) / sizeof(a[0]);
Another advantage is that you can now easily parameterize
the array name in a macro and get:
#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
int a[17];
size_t n = NELEMS(a);
@Skizz: I am pretty sure I am right, although the best "source" I can give you at the moment is Wikipedia, from the article on sizeof:
Wikipedia is wrong, Skizz is right. sizeof(char) is 1, by definition.
I mean, just read the Wikipedia entry really closely to see that it's wrong. "multiples of char". sizeof(char) can never be anything other than "1". If it were, say, 2, it would mean that sizeof(char) was twice the size of char!
"you've introduced a subtle way of shooting yourself in the foot"
C 'native' arrays do not store their size. It is therefore recommended to save the length of the array in a separate variable/const, and pass it whenever you pass the array, that is:
#define MY_ARRAY_LENGTH 15
int myArray[MY_ARRAY_LENGTH];
You SHOULD always avoid native arrays (unless you can't, in which case, mind your foot). If you are writing C++, use the STL's 'vector' container. "Compared to arrays, they provide almost the same performance", and they are far more useful!
// vector is a template, the <int> means it is a vector of ints
vector<int> numbers;
// push_back() puts a new value at the end (or back) of the vector
for (int i = 0; i < 10; i++)
numbers.push_back(i);
// Determine the size of the array
cout << numbers.size();
The sizeof way is the right way iff you are dealing with arrays not received as parameters. An array sent as a parameter to a function is treated as a pointer, so sizeof will return the pointer's size, instead of the array's.
Thus, inside functions this method does not work. Instead, always pass an additional parameter size_t size indicating the number of elements in the array.
The best way is you save this information, for example, in a structure:
Implement all necessary functions such as create, destroy, check equality, and everything else you need. It is easier to pass as a parameter.
Executive summary:
To determine the size of your array in bytes, you can use the
sizeof
operator:On my computer, ints are 4 bytes long, so n is 68.
To determine the number of elements in the array, we can divide the total size of the array by the size of the array element. You could do this with the type, like this:
and get the proper answer (68 / 4 = 17), but if the type of
a
changed you would have a nasty bug if you forgot to change thesizeof(int)
as well.So the preferred divisor is
sizeof(a[0])
, the size of the zeroeth element of the array.Another advantage is that you can now easily parameterize the array name in a macro and get:
Wikipedia is wrong, Skizz is right. sizeof(char) is 1, by definition.
I mean, just read the Wikipedia entry really closely to see that it's wrong. "multiples of char".
sizeof(char)
can never be anything other than "1". If it were, say, 2, it would mean thatsizeof(char)
was twice the size of char!"you've introduced a subtle way of shooting yourself in the foot"
C 'native' arrays do not store their size. It is therefore recommended to save the length of the array in a separate variable/const, and pass it whenever you pass the array, that is:
You SHOULD always avoid native arrays (unless you can't, in which case, mind your foot). If you are writing C++, use the STL's 'vector' container. "Compared to arrays, they provide almost the same performance", and they are far more useful!
See: http://www.cplusplus.com/reference/stl/vector/
The
sizeof
way is the right way iff you are dealing with arrays not received as parameters. An array sent as a parameter to a function is treated as a pointer, sosizeof
will return the pointer's size, instead of the array's.Thus, inside functions this method does not work. Instead, always pass an additional parameter
size_t size
indicating the number of elements in the array.Test:
Output (in a 64-bit Linux OS):
Output (in a 32-bit windows OS):