Array of pointers — a structure where each element is a pointer, usually to the same type. It is used, for example, to store a list of strings (char *arr[];).
Pointer to an array — a variable that holds the address of the beginning of the entire array (for example, int (*ptr)[10]; points to an array of 10 int elements).
Examples:
// Array of pointers to char (array of strings) char *days[] = {"Sun", "Mon", "Tue"}; // Pointer to an array of 10 integers int arr[10]; int (*p)[10] = &arr; // Access: printf("%s", days[1]); // "Mon" printf("%d", (*p)[0]); // arr[0]
"What is the difference between
char *arr[3];andchar (*arr)[3];and how to properly access the strings in each case?"
Many believe there is no difference or that they are just synonyms.
Correct answer:
char *arr[3]; — an array of 3 pointers to char (usually an array of strings).char (*arr)[3]; — a pointer to an array of 3 char (a pointer to a single block string of length 3).char *arr1[3] = {"a", "bb", "ccc"}; printf("%s", arr1[1]); // bb char str[3] = {'x', 'y', 'z'}; char (*arr2)[3] = &str; printf("%c ", (*arr2)[2]); // z
Story 1
In a project, they raised an array of strings for a menu but wrote char menu[5][30]; Incorrectly passed it to a function parameter as char *menu, which made the functions work only with the first string, while the others were distorted or the program crashed.
Story 2
They tried to create an array of pointers to arrays: int *arr[10]; initialized it through loops, but actually stored pointers to local arrays, which caused UB when accessed.
Story 3
In the implementation of a FIFO queue for buffers, they confused where to get the address of the entire queue (pointer to an array) and where to get the element (array of pointers to blocks). Because of this, shifts in pointers were incorrect — data was lost.