在参数中使用结构类型数据时,参数函数C中的结构类型未知
如何使用参数将在参数中使用结构类型数据时,参数函数C中的结构类型未知,c,pointers,C,Pointers,如何使用参数将struct类型数据传递给另一个函数?我已经创建了一个globalstruct,但我想我错过了一些东西 当我尝试上面的代码时,我得到了一个错误 未知类型名称“Stuff”(在我调用的第行myFunctionInsert(Stuff*Stuff)) 任何帮助都将不胜感激 谢谢您定义的类型是struct Stuff而不是Stuff。因此: // call another function int myFunctionInsert(struct Stuff *stuff); 或者使
struct
类型数据传递给另一个函数?我已经创建了一个global
struct
,但我想我错过了一些东西
当我尝试上面的代码时,我得到了一个错误
未知类型名称“Stuff”(在我调用的第行myFunctionInsert(Stuff*Stuff)
)
任何帮助都将不胜感激
谢谢您定义的类型是
struct Stuff
而不是Stuff
。因此:
// call another function
int myFunctionInsert(struct Stuff *stuff);
或者使用typedef
:
typedef struct Stuff
{
int id;
String name[20];
} Stuff;
您定义的类型是
struct Stuff
而不是Stuff
。因此:
// call another function
int myFunctionInsert(struct Stuff *stuff);
或者使用typedef
:
typedef struct Stuff
{
int id;
String name[20];
} Stuff;
问题不仅在于缺少
typedef
。
另外,由于(x=0;x<25;x++)的跨越了
struct Stuff *stuff[20];
另外stuff[x].name='stuff';
不会飞行。您的意思是“stuff”。请注意,C语言中没有String
类型
指定传递的结构数组大小的程序的工作版本可能如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct stuff
{
int id;
char name[20];
}Stuff;
// call another function
int myFunctionInsert(Stuff *stuff, int len);
int myFunctionInsert(Stuff *stuff,int len)
{
int x;
for(x = 0; x < len; x++){
stuff[x].id = x;
strcpy( stuff[x].name, "stuff");
printf("stuff %i id=%d name=%s\n", x, stuff[x].id, stuff[x].name );
}
}
int main()
{
Stuff sf[25];
myFunctionInsert(sf,5); // init 5 structures
return 0;
}
问题不仅在于缺少typedef
。
另外,由于(x=0;x<25;x++)的跨越了
struct Stuff *stuff[20];
另外stuff[x].name='stuff';
不会飞行。您的意思是“stuff”。请注意,C语言中没有String
类型
指定传递的结构数组大小的程序的工作版本可能如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct stuff
{
int id;
char name[20];
}Stuff;
// call another function
int myFunctionInsert(Stuff *stuff, int len);
int myFunctionInsert(Stuff *stuff,int len)
{
int x;
for(x = 0; x < len; x++){
stuff[x].id = x;
strcpy( stuff[x].name, "stuff");
printf("stuff %i id=%d name=%s\n", x, stuff[x].id, stuff[x].name );
}
}
int main()
{
Stuff sf[25];
myFunctionInsert(sf,5); // init 5 structures
return 0;
}