gpt4 book ai didi

c - 收到警告 "implicit declaration of function ' strlen'"

转载 作者:太空狗 更新时间:2023-10-29 16:24:46 26 4
gpt4 key购买 nike

我有一些简单的代码,但我收到了警告:

-bash-3.2$ gcc -Wall print_process_environ.c -o p_p
print_process_environ.c: In function 'print_process_environ':
print_process_environ.c:24: warning: implicit declaration of function 'strlen'
print_process_environ.c:24: warning: incompatible implicit declaration of built-in function 'strlen'

代码如下:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <strings.h>

void
print_process_environ(pid_t pid)
{
int fd;
char filename[24];
char environ[1024];
size_t length;
char *next_var;

snprintf(filename, sizeof(filename), "/proc/%d/environ", (int)pid);
printf("length of filename: %d\n", strlen(filename));

fd = open(filename, O_RDONLY);
......

strlen()的定义是:

   #include <string.h>

size_t strlen(const char *s);

如何摆脱这个警告。

最佳答案

#include <string.h> .您在代码中拼写错误。另外,如果您在编译器中收到该警告.. 总是做 man function_name在终端上查看该功能所需的 header

 #include <string.h> // correct header
#include <strings.h> // incorrect header - change this in your code to string.h

关于c - 收到警告 "implicit declaration of function ' strlen'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19761104/

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