I have the following code in C
#include <stdio.h>
char *gstr[] = { "Hello", "World" };
void func(char str[][8]) {
printf("%p\n", (void *)str);
printf("%p\n", (void *)(str + 1));
printf("%s\n", *(str + 1));
}
int main(void) {
printf("%p\n", (void *)gstr);
printf("%p\n", (void *)(gstr + 1));
printf("%s\n", *(gstr + 1));
printf("Calling Function\n");
func(gstr);
return 0;
}
The following is the output
0x556f11ddd010
0x556f11ddd018
World
Calling Function
0x556f11ddd010
0x556f11ddd018
$��oU
I cannot understand why printf("%s\n", *(str+1)); in the function func does not print the string world.
void func(char str[][8])? How did you compile your code - did you receive any warnings?gccfor example, compile withgcc -Wall -Werror -Wextra -gand resolve any errors that show. You can't expect a certain behavior when you run invalid codeincompatible pointer type ... expected ‘char (*)[8]’ but argument is of type ‘char **’