0

So I am in the process of learning C and am slightly confused by this statement on the website I am learning from.

if you have

char ch_arr[3][10] = {
                         "spike",
                         "tom",
                         "jerry"
                     };

It says that.

The ch_arr is a pointer to an array of 10 characters or int(*)[10]

However, what is the meaning of int(*)[10], if this is a character array, why is the pointer not a char type?

Thanks in advance, sorry if the question is bad, I'm trying to get better at asking questions.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Sekken
  • 1
  • 2
  • 2
    What's the web site? A pointer to an array of 10 characters is of type `char(*)[10]`, not `int(*)[10]`. Could be a typo on the site. – Keith Thompson Mar 25 '21 at 22:12
  • Hello, the website and specific page I am looking at is here https://overiq.com/c-programming-101/array-of-strings-in-c/ – Sekken Mar 25 '21 at 22:14
  • 3
    "ch_arr is a pointer " -->. No, `ch_arr` is an array. [array 3 of array 10 of char](https://cdecl.org/?q=char+ch_arr%5B3%5D%5B10%5D+). Pointer are not arrays. Arrays are not pointers. – chux - Reinstate Monica Mar 25 '21 at 22:14
  • But isn't the name of an array a pointer to the first element in C? – Sekken Mar 25 '21 at 22:15
  • To be precise, the object named `ch_arr` is an object of array type (and saying it's a pointer is misleading) -- however the *expression* `ch_arr` is, in most but not all contexts, treated as a pointer expression. See section 6 of the [comp.lang.c FAQ](http://www.c-faq.com/). – Keith Thompson Mar 25 '21 at 22:16
  • Worth reading on [arrays and pointers](https://stackoverflow.com/questions/48868367/whats-a-modern-term-for-array-pointer-equivalence). – jarmod Mar 25 '21 at 22:17
  • 1
    @Sekken No, it's "converted" to a pointer in most but not all contexts. For example, `sizeof ch_arr` yields the size of the array object, not the size of a pointer. See the link in my previous comment. Arrays are not pointers. – Keith Thompson Mar 25 '21 at 22:17
  • I believe I understand now, thank you for these resources they are wonderful and I will check them out, thank you all for quick responses and helping me out! – Sekken Mar 25 '21 at 22:19
  • See [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). Array/Pointer conversion applies to the first dimension of every array. – David C. Rankin Mar 25 '21 at 22:29

1 Answers1

0

This is a two-dimensional array. Each string (e.g. "spike") is a character array. That means that it's basically an array containing arrays.

char ch_arr[3][10] = {"spike", "tom", "jerry"};

The two-dimensional array ch_arr can then be converted to a pointer. In both C and C++ array to pointer conversion applies to every array automatically on access, so the pointer points to the first element, which is of type char[10] in this case. Therefore the pointer is of type char(*)[10], as Keith Thoumpson pointed out in the comments.

And concerning the website you are using... It contains some mistakes, so I would use a different source instead.

Andy Sukowski-Bang
  • 1,402
  • 7
  • 20
  • Hello, thanks, I understand what a 2D array is however int(*)[10] this syntax is quite confusing, could you explain to me what it is saying in english? Thanks – Sekken Mar 25 '21 at 22:15
  • Array/Pointer conversion applies to every array automatically on access. See [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). – David C. Rankin Mar 25 '21 at 22:32
  • cdecl is a useful resource, either https://cdecl.org/ or the application. For example, `declare foo as pointer to array 10 of char` gives you `char (*foo)[10]`. Drop the identifier to get the type name: `char (*)[10]`, which means "pointer to array 10 of `char`". – Keith Thompson Mar 25 '21 at 22:32
  • And remember that a 2D array is nothing more or less than an array of arrays. – Keith Thompson Mar 25 '21 at 22:32
  • @KeithThompson that's exactly what I wrote: _That means that it's basically an array containing arrays._ – Andy Sukowski-Bang Mar 25 '21 at 22:33
  • Yes, but the point being is array/pointer conversion applies not only to `ch_arr`, but also to `ch_arr[i]`. – David C. Rankin Mar 25 '21 at 22:34