指向结构体的指针作函数参数
来源:百度知道 编辑:UC知道 时间:2024/06/07 07:26:26
神啊,请你派你的使者来救救我吧
参数传递的时候要使用
reference-to-pointer的方式,用引用来传递参数
小例子:
struct MyTest
{
int i;
int j[10];
char b;
}exam;
Mytest *p = exam;
void fuction(struct& )
调用的时候:
fuction(*p)
struct YourStruct
{
...
};
void YourFunction(YourStruct* pstr);
main()
{
YourStruct* pstr;
YourFunction(pstr);
}
很简单.
首先,我们定义一个指针是 类型 * 变量名,比如int *pInt = NULL;
我们定义结构体指针也是一样的, 结构体名 *pStruct(C语言:struct 结构体名 *pStruct, C++:结构体名 *pStruct, struct一般可以省略),
比如:
#include <stdio.h>
#include <string.h>
struct stStudent
{
char szName[10];
int nID;
};
void Print(stStudent *pStd)
{
printf("Name:%s\n", pStd->szName);
printf("ID:%d\n", pStd->nID);
}
void InitStudent(stStudent *pStd)
{
strcpy(pStd->szName, "CIW");