[Webinar] Streamline your web hosting managementRegister Today

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 212

arrays

i want to number of elements in an array int this way
a[]={1,2,3};
int l;
l = sizeof(a) /sizeof(a[0]);
assume int size is 4 bytes
then sizeof(a) in the above expression gives 12 why ?
0
• 4
• 4
• 2
• +1
2 Solutions

Senior Software Engineer (Avast)Commented:
3 items in the array of type int, each of which is 4 bytes == 12 bytes in total.
0

Commented:
>> then sizeof(a) in the above expression gives 12 why ?

Because its size is 12 bytes. It contains 3 elements, each of which is 4 bytes. 3 * 4 = 12
0

Senior Software Engineer (Avast)Commented:
NB. The sizeof operator always returns the size of an object in bytes.

http://en.wikipedia.org/wiki/Sizeof
0

Author Commented:
that means  in sizeof(a) a refers to whole array
am i correct
0

Senior Software Engineer (Avast)Commented:
Yes.

BTW: Is there any reason you preferred I8's answer to mine? Ostensibly they are the same, but mine was (to be fair) first.
0

Commented:
>>  that means  in sizeof(a) a refers to whole array
>> am i correct

Yes. However, this will only work for arrays. Not for pointers like this for example :

int *array = (int*) calloc(3, sizeof(int));

sizeof(array) will be 4 (the size of the pointer), and not 12.
0

Commented:
>> Ostensibly they are the same, but mine was (to be fair) first.

In cases like this (two identical answers at the same time), it's suggested to split the points among the participants. Keep that in mind, prasad2315 : more than one reply can answer a question. And in that case, it's good to split the points among those replies.
0

Author Commented:
no particular reson ,but for the next time i will practice to sahre the pooints for identical answers
0

Commented:
Just fyi :

http://www.experts-exchange.com/help.jsp#hi331

The FAQ about "How do I close a question".
0

Senior Software Engineer (Avast)Commented:
>> no particular reson ,but for the next time i will practice to sahre the pooints for identical answers
Please choose your answers with care. All the experts here are volenteers and give up their time freely to help others. In return we earn points, which give us small benefits (such as free premier membership). All we ask in return is that when you aware points you do so with consideration for the answers you've been given and how helpful they were to you.

Thanks.
0

Commented:
The points are shared, the grade is A so things look good

AnnieMod