strlen.c:7:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int (*)(char *)’ [-Wformat=] printf("%d\n",my_strlenfun); ~^ ~~~~~~~~~~~~ strlen.c: At top level: strlen.c:10:5: error: conflicting types for ‘my_strlenfun’ int my_strlenfun(char const * ars) ^~~~~~~~~~~~ strlen.c:2:5: note: previous declaration of ‘my_strlenfun’ was here int my_strlenfun(char *); ^~~~~~~~~~~~是什么意思
时间: 2023-07-03 20:33:09 浏览: 75
这个错误提示意思是:文件 `strlen.c` 的第 7 行有一个警告,它告诉你 `printf` 函数期望的第二个参数是一个 `int` 类型,但是你传递给它的是一个函数指针,这是不匹配的。此外,第 10 行有一个错误,原因是 `my_strlenfun` 函数的类型与之前声明的不匹配。这意味着你在代码中有一个函数名冲突的问题。你需要检查并修复这些问题,以使代码可以正确编译和运行。
相关问题
c语言warning C4013: “gets”未定义;假设外部返回 int
这个warning提示你在代码中使用了gets函数,但是该函数在C11标准中已经被废弃,因此编译器提示你该函数未定义。另外,该warning还假设你使用的gets函数返回int类型的值,这显然是不正确的。
为了解决这个问题,你可以使用fgets函数代替gets函数,fgets函数在C11标准中已经取代了gets函数,在使用上更加安全。fgets函数的声明如下:
```c
char* fgets(char* str, int n, FILE* stream);
```
其中,str表示指向一个字符数组的指针,用于存储读入的字符串;n表示读入的最大字符数;stream表示文件指针,用于指定从哪个文件中读取字符串。
在使用fgets函数时,你需要注意以下几点:
1. fgets函数会读取换行符(\n),因此在使用读取字符串之后,你需要手动将换行符替换为NULL字符(\0)。
2. fgets函数读取的字符串可能会包含换行符,因此在使用读取字符串之前,你需要判断字符串的最后一个字符是否为换行符,如果是,就将其替换为NULL字符。
下面是一个使用fgets函数替换gets函数的例子:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[100];
printf("Enter a string: ");
if(fgets(str, sizeof(str), stdin) != NULL) {
if(str[strlen(str)-1] == '\n') {
str[strlen(str)-1] = '\0';
}
printf("You entered: %s\n", str);
}
else {
printf("Error reading string.\n");
exit(EXIT_FAILURE);
}
return 0;
}
```
note: expected ‘const char *’ but argument is of type ‘char’ char *mystrstr(const char* s1,const char* s2)
这个错误是因为在`main`函数中,您将`char`类型的字符串传递给了`mystrstr`函数,而`mystrstr`函数期望接收的是`const char*`类型的字符串。
为了解决这个问题,您需要将`s2`和`s3`声明为`const char*`类型的指针,而不是`char`类型的数组。
请将以下代码:
```c
char s2[] = "helloworld";
char s3[] = "ow";
```
改为:
```c
const char* s2 = "helloworld";
const char* s3 = "ow";
```
这样就能够解决这个问题。修正后的代码如下:
```c
#include<stdio.h>
#include<string.h>
char *mystrstr(const char* s1, const char* s2)
{
int i = 0, a = 0, c = 0;
int d = strlen(s2);
while (s1[i] != '\0' && a <= d - 1)
{
c = i;
while (s1[i] == s2[a])
{
i++;
a++;
}
if (s2[a] == '\0')
{
return (char*)(s1 + c);
}
i = c;
i++;
}
return NULL;
}
int main()
{
const char* s2 = "helloworld";
const char* s3 = "ow";
printf("%s\n", mystrstr(s2, s3));
return 0;
}
```
现在代码应该能够正确编译和运行了。