hi
i just needed to know about the sizeof...?what does it do?and what is it for? i have this question that i hope some1 can explain to me about its output?the output is coming out to be: 10 2 2 ,i
wasn't being able to understand it...how did it come?
Code:
```main()
{
int arr[]={12,13,14,15,16};
printf("\n%d %d %d",sizeof(arr),sizeof(*arr),sizeof(arr[0]));
}```

2. Sizeof is an operator that returns the size of the arguement in bytes.

Your results of 10, 2, and 2 are so because 10 is 5 the 2 bytes an int holds on your system (16 bit, though 32 bit is more common these days) The 5 being because your array has 5 members. The second is a pointer, which is also the size of an int (16 bit) and the last is the size of a single integer element in the array, which as I said is 2 bytes.

3. does that mean that sizeof(arr[0]) and sizeof(*arr) are the same thing?
i thought that *arr means that it would indicate the value at adress or the value at base address which is 12...and now it becomes sizeof(12) which is and integer so it means it is of 2 bytes....is this the right way to think...am i thinking correctly here?

4. Everything in C is an object. sizeof() is a operator that returns the size of that object in bytes that is the amount of memory that object takes.
sizeof(arr) is size of whole of the array. You have 5 integers in that array each take four bytes. So it will be 20.

sizeof(*arr) and sizeof(arr[0]) have the same meaning in C: The first element of the array. So size of will return the size of first element of the array that here is an integer that takes four bytes.
One use of sizeof() is finding the number of elements of an array, by deviding the size of it to the size of one element of it. In your code:
Code:
` int numberOfElements = sizeof(arr) / sizeof(arr[0]); /* 20/4=5 */`
It is good to take a look at this if you know some little about pointers:
http://www.cplusplus.com/doc/general/siavoshkc1.html

5. Though, the reason you're getting results of 10, 2, 2 rather than the 20, 4, 4 that siavoshkc is saying is because, as I said, you're on a 16 bit platform, not 32.

6. i believe that int takes 2 bytes and not 4 bytes as u said.....so sizeof(arr) should not be 20 but should be 10 as there r 5 elements and each integer takes 2 bytes of memory in c.i hope i am right here?

7. The amount of memory each type takes, is very dependent on platform. In mine it takes 4 bytes. This is exactly why we should use sizeof() instead of guessing the size. It keeps code portable.

8. Originally Posted by galmca
each integer takes 2 bytes of memory in c.i hope i am right here?
No, as I said, an integer takes 2 bytes of memory on a 16 bit system. As does a pointer. On a 32 bit system it takes 4 bytes. You under stand the correlation between 16 bits and 2 bytes as well as 32 bits and 4 bytes, correct?

9. yeah i get it now..thanks alot for ur help!

10. Originally Posted by SlyMaelstrom
No, as I said, an integer takes 2 bytes of memory on a 16 bit system. As does a pointer.
Or not. Things such as this existed on "a 16-bit system".
sizeof(int) = 2
sizeof(void*) = 4
ILP32 can teach incorrect assumptions.