/*********************
*
指向结构体变量的指针作函数参数
*
***********************/
#include<stdio.h>
#include<string.h>
struct student
{
int num;
char name[20];
//char *name; //若定义为指针则应与下面 stu.name = "jacob"; 配对使用,交叉使用则会报错
//当定义为char name[20]时,这是一个存在于栈的具体空间,而"jacob"则是一个字符串常量,存在于内存的data空间。
float score[3];
};
void print(struct student *); //声明一个结构体指针
void main()
{
struct student stu;
stu.num=18;
strcpy(stu.name,"jacob"); //用函数strcpy,"jacob"复制进数组name内。
//stu.name = "jacob";
stu.score[0]=91;
stu.score[1]=95;
stu.score[2]=99;
print(&stu); // 结构体变量stu作为函数实参。
}
void print(struct student *p) //定义一个结构体指针变量p,接收的是结构体变量stu的地址即p指向结构体stu首地址
{
printf("num :%d\n",p->num); //此时引用结构体各成员需要用此格式 指针变量->结构体成员变量名。
printf("name :%s\n",p->name);
printf("score_1 :%f\n",p->score[0]);
printf("score_2 :%f\n",p->score[1]);
printf("score_3 :%f\n",p->score[2]);
}
转载于:https://my.oschina.net/u/2379244/blog/424129