简体   繁体   English

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

[英]Header file in C for the functions with strings

main.c is my piece of code that takes the header file test.h and my test.h and test.c files as follows. main.c是我的代码,它采用以下头文件test.h以及我的test.h和test.c文件。 I am not able to compile and execute this. 我无法编译并执行此操作。 Where am I going wrong? 我要去哪里错了?

#ifndef TEST_H_
#define TEST_H_

char test(void);

#endif

is my test.h 是我的考验

 #include "test.h"

  char test(void)
 {
   return "abcd";
 }

is my test.c file 是我的test.c文件

#include <stdio.h>
#include "test.h"

int main(){

   char z = test();
   printf("%s\n",z);
   return 0;
  }

is my main.c file. 是我的main.c文件。

"char test(void);" “字符测试(无效);” should be: "char* test(void); 应该是:“ char * test(void);

"char z" should be: "char* z" “ char z”应为:“ char * z”

You are returning a char array (technically a pointer to the start of the array) from test() not a single char. 您要从test()返回一个char数组(从技术上讲是指向数组开头的指针),而不是单个char。

Edit just compiled and it works 编辑刚刚编译就可以了

#ifndef TEST_H_
#define TEST_H_

char* test(void);

#endif

is the new test.h 是新的test.h

 #include "test.h"

  char* test(void)
 {
   return "abcd";
 }

is the new test.c file 是新的test.c文件

#include <stdio.h>
#include "test.h"

int main(){

   char* z = test();
   printf("%s\n",z);
   return 0;
  }

is the new main.c file. 是新的main.c文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM