main.c 是我的一段代码,它采用头文件 test.h 以及我的 test.h 和 test.c 文件,如下所示。我无法编译和执行它。我哪里出错了?
#ifndef TEST_H_
#define TEST_H_
char test(void);
#endif
是我的测试.h
#include "test.h"
char test(void)
{
return "abcd";
}
是我的 test.c 文件
#include <stdio.h>
#include "test.h"
int main(){
char z = test();
printf("%s\n",z);
return 0;
}
是我的 main.c 文件。
最佳答案
“字符测试(无效);” 应该: “字符*测试(无效);
“字符z” 应该: “字符* z”
您从 test() 返回一个字符数组(技术上是指向数组开头的指针),而不是单个字符。
编辑刚刚编译的内容,它可以工作
#ifndef TEST_H_
#define TEST_H_
char* test(void);
#endif
是新的test.h
#include "test.h"
char* test(void)
{
return "abcd";
}
是新的test.c文件
#include <stdio.h>
#include "test.h"
int main(){
char* z = test();
printf("%s\n",z);
return 0;
}
是新的 main.c 文件。
关于c - C 中带有字符串的函数的头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43890563/