C结构指针-如何从模块返回结构指针?
我有3个不同的文件:main.c、module.h和module.c 模块.c应将2条文字信息“传输”至主设备:C结构指针-如何从模块返回结构指针?,c,pointers,module,C,Pointers,Module,我有3个不同的文件:main.c、module.h和module.c 模块.c应将2条文字信息“传输”至主设备: 一条“信息”信息 和一条“错误”信息 这两条消息是在module.c中生成的 其思想是使用指向struct的指针传递这两条消息。不幸的是,我遗漏了一些关于指针的信息,因为只有第一条消息(“这是信息”)通过。。。第二个在两者之间的某个地方迷路了 /*file:main.c (gcc -o test main.c module.c)*/ #include <stdio.h>
- 一条“信息”信息
- 和一条“错误”信息
/*file:main.c (gcc -o test main.c module.c)*/
#include <stdio.h>
#include <stdlib.h>
#include "module.h"
static struct message *text = NULL;
int main(int argc, char **argv)
{
text = (struct message *) malloc(sizeof(struct message));
text->info_text="toto";
text->error_text="tutu";
text->id = 55;
text = moduleFcn();
printf("message->info_text: %s\n", text->info_text);
printf("message->error_text: %s\n", text->error_text);
printf("message->id: %u\n", text->id);
return 0;
}
/*文件:main.c(gcc-o test main.c module.c)*/
#包括
#包括
#包括“模块h”
静态结构消息*text=NULL;
int main(int argc,字符**argv)
{
text=(结构消息*)malloc(sizeof(结构消息));
text->info_text=“toto”;
text->error\u text=“tututu”;
text->id=55;
text=moduleFcn();
printf(“消息->信息文本:%s\n”,文本->信息文本);
printf(“消息->错误文本:%s\n”,文本->错误文本);
printf(“消息->标识:%u\n”,文本->标识);
返回0;
}
和模块
/*module.h*/
struct message
{
char *info_text;
char *error_text;
int id;
};
extern struct message* moduleFcn(void);
/*module.c*/
#include <stdio.h>
#include "module.h"
static struct message *module_text = NULL;
struct message* moduleFcn(void)
{
struct message dummy;
module_text = &dummy;
module_text->info_text = "This is info";
module_text->error_text = "This is error";
module_text->id = 4;
return module_text;
}
/*module.h*/
结构消息
{
字符*信息\文本;
字符*错误\文本;
int-id;
};
外部结构消息*moduleFcn(无效);
/*模块c*/
#包括
#包括“模块h”
静态结构消息*module_text=NULL;
结构消息*moduleFcn(无效)
{
结构消息伪;
模块_text=&dummy;
模块\文本->信息\文本=“这是信息”;
模块\文本->错误\文本=“这是错误”;
模块_text->id=4;
返回模块_文本;
}
提前谢谢你帮助我。
斯蒂芬
更改模块代码和主要功能。在模块部分的堆上分配struct并返回该结构。在main函数中,为什么要分配一个结构并用moduleFcn()返回的struct覆盖它
/*module.h*/
结构消息
{
字符*信息\文本;
字符*错误\文本;
int-id;
};
外部结构消息*moduleFcn(无效);
/*模块c*/
#包括
#包括“模块h”
结构消息*moduleFcn(无效)
{
结构消息*伪=(结构消息*)malloc(sizeof(结构消息));
dummy->info_text=“这是信息”;
虚拟->错误\u text=“这是错误”;
虚拟->id=4;
返回假人;
}
在main()中,执行以下更改
/*文件:main.c(gcc-o test main.c module.c)*/
#包括
#包括
#包括“模块h”
int main(int argc,字符**argv)
{
结构消息*text=moduleFcn();
printf(“消息->信息文本:%s\n”,文本->信息文本);
printf(“消息->错误文本:%s\n”,文本->错误文本);
printf(“消息->标识:%u\n”,文本->标识);
免费(文本);
返回0;
}
首先,不能返回指向局部变量的指针。从函数返回后,局部变量将消失,而指向它的指针则毫无意义。第二,为什么你要花4行来创建文本
,然后立即用moduleFun
返回的值覆盖它?可能是@DYZ的重复,我在评论行的同时使用了这4行//text=moduleFcn()=>只是为了“理解”出了什么问题……非常感谢。已解决:-)
/*module.h*/
struct message
{
char *info_text;
char *error_text;
int id;
};
extern struct message* moduleFcn(void);
/*module.c*/
#include <stdio.h>
#include "module.h"
struct message* moduleFcn(void)
{
struct message *dummy = (struct message*)malloc(sizeof(struct message));
dummy->info_text = "This is info";
dummy->error_text = "This is error";
dummy->id = 4;
return dummy;
}
/*file:main.c (gcc -o test main.c module.c)*/
#include <stdio.h>
#include <stdlib.h>
#include "module.h"
int main(int argc, char **argv)
{
struct message *text = moduleFcn();
printf("message->info_text: %s\n", text->info_text);
printf("message->error_text: %s\n", text->error_text);
printf("message->id: %u\n", text->id);
free(text);
return 0;
}