ravipandhi
ravipandhi

Reputation: 49

difference between character array and integer array

  1. char *s = "Hello"
  2. char s[6] = "Hello"

Any one of the above syntax would work fine.

But what about following?

  1. int a[3] = {1,2,3} (this would work excellent)
  2. But why not this, int *a = {1, 2, 3};?

Explanation along with the comparison between case [2] and [4] will be helpful.

Upvotes: 3

Views: 3725

Answers (7)

too honest for this site
too honest for this site

Reputation: 12263

Reason why that will not work is that the data type of the initialier is not defined. For the string literal, this is given implicitly by the syntax. But { 1,2,3} could be an array, a struct, or many other variants.

You have to specify the type:

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

This uses a compound literal (C99).

Note this not only works for initialization, but also in normal code.

Upvotes: 5

newacct
newacct

Reputation: 122439

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

This is the normal initialization syntax for arrays.

  1. char s[6] = "Hello";

This is a special case initialization syntax only for character arrays, where you can write a string literal on the right side and it will expand out to the normal initialization syntax as above, i.e. char s[6] = {'H','e','l','l','o','\0'};

  1. char *s = "Hello";

This is the normal initialization syntax for a scalar variable, initialized to an expression on the right side. Here, "Hello" is a valid C expression.

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

This is different from (1) above in that {1, 2, 3} is not a valid C expression.

Upvotes: 0

Eregrith
Eregrith

Reputation: 4366

It's because "Hello" is replaced with "The address of the string literal Hello". So char *s = "Hello" means "Assign to the pointer s the address of the string literal Hello".

Meanwhile {1, 2, 3} does not constitute an address and is not replaced. You can't assign anything else but an address to a pointer, so you can't write int *a = {1, 2, 3}.

Upvotes: 5

Peter
Peter

Reputation: 36597

The reason for case 4 not being supported but the other cases being supported are historical. The factors are lobbying and political interplay between some early influential programmers and compiler vendors, rather than a deliberate reasoned technical decision.

So, if you're looking for a robust technical justification, you won't find one.

Historically, cases 2 and 3 have been supported since quite early in the evolution of C. Your case 2 achieves the same effect as

  char s[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

There is no counterpart of string literals to initialise arrays of anything other than char types.

Historically, case 1 is an anomaly introduced by programmers who wanted to achieve the effect of

  char s_temp[] = "Hello";
  char *s = temp_s;

with less typing (i.e. as a single statement). The lobbying for support of case 1 eventually won out (it was introduced into mainstream compilers, and later into the standard). Case 1 is the only case in the standard where a pointer can be initialised directly using an array initialiser without any need for a type conversion.

Historically, there has never been demand or lobbying from programmers for case 4.

Which, like it or not, is the reason why case 1,2,3 are supported but case 4 is not.

There is no real comparison between cases 2 and 4, because they (seek to) achieve different things. A string literal, as in case 2, is an array initialiser that only works for arrays of char - there is no counterpart for non-char types. Case 4 is trying to initialise a pointer using an array initialiser.

Upvotes: 3

Sourav Ghosh
Sourav Ghosh

Reputation: 134326

  1. char *s="Hello"

    Here, s is a pointer to char, which points to the base address of the string literal "Hello".

  2. char s[6]="Hello"

    here s is an array of 6 chars, having H, e, l, l, o and \0 as initilizer value.

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

    here, a is an int array of 3 elements, initialized with the values 1, 2 and 3.

Note: all the above three are legal and valid.

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

    here, a is of type int *, and the brace enclosed list does not supply a value of int *. So, this is not a defined behaviour and invalid.

Upvotes: 3

Yu Hao
Yu Hao

Reputation: 122383

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

is syntactically incorect because {1, 2, 3} cannot be used to initialize a pointer.

However, a little modification could make it work:

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

It's a C99 compound literal.

Upvotes: 2

William Pursell
William Pursell

Reputation: 212248

Initializing a character array with a string is a special case: char s[6] = "hello" is treated as if the code were written char s[6] = { 'h', 'e', 'l', 'l', 'o', '\0'};. Initializing a character array with a string is a common occurrence, so this idiom makes sense.

Upvotes: 1

Related Questions