gpt4 book ai didi

c - 不确定为什么编译器会提示......函数 strchrnul 的隐式声明

转载 作者:太空狗 更新时间:2023-10-29 15:58:42 26 4
gpt4 key购买 nike

这是我的代码:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

static int valid_line(char *cur_line) {
if (*cur_line == '#') {
return 0;
}

char *end = strchrnul(cur_line, '#');

while(end > cur_line && isspace(*end)) end--;
return (!(*cur_line == *end));
}

我正在检查整行,并去除前导和尾随空格以及“#”(包括“#”)之后出现的任何内容。

我的编译器是这样说的:

parser.c:20:2: warning: implicit declaration of function ‘strchrnul’ [-Wimplicit-    function-declaration]
parser.c:20:14: warning: initialisation makes pointer from integer without a cast [enabled by default]

尽管我上面有 string.h

谁能解释一下。

最佳答案

strchrnul()是一个 GNU 扩展,你可以通过 feature test macro 获得这个包含警告的函数。 .

#define _GNU_SOURCE  // required for strchrnul()

#include <stdio.h>
#include <ctype.h>
#include <string.h> // required for strchrnul()
#include <stdlib.h>

static int valid_line(char *cur_line) {
if (*cur_line == '#') {
return 0;
}

char *end = strchrnul(cur_line, '#');

while(end > cur_line && isspace(*end)) end--;
return (!(*cur_line == *end));
}

请注意第二个链接的手册页,#define 的位置很重要:

NOTE: In order to be effective, a feature test macro must be defined before including any header files

关于c - 不确定为什么编译器会提示......函数 strchrnul 的隐式声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16903285/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com