• C

# 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 ?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

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