使用写入时,c中的字符串poniter不工作*
我写这个c程序是为了学习,我发现使用写入时,c中的字符串poniter不工作*,c,arrays,string,pointers,C,Arrays,String,Pointers,我写这个c程序是为了学习,我发现 printf(“%s”,a)有效,但printf(“%s”,*a)不起作用 我定义了a这样的char*a[]=“你好” 为什么*a没有指向字符串 我得到了这个错误 test2.c: In function ‘main’: test2.c:7:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=] printf("%s
printf(“%s”,a)代码>有效,但printf(“%s”,*a)代码>不起作用
我定义了a
这样的char*a[]=“你好”代码>
为什么*a
没有指向字符串
我得到了这个错误
test2.c: In function ‘main’:
test2.c:7:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("%s \n",*a);
~^ ~~
%d
它是未定义的行为,因为%s
printf将*a
解释为一个地址,但实际上它不是一个地址,如果您将其视为地址,它将指向某个为您的程序读保护的位置。所以,你可能会出现分段错误。char*a[]=“你好”代码>应该是char*a[]={“你好”}代码>您应该始终使用-Wall
(或编译器的等效工具)编译,并注意这些警告。是的。我得到了分段错误。但是对于其他指针,比如int,我必须写*a来打印存储在a的加法器中的值。