Original
Why cannot char (*)[N]
be converted to char **
?
What happened when char *[N]
was converted to char (**)[N]
? When I convert char *[N]
to char (**)[N]
, it did work.
#include <stdio.h>
int main() {
char *a[2]={"a", "a"};
char b[2][2]={"b", "b"};
// Expect "a, b"
printf("%s, %s\n", a[0], b[0]); // 1. OK
printf("%s, %s\n", *a, *b); // 2. OK
printf("%s, %s\n", *(char **)a, *(char **)b); // 3. Segmentation fault
printf("%s, %s\n", *(char **)a, *(char (*)[2])b); // 4. OK
printf("%s, %s\n", *(char (*)[2])a, *(char (*)[2])b); // 5. Wrong output
printf("%s, %s\n", *(char (**)[2])a, *(char (*)[2])b); // 6. Correct output
}
Edited
Why cannot char (*)[N]
be converted to char **
?
What happened when char *[N]
was converted to char (**)[N]
? When I convert char *[N]
to char (**)[N]
, it did work.
Why do 5.0
and 5.1
have the same value and same address?
What's the value of 5.2
?
#include <stdio.h>
#define N 10
// #define S "123456789"
#define S "abcdefghi"
int main() {
char *a[2]={S, S};
char b[2][N]={S, S};
// Expect "a, b"
printf("1. %s, %s\n", a[0], b[0]); // 1. OK
printf("2. %s, %s\n", *a, *b); // 2. OK
printf("3. %s, %s\n", *(char **)a, *(char **)b); // 3. Segmentation fault
printf("4. %s, %s\n", *(char **)a, *(char (*)[N])b); // 4. OK
printf("5.0. %s, %s\n", (char (*)[N])a, *(char (*)[N])b); // 5.0. Wrong output
printf("5.1. %s, %s\n", *(char (*)[N])a, *(char (*)[N])b); // 5.1. Wrong output
printf("5.2. %s, %s\n", **(char (*)[N])a, *(char (*)[N])b); // 5.2. Segmentation fault
printf("6.0. %s, %s\n", (char (**)[N])a, *(char (*)[N])b); // 6.0. Wrong output
printf("6.1. %s, %s\n", *(char (**)[N])a, *(char (*)[N])b); // 6.1. Correct output
}