• 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 ?
prasad2315Asked:
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.

evilrixSenior 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

Your issues matter to us.

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

Start your 7-day free trial
Infinity08Commented:
>> 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
evilrixSenior Software Engineer (Avast)Commented:
NB. The sizeof operator always returns the size of an object in bytes.

http://en.wikipedia.org/wiki/Sizeof
0
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

prasad2315Author Commented:
that means  in sizeof(a) a refers to whole array
am i correct
0
evilrixSenior 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
Infinity08Commented:
>>  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
Infinity08Commented:
>> 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
prasad2315Author Commented:
no particular reson ,but for the next time i will practice to sahre the pooints for identical answers
0
Infinity08Commented:
Just fyi :

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

The FAQ about "How do I close a question".
0
evilrixSenior 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
AnnieModCommented:
The points are shared, the grade is A so things look good

AnnieMod
Cleanup Admin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.