我想創建一個函數,它爲在「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);
}
...而在另一個文件...
struct complex *myfunction(int n) {
complex *result = (complex *)malloc(n*sizeof(*complex));
if(result==NULL) return(NULL);
else return(result);
}
你在看什麼? –
你的'struct'被稱爲'_complex'。你爲什麼用那些髒的'typedefs'搞砸?此外,在這種情況下,您應該將'struct's' deffinition移動到* .h文件以包含在兩個* .c文件中。 – Kamiccolo
錯誤:'複雜'未聲明(首次在此函數中使用) – JWDN