In C language if
char *ptr1 = "something";
is allowed, then why not
int *ptr2 = {1,2,3};
not allowed?
In C language if
char *ptr1 = "something";
is allowed, then why not
int *ptr2 = {1,2,3};
not allowed?
In the first case,
char *ptr1 = "something";
"something"
boils down to a pointer to the first element, of the string literal, yields a type char*
, so the type is compatible and assignment in the initialization is valid.
On the other hand,
int *ptr2 = {1,2,3};
{1,2,3}
is a brace enclosed initializer list, not an int *
, so it's invalid operation and not allowed.
What you can do, actually is to use a compound literal for the intialization, something like
int *ptr2 = (int []){1,2,3};
where ptr2
will point to the first element of the array composed of the values given in the brace enclosed initializer list.