1

When my professor taught about double-pointers and two-dimensional arrays, he wrote like this in his lesson:

char* words[]={"one","two","three"};

Later he introduced another kind of writing style:

int (*table)[3];

But after class, when I try writing on my own, I wrote:

char (*word)[6]={"one","two","three"};

Then it came error: the error

Furthermore, if I wrote like:

char table[][6]={"one","two","three"};
char (*word)[6]=table;

It ran successfully, which meant it just couldn't be initialized(?)

Could you please tell me the difference between

char* words[]={"one","two","three"};

and

char (*word)[6]={"one","two","three"};

and tell me why the latter one encountered with error?

I googled and searched the error info in the website, but found nothing useful. Neither can my classmates answer my questions, I have asked my prof but he hasn't answered me yet.

I have found the difference between the two styles, the only question now is the cause of the error!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Gaucher21
  • 13
  • 3
  • The former declares an array of pointers to ```char```. The latter is a pointer to an array of 6 ```char```s. – Harith Jan 16 '23 at 07:54
  • Thank you, Harris! But could you tell me why my initializing the latter one encountered with the error? – Gaucher21 Jan 16 '23 at 07:59
  • @Haris Cool! Thanks for your recommendation! Now I'm clear with the difference of the two styles, but that whether I can view both array of pointers and pointer to array as equivalent with two-dimensional array still baffles me. And I just still can't find the cause of the error...QAQ Anyway, you have my heartful gratitude! – Gaucher21 Jan 16 '23 at 08:15

1 Answers1

1
  • char* words[]={"one","two","three"}; This is a one-dimensional array of pointers to char. Since they are pointing to string literals, it should most often be written as const char* since we can't write to a pointed-at string literal.

  • int (*table)[3]; This is a different beast, a single pointer to an array of type int [3]. It has no relation to arrays of pointers. These kind of pointers are often called "array pointers" and they are not really a beginner topic, so why your professor introduced them this early, I don't know.

  • char (*word)[6]={"one","two","three"}; Won't work since you declared a pointer to an array of type char [6] then try to initialize it with 3 string literals.

    In formal standard C terms, a single variable or pointer is known as a "scalar", whereas an array or struct is known as an "aggregate". Therefore a char(*)[6] is a scalar and you may only give one single initializer to a scalar, not 3 as in your case, hence the compiler error. (You may however surround that single initializer with braces, int a={1}; is for example equivalent to int a=1;.)

  • char table[][6]= This is a true 2D array and after initialization it will get the type char[3][6]. You allocated exactly 3*6 = 18 bytes in memory and no pointers. But you actually allocated memory to hold the data, instead of just pointing at it, meaning that each string can be changed. (Although it can't be longer than 5 characters + 1 null terminator.) So the string literals in this case aren't just pointed at, they get copied into the 2D array upon initialization.

  • char (*word)[6]=table; This works because any array, table in this case, "decays" into a pointer to its first element when used in most expressions. The first element of an array char [3][6] is of type char [6]. A pointer to the first element is char(*)[6] so the code is fine.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Thanks a lot! But why you can initialize like char* word="one", even though obviously "one" isn't a char* variable? Is there something subtle with char* and string? – Gaucher21 Jan 16 '23 at 12:45
  • In other words, can we put it like this: string and char* are equivalent ? – Gaucher21 Jan 16 '23 at 13:12
  • 1
    @Gaucher21 Yes there is something subtle, C has special initialization rules for string literals. In case of `char* word="one";` you _point at_ the string literal (which is an array in itself). But in case of `char word[] = "one";` you allocate memory with the size of the string literal and then the string literal is copied into that memory. Generally arrays must be initialized with an initializer list `{ 1, 2, 3 }` but with character arrays we can use string literals as an option, and without the braces. This is one of many reasons why I always say: learn arrays, then pointers, then strings. – Lundin Jan 16 '23 at 13:50
  • Thx! I think now I’m totally clear with it! By the way, I’m just a freshman in China majoring computer science, and I came up with the questions while reviewing the lessons in last semester. All in all, thank you for bothering to answer my questions! – Gaucher21 Jan 17 '23 at 08:38