0
char strings[10][100], ** ptr = strings;

Someone can help with it? What isn't right with this part of code, isn't right that **strings == strings[0][0], and strings[0][0] is char? Sorry for my really bad english.

  • Well, yes, `char (*)[100]` does not equate to `char **`. So `char strings[10][100], (*ptr)[100] = strings;` – David C. Rankin Jul 31 '20 at 07:32
  • 1
    Full explanation [2D array seg fault in C](https://stackoverflow.com/a/60498812/3422102) – David C. Rankin Jul 31 '20 at 07:39
  • Hint: There's a huge difference between `char *a[n]` and `char a[n][m]`. One is an array of pointers, the other is an array of arrays. A 2D array is allocated as a contiguous block, but an array of pointers to other arrays is usually *not*. – tadman Jul 31 '20 at 07:42

2 Answers2

1

The compiler recognizes that strings is an array of arrays and therefore suggests to declare the pointer accordingly:

char strings[10][100], (*ptr)[100] = strings;

Update:

As mentioned by David C. Rankin a conversion takes place in the initialization of ptr. strings is converted from an array of arrays of 100 chars to a pointer to an array of 100 chars pointing to the first element of this array of arrays. The conversion is specified here: C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3).

Stefan Scheller
  • 953
  • 1
  • 12
  • 22
  • 1
    It would help to explain array conversion to a pointer and reference [C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3)](http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1p3) as the source of the conversion. – David C. Rankin Jul 31 '20 at 07:34
  • You are right. Thank you for the link, I updated my answer accordingly. – Stefan Scheller Jul 31 '20 at 07:45
  • But if I use this declaration i cant use this part of my code: (fgets(*(ptr + ct),99,stdin); //ct of int type. How I can fill all the lines of the array with such a declaration? – Dedak Official Jul 31 '20 at 07:48
  • You can, `fgets (ptr[ct], 100, stdin)` is fine. Note, with `fgets()` you do not subtract 1, `fgets (ptr[ct], 100, stdin)` is correct. Where `0 <= ct < 10` – David C. Rankin Jul 31 '20 at 07:50
  • But in this case i get error: invalid use of array with unspecified bounds. I can fix it if i will do char (* ptr)[100] = strings; instead char (* ptr)[] = strings; But what i shoul to do if i don't know about size of my array(i use it in my function). – Dedak Official Jul 31 '20 at 08:05
0

the warning:

Warning: initialization of ‘char **’ from incompatible pointer type ‘char (*)[100]’ [-Wincompatible-pointer-types]
    8 |   char strings[10][100], ** ptr = strings;

It says that: ptr & strings are different types so ptr[0][0¿ & string[0][0] are diffrents

  • But string is array and array's name is pointer on first element. In my case string = =&string[0]; string[0] == &string[0][0] HENCE string == &&string[0][0] HENCE **string == string[0][0] HENCE type of string is char **. What is wrong with it? – Dedak Official Jul 31 '20 at 07:44
  • To test your code I use: `int main(int argc, string argv[]) { char strings[10][100], **ptr = strings; printf("%s\n", strings); if (strings[0][0] == ptr[0][0]) printf("Same"); else printf("Not the same"); return 0; }` Here: It prints Not the same But if I change to ` `char strings[10][100], (*ptr)[100] = strings;` It return Same because there are the same type – Martin Rougeron Jul 31 '20 at 07:49