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

C 如何正确定义函数返回的结构?,c,arrays,function,pointers,structure,C,Arrays,Function,Pointers,Structure,我试图创建一个函数,为“main”中定义的结构数组分配内存。问题似乎是我的函数无法识别结构。以下代码有什么问题 #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 *a

我试图创建一个函数,为“main”中定义的结构数组分配内存。问题似乎是我的函数无法识别结构。以下代码有什么问题

#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);
}

移动此声明
typedef struct_complex{float r;float i;}complex到“其他”文件。另一个文件必须是您的foo.h文件,该文件具有实现foo.h中声明的方法的foo.c等效文件。然后,只需将foo.h添加到主.c文件中,一切都会正常工作。

移动此声明
typedef struct\u complex{float r;float i;}complex到“其他”文件。另一个文件必须是您的foo.h文件,该文件具有实现foo.h中声明的方法的foo.c等效文件。然后,您只需将foo.h添加到主.c文件中,一切都会正常工作。

下面是一段代码,其中包含编译良好的更正:

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);
}

下面是一段代码,其中包含编译良好的更正:

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);
}

基于fvdalcin的回答:

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);
}
(使用#ifdef是一个好主意,可以避免它被多次包含。)

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);
}

基于fvdalcin的回答:

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);
}
(使用#ifdef是一个好主意,可以避免它被多次包含。)

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);
}

您的
结构被称为
\u complex
。你为什么要摆弄那些脏兮兮的
typedef
?此外,在这种情况下,您应该将
struct的
definition移动到*.h文件,以便从两个*.c文件中包含。错误:“complex”未声明(首次在此函数中使用)必须将声明放在公共头文件中。。。(请不要强制转换malloc()的返回值。
!也不要混乱保留的命名空间(即不要使用以下划线开头的标识符)等。@H2CO3-有没有不使用头文件的方法?@JWDN您可以复制定义,但最好使用头文件,这样定义就不会同时出现在两个位置;任何时候这样做,您都可能会犯一个错误,即更新一个而忘记更新另一个。为什么不使用头文件呢de>struct
被称为
\u complex
。你为什么要使用那些脏的
typedef
?同样,在这种情况下,你应该将
struct的
definition移到*.h文件中,以便从两个*.c文件中都包含进去。错误:“complex”未声明(首次用于此函数)必须将声明放在一个公共头文件中。。。(请不要强制转换malloc()的返回值。
!也不要混乱保留的命名空间(即不要使用以下划线开头的标识符)等。@H2CO3-有没有不使用头文件的方法?@JWDN您可以复制定义,但最好使用头文件,这样定义就不会同时出现在两个位置;任何时候这样做,您都可能会犯一个错误,即更新一个而忘记更新另一个。为什么不使用头文件呢?谢谢或者捕获*。这将工作,但我需要一个单独的文件中的函数,因此许多程序可以调用它:)感谢捕获*。这将工作,但我需要一个单独的文件中的函数,因此许多程序可以调用它:)