[Webinar] Streamline your web hosting managementRegister Today

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

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
prasad2315
Asked:
prasad2315
  • 4
  • 4
  • 2
  • +1
2 Solutions
 
evilrixSenior Software Engineer (Avast)Commented:
3 items in the array of type int, each of which is 4 bytes == 12 bytes in total.
0
 
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
Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

 
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

Featured Post

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now