Difference between character array and integer array
The reason why this won't work is because the initialization datatype is undefined. For a string literal, this is implicitly determined by the syntax. But it { 1,2,3}
can be an array, a structure, or many other options.
You must specify the type:
int *ia = (int []){1,2,3};
A compound literal (C99) is used here.
Note that this not only works for initialization but also in normal code.
source to share
This is because it "Hello"
is replaced by "Address of the string literal Hello". So it char *s = "Hello"
means "Assign the s
address of the string literal Hello" to the pointer .
Meanwhile, {1, 2, 3}
it is not an address and is not replaced. You cannot assign anything other than an address to a pointer, so you cannot write int *a = {1, 2, 3}
.
source to share
-
char *s="Hello"
Here
s
is a pointer tochar
that points to the base address of the string literal"Hello"
. -
char s[6]="Hello"
herein
s
is an array of 6char
s, havingH
,e
,l
,l
,o
and\0
as the value of the initiator. -
int a[3]={1,2,3}
here
a
is an array ofint
3 elements, initialized with values1
,2
and3
.
Note: all three of them are legal and valid.
-
int *a={1,2,3}
; invalid.here
a
has a typeint *
, and the closed parenthesis list contains no valueint *
. Thus, this is undefined behavior and is not valid.
source to share
The reason for case 4 is not supported, but other supported cases are historical. The factors are lobbying and political interaction between some early influential programmers and compiler vendors, not a deliberate sound technical decision.
So, if you're looking for a solid technical justification, you won't find it.
Historically, cases 2 and 3 have been supported from the very beginning of C. Your case 2 achieves the same effect as
char s[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
There are no analogs of string literals for initializing arrays other than char
.
Historically, case 1 is an anomaly introduced by programmers who wanted to achieve the effect
char s_temp[] = "Hello";
char *s = temp_s;
with a smaller character set (i.e. as one operator). Lobbying for support for case 1 ultimately won out (it was introduced into major compilers and then standard). Case 1 is the only case in the standard where a pointer can be initialized directly using an array initializer without the need for type conversion.
Historically, there has never been demand or lobbying from programmers for case 4.
Which, like this or not, is the reason that case 1,2,3 is supported, but case 4 is not.
There is no real comparison between cases 2 and 4 because they (tend to) achieve different goals. The string literal, as in case 2, is an array initializer that only works for arrays char
- char
there are no counterparts for types . Case 4 tries to initialize a pointer using an array initializer.
source to share
int a[3] = {1,2,3};
This is the standard initialization syntax for arrays.
char s[6] = "Hello";
This is a special case syntax for character arrays only, where you can write a string literal on the right side and it will expand to the normal initialization syntax as above, i.e. char s[6] = {'H','e','l','l','o','\0'};
char *s = "Hello";
This is the standard initialization syntax for a scalar variable, initialized with the expression on the right. Here "Hello"
is a valid C expression.
int *a = {1, 2, 3};
This differs from (1) above in that {1, 2, 3}
it is not a valid C expression.
source to share