/*
2009年8月26日14:18:02
如何使用结构体
两种方式:
struct Student st = {1000, "zhangsan", 20};
struct Student * pst = &st;

1.
st.sid
2.
pst->sid
pst所指向的结构体变量中的sid这个成员

*/

# include <stdio.h>
# include <string.h>

struct Student
{
int sid;
char name[200];
int age;
}; //分号不能省

int main(void)
{
struct Student st = {1000, "zhangsan", 20};
//st.sid = 99; //第一种方式

struct Student * pst;
pst = &st;
pst->sid = 99; //第二种方式 pst->sid 等价于 (*pst).sid 而(*pst).sid等价于 st.sid, 所以pst->sid 等价于 st.sid

return 0;
}

结构体2

# include <stdio.h>
# include <string.h>

struct Student
{
int sid;
char name[200];
int age;
}; //分号不能省

//前置声明
void f(struct Student * pst);
void g(struct Student st);
void g2(struct Student *pst); //只传其结构体的地址,所以接收的数据,也只能是地址


int main(void)
{
struct Student st; //已经为st分配好了内存,因为没内存,怎么在f中写他的地址呢

f(&st);
g2(&st);

//printf("%d %s %d\n", st.sid, st.name, st.age);

return 0;
}

//这种方式耗内存 耗时间 不推荐
void g(struct Student st) //这样传,至少是208个字节,200+4+4
{
printf("%d %s %d\n", st.sid, st.name, st.age);
}

void g2(struct Student *pst) //这样传,只传了4个字节
{
printf("%d %s %d\n", pst->sid, pst->name, pst->age);
}

void f(struct Student * pst)
{
(*pst).sid = 99;
strcpy(pst->name, "zhangsan");
pst->age = 22;
}




//使用结构体,有两种方式,
//1,通过结构体变量的名字来使用
//2,通过一个指向了某一个结构体变量的指针来实现

//第二种更加方便,第一种必须要有其名字
//不能定义10个变量,再定义10个名字

//结构体变量之间,通常只传递他们的地址,不发送整体变量



int i;
f(i);
f(&i);