1
#include<stdio.h>
#include<stdlib.h>

int main (int argc, char *argv[]) {

    int* arr1 = (int*)malloc(sizeof(int)*4);
    int arr2[4];


    printf("%d \n", sizeof(arr1));
    printf("%d \n", sizeof(arr2));

    free(arr1);

    return 0;
}

Output

8
16

Why?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

2 Answers2

3

Arrays are not pointers.

In your code, arr1 is a pointer, arr2 is an array.

Type of arr1 is int *, whereas, arr2 is of type int [4]. So sizeof produces different results. Your code is equivalent to

sizeof (int *);
sizeof (int [4]);

That said, sizeof yields the result of type size_t, so you should be using %zu to print the result.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

arr1 is a pointer when you call sizeof(arr1) this will you size of pointer. but arr2 represent block of memory for 4 integers.

Abhishek
  • 379
  • 1
  • 8