-1
Case I: 
   int a[12];
   printf("%d",sizeof(a));

Case II:
    int *a = (int *)malloc(12*sizeof(int));
    printf("%d",sizeof(a));

The above two code snippets return different outputs,

  • 48 in the first case
  • 4 in the second case

Why is it so?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
AAB
  • 1,594
  • 2
  • 25
  • 40
  • 1
    Does [Is an array name a pointer in C?](https://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c/1641963#1641963) make a suitable duplicate? A search on '[c] array pointer' yields 33,000 or so entries, which is a tad painful to search. – Jonathan Leffler Dec 21 '15 at 11:39

4 Answers4

10

Because arrays are not pointers and vice-versa. Period.

To elaborate, in case of int a[12];, a is an array, which has got it's own properties. It is of type array, so when you pass the array name to sizeof operator, it gives you the size of the array (in your case, 12 ints, multiplied by size of an int, 4, total 48).

OTOH, int *a = malloc(12*sizeof*a); (corrected format), a is a pointer, and using sizeof on this will give you only the size of the pointer itself, not the size of the memory allocated (pointed by) to the pointer.

To quote the C-faq, chapter 6,

Q: But I heard that char a[] was identical to char *a?

A: Not at all. (What you heard has to do with formal parameters to functions). Arrays are not pointers, though they are closely related.

The array declaration char a[6] requests that space for six characters be set aside, to be known by the name a. That is, there is a location named a at which six characters can sit. The pointer declaration char *p, on the other hand, requests a place which holds a pointer, to be known by the name p. This pointer can point almost anywhere: to any char, or to any contiguous array of chars, or nowhere.

Notes:

  1. Please see this discussion on why not to cast the return value of malloc() and family in C..
  2. To make your code robust and portable, prefer using the variable name itself instead of the hard-coded datatype in the allocation statement.
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 4
    That period felt so powerful :D Plus for short and concise answer, but i believe this question is probably a duplicate. – Transcendental Dec 21 '15 at 11:17
  • @Transcendental Thanks. Only wish that value of _period_ would have been understood by all. :) – Sourav Ghosh Dec 21 '15 at 11:18
  • @SouravGhosh a[i] is the same as *(a+i) rite? so then why are they different datatypes apologies but a link that explains arrays and pointers would be of good help – AAB Dec 21 '15 at 11:42
  • @AAB I think you're mixing up the point. Both the representations will give you the same result, but here we're talking about the type. Hope it's clear. – Sourav Ghosh Dec 21 '15 at 11:47
  • well I understand they are separate data types but when an operation is done internally a[i] gets converted to *(a+i) rite? so what actually happens here can you throw some light – AAB Dec 21 '15 at 11:52
0

Arrays and pointers are different datatypes in C. When array is used as an operand with sizeof operator , it gives the size of array data type. But, when a pointer is used with sizeof operator, size of pointer is returned.

Standard says (C11-6.5.3.4):

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand.

The type of operand a in first case is int [12] while the type of operand in second case is int *.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

You are getting different answers because in the two cases a is different.

int a[12]; — in this a is an array,

int *a = malloc(12*sizeof*a); — in this a is a pointer.


Pointers and arrays can be used interchangeably at most of the places, but that does not mean they are the same things. Both have different properties (as explained by Sourav).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Haris
  • 12,120
  • 6
  • 43
  • 70
0

sizeof operator deals with the size of type. It does not have to do anything with the contents held by the operands passed to it.

int a[12] as a type int [12] and therefore give size 48(4*12).

But int *a is an integer pointer and sizeof operator give size of the pointer (i.e 4).

Both in these cases int a[12] and int *a are not same. Note that sizeof is one of the few cases where an array does not decay to a pointer.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ameyCU
  • 16,489
  • 2
  • 26
  • 41