c - "Implicit declaration"警告

标签 c include header segmentation-fault

对于这段代码:

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//我应该在哪里修复我的代码,以便它给我预期的 //输出

最佳答案

问题#1:

warning: ‘mainp’ may be used uninitialized in this function

首先需要为数组的数组分配内存。

char **mainp = malloc(sizeof(char*)*2);

问题#2:

warning: implicit declaration of function ‘scanf’
warning: incompatible implicit declaration of built-in function ‘scanf’

您需要在文件顶部包含 stdio.h:

#include <stdio.h>

问题 #3:(未包含在您的编译警告中)

记住释放分配的数组成员和数组地址的数组。

关于c - "Implicit declaration"警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2866520/

相关文章:

c - Extern "C"NULL 函数指针

c++ - C1083 : Cannot open include file: math. h: 没有那个文件或目录

php - 这个 PHP 重定向不安全吗?

swift - 如何让 UITableView 的固定标题在滚动后隐藏

c - 弹性 Action 在什么范围内执行?

c - #ifdef 和使用 Makefile 的条件编译

c++ - ‘-’ token 之前应有非限定 ID

c++ - 可以将 #includes 放入 C++ header 中吗?

xaml - ListView 和 GridView WinRT xaml 中的粘性标题

c - 在不知道返回类型的情况下将结构的成员初始化为函数指针