I wrote two program that I thought were the same but apparently they are not and I cant figure out why.
the first program(works as expected):
#include <stdio.h>
void main()
{
char *p[2][3]={"xyz","wvu","ts","rqponm","lkgihgfe","dcba"};
printf("
%c
",(***p)); /*z*/
printf("
%c
",((*(*(p+1)+1))[6]));/*f*/
printf("
%c
",(**(p[1]+2)));/*d*/
printf("
%c
",(**p[1]));/*r*/
printf("
%c
",(*(p[1][2]+2)));/*b*/
}
the second program:
#include <stdio.h>
void main()
{
char *p[2][3]={{'x','y','z',''},"wvu","ts","rqponm","lkgihgfe","dcba"};
printf("
%c
",(***p)); /*z*/
printf("
%c
",((*(*(p+1)+1))[6]));/*f*/
printf("
%c
",(**(p[1]+2)));/*d*/
printf("
%c
",(**p[1]));/*r*/
printf("
%c
",(*(p[1][2]+2)));/*b*/
}
when I Compile this program I get a warning: excess elements in array initializer.
when I run it I get an error: Segmentation fault (core dumped).
obviously the problem is in {'x','y','z',''} but I thought its the same as "xyz".
question from:
https://stackoverflow.com/questions/65846563/access-via-pointer-to-2d-char-array-warning-excess-elements-in-array-initializ 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…