简体   繁体   中英

Difference between array of pointers to char and array of pointers to int

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.

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 int s. 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.

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}};

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM