C 指针/解引用错误
我想显示“字符串指针受影响”,但我得到一个错误。这是我的密码:C 指针/解引用错误,c,C,我想显示“字符串指针受影响”,但我得到一个错误。这是我的密码: #include<stdio.h> main() { char* *p; char * s="string pointer affected"; *p=s; printf("%s",*p); } #包括 main() { 字符**p; char*s=“受影响的字符串指针”; *p=s; printf(“%s”,*p); } 取消引用未初始化的指针,这将导致未定义的行为。这就是问题所在- *p=s
#include<stdio.h>
main()
{
char* *p;
char * s="string pointer affected";
*p=s;
printf("%s",*p);
}
#包括
main()
{
字符**p;
char*s=“受影响的字符串指针”;
*p=s;
printf(“%s”,*p);
}
取消引用未初始化的指针,这将导致未定义的行为。这就是问题所在-
*p=s;
p
没有指向任何已知位置,因此向*p
写入是个坏主意
你的意思是说:
p = &s;
您正在下面的行和printf
语句中使用未初始化的变量。如果你更换
*p = s;
与
然后它就会工作。试试:
#include<stdio.h>
main()
{
char *p; // <--------------------------
char *s="string pointer affected";
printf("===== s=%p\n", s);
p=s;
printf("===== p=%p\n", p);
printf("%s\n", p);
}
@对不起,我用词不当,先生。你犯了什么错误?我对代码进行了测试,它看起来很有效。理解指针以及如何取消引用指针可以避免这个问题。请参阅以获取解释。
#include<stdio.h>
main()
{
char *p; // <--------------------------
char *s="string pointer affected";
printf("===== s=%p\n", s);
p=s;
printf("===== p=%p\n", p);
printf("%s\n", p);
}
#include<stdio.h>
main()
{
char *q;
char **p = &q;
char *s="string pointer affected";
*p=s;
printf("%s\n", *p);
}