-2

Here is the code:

#include <stdio.h> 

int main()   
{
   int arr[] = {10, 20, 30, 40, 50, 60};   
   int *ptr = arr; 
   printf("Size of arr[] %d\n", sizeof(arr)); 
   printf("Size of ptr %d", sizeof(ptr)); 
   return 0; 
}

What is the difference between the two printf statements?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

1 Answers1

1

Take a look at the wikipedia article on how sizeof works on arrays. Essentially, it is returning the bytes required to store the entire array. In this case you have 6 ints, so this turns into sizeof(int) * 6 = 4 * 6 = 24

However, the second sizeof is getting the size of an int pointer. Your 64-bit machine has 64/8 = 8 byte pointers. Note that while pointers and arrays are usually considered the "same" in C, this is one of the areas where the behavior differs.

Ammar Askar
  • 475
  • 3
  • 9
  • 1
    They differ in all areas. A pointer is not an array, and an array is not a pointer, but an array is converted to a pointer to the first element on access in all but 4 limited exceptions detailed in [C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3)](http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1p3) – David C. Rankin Sep 03 '19 at 06:03