2

Here is my sample code

#include<stdio.h>
void main()
{
 int arr[]={1,2,3,4,5,6};
 char *ptr,a;
 a='c';
 ptr=&a;
 int *ptr1,a1;
 a1=4;
 ptr1=&a1;
 printf("%d  %d   %d",sizeof(arr), sizeof(ptr1), sizeof(ptr));
}

Now, as far as I understand, size of will tell me the size required to store the variable, now the output for this one is

24 4 4

Why is the size of arr=24, after all it's just a pointer and it should be having size =4 ?

Thanks.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177

5 Answers5

5

"...after all it's just a pointer..."? No. Array is not a pointer. Array is an array object: a solid continuous block of memory that stores the array elements, no pointers of any kind involved. In your case array has 6 elements of size 4 each. That is why your sizeof evaluates to 24.

The common misconception about arrays being pointers has been debunked millions of times, but somehow it continues to pop up now and then. Read the FAQ, come back if you have any questions about it

http://c-faq.com/aryptr/index.html

P.S. As @Joachim Pileborg correctly noted in his answer, sizeof is not a function. It is an operator.


Another context in which arrays behave differently from pointers is the unary & operator (the "address of" operator). When unary & is applied to a pointer of type int * is produces a pointer of type int **. When unary & is applied to an array of type int [10] is produces a pointer of type int (*)[10]. These are two very different types.

int *p = 0;
int a[10] = { 0 };

int **p1 = &p;      /* OK */
int **p2 = &a;      /* ERROR */
int (*p3)[10] = &a; /* OK */

It is another popular source of questions (and errors): sometimes people expect & to produce a int ** pointer when applied to an int [10] array.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Well, I guess, it's just hard to keep up when sometimes it behaves like pointer and then other times it's just an array. :) –  Dec 02 '12 at 18:15
  • 1
    @Sahil: True. In C language arrays behave as arrays in three contexts only: unary `&` operator, `sizeof` operator and initialization of `char[]` array by a string literal. In all other contexts they get automatically *converted* to pointers. – AnT stands with Russia Dec 02 '12 at 18:17
  • When you say unary `&` operator, what exactly do you mean, can you give an example to demonstrate. Maybe add it to the question itself, before I accept the answer. –  Dec 02 '12 at 18:18
  • I have one more question; follow up i guess. Since `a` is of size 24; if I do a+1 and find value at that, why is it a[1] and not a[7](i.e. out of bound). Since a pointer jumps as much bytes as is the sizeof of the pointer.? –  Dec 02 '12 at 18:29
  • or maybe, this time it is just behaving like a pointer to integer. But had it been &a+1 that would jump the entire array.? Right? –  Dec 02 '12 at 18:32
  • 1
    @Sahil: Pointer jumps by as many bytes as the size of the *pointed type*. As I said above, array is converted to a pointer in all contexts, except the three I mentioned. `a + 1` is not one of those contexts, which means that your `a` gets converted to `int *` and you get `(int *) a + 1`. This, expectedly, jumps forward by size of `int`. But if you do `&a + 1`, you get an `int (*)[6]` pointer from `&a`. This time the pointed type is `int[6]`, i.e. it is a pointer *to the entire array*. This pointer will jump forward by the entire array size (24 bytes) every time you add 1 to it. – AnT stands with Russia Dec 02 '12 at 18:38
0

When you have an array, sizeof returns the size of the array (note that it's the size in bytes of the array, not the number of items in the array), otherwise it returns the size of the type or expression.

However, if you pass the array to a function, the array degrades to a pointer and the size information is lost.

Also, technically speaking sizeof is not a function, it's a special keyword in the language, and can be used without parentheses as well when used with expressions.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Arrays don't decay into pointers inside sizeof; sizeof(arr) returns the total size allocated for the array which, in this case, is 6 * sizeof(int).

Joshua Green
  • 1,517
  • 1
  • 10
  • 14
  • Is not it some sort of ambiguity? –  Dec 02 '12 at 18:12
  • It's not ambiguous, though it can be a bit confusing. However, it does allow certain useful idioms like `size_t numElements = sizeof(arr)/sizeof(arr[0]);` for computing the number of elements in the _array_ `arr`. This won't work if you've passed `arr` into a function in which it's "only" a pointer. – Joshua Green Dec 02 '12 at 18:52
  • Yeah, actually, that is the starting point of the conversation that lead to this question. Cheers. :) –  Dec 02 '12 at 18:58
0

From Wikipedia

When sizeof is applied to the name of an array, the result is the size in bytes of the whole array. (This is one of the few exceptions to the rule that the name of an array is converted to a pointer to the first element of the array.)

koopajah
  • 23,792
  • 9
  • 78
  • 104
0

the sizeof() operator does not give you the number of elements in an array, it gives you the number of bytes a thing occupies in memory. Hence:

#include <stdio.h>

int main()
{
  char s[] = { 1, 2, 3, 4, 5, 0 };
  int xs[] = { 1, 2, 3, 4, 5, 0 };

  printf( "sizeof( s ) = %d\n",  sizeof( s  ) );
  printf( "sizeof( xs ) = %d\n", sizeof( xs ) );

  return 0;
}

sizeof( s ) = 6
sizeof( xs ) = 24
Behnam Esmaili
  • 5,835
  • 6
  • 32
  • 63