Difference between character array and integer array

  • char *s = "Hello"

  • char s[6] = "Hello"

Any of the above syntaxes will work fine.

But what about the next one?

  1. int a[3] = {1,2,3}

    (this will work great)
  2. But why not this int *a = {1, 2, 3};

    ,?

An explanation along with a comparison between case [2] and [4] would be helpful.

+3


source to share


7 replies


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.

+5


source


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}

.

+5


source


  • char *s="Hello"

    Here s

    is a pointer to char

    that points to the base address of the string literal "Hello"

    .

  • char s[6]="Hello"

    herein s

    is an array of 6 char

    s, having H

    , e

    , l

    , l

    , o

    and \0

    as the value of the initiator.

  • int a[3]={1,2,3}

    here a

    is an array of int

    3 elements, initialized with values 1

    , 2

    and 3

    .

Note: all three of them are legal and valid.

  1. int *a={1,2,3}

    ; invalid.

    here a

    has a type int *

    , and the closed parenthesis list contains no value int *

    . Thus, this is undefined behavior and is not valid.

+3


source


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.

+3


source


int *a = {1, 2, 3};

      

is syntactically installed because it {1, 2, 3}

cannot be used to initialize a pointer.

However, a little modification can make it work:

int *a = (int []){1, 2, 3};

      

It is a C99 coherent literal.

+2


source


Initializing a character array with a string is a special case: it is char s[6] = "hello"

treated as if the code were written char s[6] = { 'h', 'e', 'l', 'l', 'o', '\0'};

. Initializing a character array with a string is common, so this idiom makes sense.

+1


source


  1. int a[3] = {1,2,3};

This is the standard initialization syntax for arrays.

  1. 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.

  1. int *a = {1, 2, 3};

This differs from (1) above in that {1, 2, 3}

it is not a valid C expression.

0


source







All Articles