结构体指针型函数的声明

来源:百度知道 编辑:UC知道 时间:2024/05/09 20:32:05
main函数调用一个返回值为结构体指针型的函数的声明

#define LIST struct func
struct funs
{char name[15];
char func[30];
struct funs *next;
}
#LIST *creat()
main()
{LIST *head;
head=creat();
}
LIST *creat()
{}

编译时错误提示
fatal error C1021: invalid preprocessor command 'LIST'

不明白你的意思,你想问的问题是:
(1)如何声明一个返回结构体指针的函数?
(2)如何声明一个带有结构体指针参数的函数?

如果是(1),则看如下代码:
struct A{
A():m1(0),m2(0){}
int m1;
int m2;
}

A* Func(void){
static A a; // 静态局部变量
return &a; // 返回其指针地址
}

如果是(2)则:
void Func(A *pA){
pA->m1 = 1;
pA->m2 = 2;
}
// 测试代码
A a; //a.m1 = 0, a.m2 = 0
Func(&a); //Func函数对a中的成员进行了修改 a.m1 = 1, a.m2=2

你确实没怎么说清楚,看看这个吧:
struct MyTest
{
int i;
int j[10];
char b;
}exam;
声明可以是:
void fuction(struct& )
struct fuction()

调用的时候:
Mytest *p = exam;
fuction(*p)

typedef struct funs
{char name[15];
char func[30];
struct funs *next;
}LIST ;

LIST *creat() ;//
main()
{LIST *head;
head=creat();
}
LIST *creat()
{}

声明结构体的时候一般用typedef