CRag
CRag

Reputation: 47

How to find the number of elements in an array?

int a[10];
for(int i=0;i<5;i++)
{
   a[i]=i;
}
int len=sizeof(a)/sizeof(int);
print("%d",len);

This above code prints 10, but the actual number of elements present is 5. Help needed.

This wont be a problem if its a char array, but here integer array is causing pain.

Upvotes: 1

Views: 242

Answers (4)

Sivaram Rasathurai
Sivaram Rasathurai

Reputation: 6403

int a[10];
for(int i=0;i<5;i++)
{
   a[i]=i;
}
int len=sizeof(a)/sizeof(int);
print("%d",len);

When you code one line you need to understand why did you do like that Here small Explanation about each line.

int a[10];

This is an array declaration with the size of 10 and all elements are integer. This line will allocate 10 blocks of int size memory for your array with some random values(garbage values). For example its look like below.

|20|30|34|34|223|23|234|89|87|76|

for(int i=0; i<5; i++){
 a[i] =i;
}

This is a for loop which is run 5 times from i =0 to i =4 where ith element of an array will be replaced by i value.

When i = 0 array first element will be replaced by 0 with this line

a[i] =i;

a[0] =0 when i = 0

if a[0] is replaced by i then the array looks like below.

|0|30|34|34|223|23|234|89|87|76|

it continues until i <5 for each iteration, array will modify like below.

|0|1|34|34|223|23|234|89|87|76|

|0|1|2|34|223|23|234|89|87|76|

|0|1|2|3|223|23|234|89|87|76|

|0|1|2|3|4|23|234|89|87|76|

Clearly You can see that array size is didn't change. It remains 10

Upvotes: 0

"This above code prints 10, but the actual number of elements present is 5."

No. -> int a[10]; -> a is an array of 10 elements of type int.

The fact that you initialize only the first 5 elements in the for loop does not effect the size of the array a.

"This wont be a problem if its a char array, but here integer array is causing pain."

That can't be true if your tested code isn't significant different. Perhaps by using strlen instead of sizeof(a) / sizeof(char).

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

This above code prints 10, but the actual number of elements present is 5

No, the actual number of element is 10. You did not populate a value to have it stored, that's a different this, but that does not change the array dimension.

If you need to keep track of the used/ operated elements, you need to do that yourself by maintaining a counter somewhere.

Alternatively, if you can ensure that

  • The array size is always the max required length + 1
  • There's a certain value that will never appear in the list of valid values

You can use a sentinel value, (analogous to the null-terminator in char array), and using that you can find the number of used elements in the array.

Upvotes: 8

Dominique
Dominique

Reputation: 17565

You have declared the array as:

int a[10];

So, the number of elements, as reserved in memory, equals 10 (whether you fill them in or not).

If you are interested in keeping the number of elements in an array, I would advise you to fill it in with a number you won't use anyway (mostly people choose -1 for that purpose). Later, you fill in the array as you like.
Like that, when you want to know the number of elements in your array, you just run through your array for the first -1 (in case there is any), whose index will provide you the length of the array.

Upvotes: 2

Related Questions