0

This is a C question:

I dont understand why this code works:

char *c[] = {"hello","world"};

But this doesnt:

int *v[] = {{1,2},{3,4}};

For me they are the same thing (array of pointers initialized with their respective type) but clearly they are not. What is exaclty the difference then? Thanx.

Edit: If the person who downvoted my post could say WHY this is a bad question... that would be great.

2
  • If this is C/C++ there isn't really a difference. I wouldn't use them interchangeably, but data wise these are the same. Commented Jan 29, 2015 at 18:34
  • But hte compiler shows me warnings with the int one, and I cant have access to a single element. v[0]{0] for example, halts the program and does not show anything. Commented Jan 29, 2015 at 18:44

3 Answers 3

6

Assuming you are talking about C, the differences are:

  • "hello" defines an array of characters
  • arrays can decay to pointers

However:

  • {1,2} does not define an array of ints. It specifies a list of values which can be used as initializers to fields of type int (or convertible).

The analogous case for the int would be to use a compound array literal:

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

Compound literals default to being writable (unlike string literals), so you can then go v[0][0] = 5;, which you cannot do with the char version.

Sign up to request clarification or add additional context in comments.

2 Comments

If {1,2} does not define an array, why v[] = {1,2} works then?
@Zaratustra because in a declaration, the initial values for an array can be specified by a brace-enclosed list. (The brace-enclosed list isn't an expression). The = sign in a declaration isn't an assignment operator.
2

While the name of an array does decay into a pointer in some contexts (for example myArray[5] is really just another way of saying *(myArray + 5)), arrays and pointers are not the same thing in the C language.

One difference is that the contents of arrays can be initialized with curly braces { } in the same line they are declared. This is not true with pointers.

int a[] = {1,2,3};  // this is okay
int* p  = {7,8,9};  // this isn't

Another difference is that pointer variables can be modified, while the address pointed to by an array name is fixed.

char* p = "hello";
char a[] = "hello";

a = a + 2;    // this is fine
a++;          // this is fine too

b = b + 2;    // these will cause the compiler to complain
b++;

Despite these differences, it is perfectly legal to assign an array's address to a pointer variable--in fact, this is what you are doing when you pass arrays to functions.

int a[] = {234,0,-23,34,3};
int* p = a;                 // this is okay


The following line is legal because you are defining an array of character pointers. The array c[] can be initialized with curly braces { }. But it is still fine to declare character arrays with pointers if you use quotes " ".

char *c[] = {"hello","world"};

This next line isn't allowed because you declared a pointer variable and are also trying to define its contents with { } as it it were an array.

int *v[] = {{1,2},{3,4}};

You should use this instead:

int v[][2] = {{1,2},{3,4}};

2 Comments

The last line does not compile.
Whoops. I forgot that the compiler cannot figure out both array dimensions on its own. It should be fine now.
1

The string "hello" (rather all string declared with quotations [""]) are of type const char* hence the first one succeeds, since it initializes the array with two char pointers . However {1,2} is not of type (int *) hence it would not be used to initialize an array of int pointers.

2 Comments

Ok, I got that. But what type is {1,2} then?
Since pointers are not arrays we cannot use aggregate initializers to fill out the memory. Using int a[2] = {1,2} would work since the array is allocated memory on the stack and curly braces are used to initialize the array however a pointer doesn't behave the same. For reference : en.cppreference.com/w/cpp/language/aggregate_initialization

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.