Page 99 - C-Language
P. 99
print_strings(strings, 4);
^
file1.c:3:10: note: expected 'char **' but argument is of type 'char (*)[20]'
void print_strings(char **strings, size_t n)
The error states that the s array in the main function is passed to the function print_strings, which
expects a different pointer type than it received. It also includes a note expressing the type that is
expected by print_strings and the type that was passed to it from main.
The problem is due to something called array decay. What happens when s with its type
char[4][20] (array of 4 arrays of 20 chars) is passed to the function is it turns into a pointer to its
first element as if you had written &s[0], which has the type char (*)[20] (pointer to 1 array of 20
chars). This occurs for any array, including an array of pointers, an array of arrays of arrays (3-D
arrays), and an array of pointers to an array. Below is a table illustrating what happens when an
array decays. Changes in the type description are highlighted to illustrate what happens:
Before
After Decay
Decay
char [20] array of (20 chars) char * pointer to (1 char)
array of (4 arrays of 20 pointer to (1 array of 20
char [4][20] char (*)[20]
chars) chars)
array of (4 pointers to 1 pointer to (1 pointer to 1
char *[4] char **
char) char)
char array of (3 arrays of 4 arrays char pointer to (1 array of 4
[3][4][20] of 20 chars) (*)[4][20] arrays of 20 chars)
char array of (4 pointers to 1 pointer to (1 pointer to 1
(*[4])[20] array of 20 chars) char (**)[20] array of 20 chars)
If an array can decay to a pointer, then it can be said that a pointer may be considered an array of
at least 1 element. An exception to this is a null pointer, which points to nothing and is
consequently not an array.
Array decay only happens once. If an array has decayed to a pointer, it is now a pointer, not an
array. Even if you have a pointer to an array, remember that the pointer might be considered an
array of at least one element, so array decay has already occurred.
In other words, a pointer to an array (char (*)[20]) will never become a pointer to a pointer (char
**). To fix the print_strings function, simply make it receive the correct type:
void print_strings(char (*strings)[20], size_t n)
/* OR */
void print_strings(char strings[][20], size_t n)
A problem arises when you want the print_strings function to be generic for any array of chars:
https://riptutorial.com/ 75

