[英]How can I properly define a structure which is returned by a function?
I am trying to create a function which allocates memory for a structure array defined in "main". 我正在尝试创建一个为“main”中定义的结构数组分配内存的函数。 The problem seems to be that my function does not recognize the structure.
问题似乎是我的功能无法识别结构。 What is wrong with the following code?
以下代码有什么问题?
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct typecomplex { float r; float i; } complex;
complex *myfunction(int n);
int main (int argc, char *argv[]) {
complex *result = myfunction(1000);
exit(0);
}
... and in another file... ......在另一个文件中......
struct complex *myfunction(int n) {
complex *result = (complex *)malloc(n*sizeof(*complex));
if(result==NULL) return(NULL);
else return(result);
}
Building on fvdalcin's answer: 以fvdalcin的答案为基础:
myprog.c: myprog.c中:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "mycomplex.h"
int main (int argc, char *argv[]) {
complex *result = myfunction(1000);
exit(0);
}
mycomplex.h: mycomplex.h:
#ifndef __MYCOMPLEX_H__
typedef struct typecomplex { float r; float i; } complex;
complex *myfunction(int n);
#define __MYCOMPLEX_H__
#endif
(The #ifdef's are a good idea to keep it from being included more than once.) (#ifdef是一个好主意,不让它被包含多次。)
mycomplex.c: mycomplex.c:
#include <stdlib.h>
#include "mycomplex.h"
complex *myfunction(int n) {
complex *result = malloc(n*sizeof(complex));
if(result==NULL) return(NULL);
else return(result);
}
Note subtle but important fixes here--sizeof(complex) instead of sizeof(complex*), declaration of myfunction() doesn't include the keyword "struct", and no cast on malloc()--it doesn't need one and can hide the fact that you may be missing the include file with its prototype (see Do I cast the result of malloc? ). 注意这里微妙但重要的修复 - sizeof(复杂)而不是sizeof(complex *),myfunction()的声明不包含关键字“struct”,并且没有对malloc()进行强制转换 - 它不需要一个并且可以隐藏您可能错过包含其原型的包含文件的事实(请参阅我是否转换了malloc的结果? )。
myfunction()
could actually by simplified down to one line: myfunction()
实际上可以简化为一行:
return malloc(n*sizeof(complex));
Move this declaration typedef struct _complex { float r; float i; } complex;
移动此声明
typedef struct _complex { float r; float i; } complex;
typedef struct _complex { float r; float i; } complex;
to the "other" file. 到“其他”文件。 This other file has to be your foo.h file, which has an foo.c equivalent which implements the methods declared in the foo.h .
这个其他文件必须是你的foo.h文件,它有一个foo.c等价文件,它实现了foo.h中声明的方法。 Then you can simply add the foo.h to your main.c file and everything will work fine.
然后你可以简单地将foo.h添加到你的main.c文件中,一切都会正常工作。
Here is a code with corrections that compiles well: 这是一个编译得很好的代码:
typedef struct typecomplex { float r; float i; } complex;
complex *myfunction(int n) {
complex *result = (complex *)malloc(n*sizeof(complex)); //removed * from sizeof(*complex)
if(result==NULL) return(NULL);
else return(result);
}
int main (int argc, char *argv[]) {
complex *result = myfunction(1000);
exit(0);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.