Reputation: 3303
According to the C standard, this is considered a definition
int x;
Because it declares x and allocates storage. But is this also a definition?
int *x;
That is, does declaring a pointer variable or array variable allocate storage? I'm guessing no because we have to use malloc.
Upvotes: 2
Views: 103
Reputation: 370455
int* x;
is a definition. It allocates storage to store the pointer. It does not allocate any storage for an integer - that's why you need malloc
if you don't want to point to an existing variable (or other memory location).
A pure declaration would be something like extern int x;
or extern int* x;
, which would then have a corresponding definition in a different compilation unit.
Upvotes: 6
Reputation: 5369
int *x;
allocates storage. Variable x
is allocated storage equal to sizeof(int *)
. It contains a garbage value if it's at block scope or a null pointer value if it's at file scope.
Upvotes: 4