简体   繁体   English

如何正确定义函数返回的结构?

[英]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.

相关问题 如何为recvfrom()函数中提到的字段定义结构? - how can i define structure to the fields mentioned in recvfrom() function? 如何正确定义和使用返回类型为用户定义结构的函数? (在C中) - How do I properly define and use a function whose return type is a user defined structure? (In C) 如何在struct中正确定义一个函数指针,它将struct作为指针? - How to properly define a function pointer in struct, which takes struct as a pointer? 我们如何在 C 中定义和使用函数内部的结构? - how we can define and use structure inside function in C? 如何定义一个function指针and结构? - How to define a function pointers and and structure? 如何为函数内部的结构分配动态内存插槽,可以从程序中的任何位置访问该结构 - How can I Allocate a dynamic memory slot for a structure inside a function, Which can be accessed from anywhere in the program 我该如何让#define函数循环? - How can I make for loop of #define function? 我如何定义和调用此代码作为函数? - How can I define and call this code as a function? 如何将扫描的结构打印出来以正确归档? (C) - How can I print out scanned in structure to file properly? (C) 如何为一个数组(结构类型)元素正确赋值? - How can I properly assign a value to an array (of structure type) element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM