0

Following program:

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

int main()
{
    int *array;
    array = malloc(sizeof(int)*100);

    for(int i=0; i<sizeof(array); i++) {
    printf("%d\n",i);
    }
    free(array);
}

It display 0-3. But I expected 0-399. I thought the size will be calculated through 4*100.

bando
  • 3
  • 2
  • What you have is a pointer, not an array, so `sizeof` will give you the size of a pointer which is 4 on your system. – Christian Gibbons Jun 02 '20 at 16:08
  • You already know the number of elements there can be in the array, because you allocated the memory for `100` elements in the previous line (not `400` its size). Bear in mind too that you are printing the useless values of *uninitialised data*, because `malloc` (unlike `calloc`) makes no promises about its content. – Weather Vane Jun 02 '20 at 16:24

0 Answers0