c - C 中带有字符串的函数的头文件

标签 c string header

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/

相关文章:

c - 优化 O(n^2) 到 O(n)(未排序的字符串)

c# - 引用 C# 中的几个字符串之一

c - 返回时指针崩溃 - C

button - 在 ionic 中,当向后滑动时,导航栏变为空白

PHP:强制下载后显示内容?

java - Android:在页眉和页脚之间显示 ListView

c++ - 如何仅在 C 中将 SIGINT 发送到后台进程

c - 调用函数时出现段错误

python - 如何使用 ctypes 从 Python 创建 C 结构体?

java - 将 %s 替换为列表的随机变量