0

In C, as far as I can tell, there are two ways to create an array:

int arr[3] = {1,2,3} and int *arr = malloc(3*sizeof(int))

What's the difference? I understand that when you call malloc(), it makes space in memory for the array, but when you don't use malloc() does it go into memory automatically or is it pushed onto the stack?

I'm pretty new to C, so I might be interpreting this wrong.

Edit: Syntax mistakes

Joe Cutler
  • 43
  • 1
  • 6
  • http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html – Vinicius Kamakura Jan 30 '15 at 15:38
  • Apart from missing closing paranthese, the latter construct is invalid in C as `malloc(3*sizeof(int))` is not valid array initializer. I guess you meant `int *arr = malloc(3*sizeof(int))` or even better `int *arr = malloc(3*sizeof*arr)`. – Grzegorz Szpetkowski Jan 30 '15 at 15:46

0 Answers0