Reputation: 103
#include <stdio.h>
int main() {
int x[] = {22, 8, 87, 76, 45, 43, 34, 13, 51, 15};
int count = 0;
while (x[count] != '\0') {
count++;
}
printf("%d", count);
return 0;
}
I cannot figure out why this program gives me output 12 whereas it is supposed to be 10. Where is the mistake? Is there any other way to know the number of elements?Because I have to use it further to calculate the median of the given data.
Please anyone help me clear this doubt. Thanks in advance.
Upvotes: 0
Views: 4962
Reputation: 29955
An array of int
is not necessarily zero terminated. Array of char
sometimes is. But in any case, if you have the array itself and not just a pointer to the array, here's how you get the size of the array:
size_t const count = sizeof(x) / sizeof(*x);
Note that this will not work if you have passed the array as an argument to a function, because then the size information is lost in the function.
Upvotes: 1
Reputation: 385506
A NUL is only added when initializing from a string literal.
char a1[] = "abc"; // 4 elements: 0x61, 0x62, 0x63, 0x00.
char a2[] = { 'a', 'b', 'c' }; // 3 elements: 0x61, 0x62, 0x63.
char a3[] = { 97, 98, 99 }; // 3 elements: 0x61, 0x62, 0x63.
Since your array doesn't contain '\0'
(0
), you read past its end until you happen to hit a 0
. This is undefined behaviour.
You could add a zero:
#include <stdio.h>
int main(void) {
int x[] = { 22, 8, 87, 76, 45, 43, 34, 13, 51, 15, 0 };
size_t count = 0;
while (x[count]) {
count++;
}
printf("%zu\n", count);
}
That said, using a sentinel value here looks odd. One normally uses the size of the array.
#include <stdio.h>
int main(void) {
int x[] = { 22, 8, 87, 76, 45, 43, 34, 13, 51, 15 };
size_t count = sizeof(x) / sizeof(x[0]);
printf("%zu\n", count);
}
Upvotes: 5
Reputation: 310910
As you correctly pointed out yourself the array has 10 elements
int x[] = {22, 8, 87, 76, 45, 43, 34, 13, 51, 15};
and among the elements there is no sentinel value that is equal to '\0'
.`.
So the loop invokes undefined behavior due to accessing the memory outside the array.
You could append to the array an element with the value equal to 0 like
int x[] = {22, 8, 87, 76, 45, 43, 34, 13, 51, 15, 0 };
In this case the loop
while (x[count] != '\0') {
count++;
}
will count all elements except the last one. So you will need to increase the variable count
after the loop that the last element also would be count.
Or you could rewrite the loop like
while ( x[count++] );
To get the number of elements in an array you can obtain its size and then divide it by the size of its element. For example
size_t count = sizeof( x ) / sizeof( x[0] );
or
size_t count = sizeof( x ) / sizeof( *x );
or even like
size_t count = sizeof( x ) / sizeof( x[1000] );
because the operator sizeof
does not evaluate the used expressions.
Upvotes: 0
Reputation: 691
This condition x[count]!='\0'
is not correct. It's only correct when you have to deal with strings, in that case '\0'
stands for end of the string, otherwise, when you deal with an array, you have to carry on with you a variable to get the length of your array. You can get this size with sizeof(count)/sizeof(count[0]);
In fact x[count]
can point any point of memmory, x
being the origin, and count
the offset, this is why you can access count[11]
for exemple.
Upvotes: 0