“隐式声明”警告
对于此代码:
int i=0; char **mainp;
for(i=0;i<2;++i)
{
mainp[i]=malloc(sizeof(char)*200);
if(!scanf("%[^#],#",mainp[i]))
break;
if(i<2)
scanf("%[^#],#",mainp[i]);
}
GCC 发出警告:
warning: implicit declaration of function ‘scanf’
warning: incompatible implicit declaration of built-in function ‘scanf’
warning: ‘mainp’ may be used uninitialized in this function
并且我在运行时输入时出现分段错误
:(P>Q),(Q>R),-R#-P 输出: (P>Q),(Q>R),-R (空槽)
我期望给我 (P>Q),(Q>R),-R -P //我应该在代码中修复哪里,以便它给我预期的结果 //输出
For this code:
int i=0; char **mainp;
for(i=0;i<2;++i)
{
mainp[i]=malloc(sizeof(char)*200);
if(!scanf("%[^#],#",mainp[i]))
break;
if(i<2)
scanf("%[^#],#",mainp[i]);
}
GCC emits the warnings:
warning: implicit declaration of function ‘scanf’
warning: incompatible implicit declaration of built-in function ‘scanf’
warning: ‘mainp’ may be used uninitialized in this function
And I get a segmentation fault at runtime
input:(P>Q),(Q>R),-R#-P
output:
(P>Q),(Q>R),-R
(empt slot)
i expected to give me
(P>Q),(Q>R),-R
-P //where should i fix in my code such that it gives me expected
//output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题#1:
您需要首先为数组的数组分配内存。
问题#2:
您需要在文件顶部包含
stdio.h
:问题 #3:(不包含在您的编译警告中)< /strong>
请记住释放已分配的数组成员以及数组地址的数组。
Problem #1:
You need to allocate memory for the array of arrays first.
Problem #2:
You need to include
stdio.h
at the top of your file:Problem #3: (Not included in your compiling warnings)
Remember to free both the allocated array members and also the array of array address.
gcc 期望在文件的开头添加这一行:
以及 mainp 的声明,如下所示:
gcc expects this line at the beginning of your file:
and a declaration of mainp like this one:
不应该在没有声明的情况下使用函数;您使用了
scanf
,但代码中没有声明scanf
。由于它是一个标准库函数,它在标准头文件之一stdio.h
中声明,因此您只需包含它:Brian 的 答案对另一部分有好处
You shouldn't use functions without declaring them; you used
scanf
, but at no point in your code isscanf
declared. Since it's a standard library function it's declared in one of the standard headers,stdio.h
, so you just need to include it:Brian's answer is good for the other part