As Grzegorz Szpetkowski's answer says, that syntax:
int *varname = {0};
is valid. It just doesn't do what I suspect you think it should do. It's equivalent to
int *varname = 0;
which is equivalent to
int *varname = NULL
(assuming NULL
is visible).
If my guess about what you're trying to do is wrong, the rest of this answer doesn't apply.
Based on the comments, it looks like that's not what the OP was trying to do. Not sure whether to delete this answer or not; it could be a good answer to some other question.
You can initialize a char*
pointer to point to a string literal:
char *cptr = "hello";
The string literal "hello"
implicitly creates an anonymous array object with static storage duration; the initialization causes cptr
to point to that array's initial element.
Prior to C99, there was no equivalent syntax for defining a non-character pointer and simultaneously creating something for it to point to.
C99 added compound literals. For example, this:
(int){42}
creates an int
object with the value 42
. Unlike a literal 42
, this actually creates an object, not just a value -- which means it has an address. So this:
int *iptr = &((int){42});
creates an anonymous int
object with an initial value of 42
, and initializes iptr
to point to it. (If your compiler supports compound literals.)
Compound literals are usually used for array and structure types, but they're also valid for scalar types.
One thing to watch out for: the array created by a string literal always has static storage duration, meaning it exists during the entire execution of the program. The storage duration of the anonymous object created by a compound literal depends on where it appears. If it's inside a function, the object has automatic storage duration, which means it ceases to exist as soon as execution leaves the nearest enclosing block.
So given:
char *cptr = "hello";
you can safely return the value of cptr
from a function, and it will continue to be valid. But given:
int *iptr = &((int){42});
returning the value of iptr
from a function would be dangerous, since the object it points to will cease to exist before the caller gets the pointer value.
A simpler way to do this kind of thing is to define the object yourself:
int obj = 42;
int *iptr = &obj;
You can define obj
as static
if necessary.