结构体 -转
【C&C++】结构体作用笔记
一. 结构作为函数的参数
用结构作为参数有四种情形:
1. 直接用它本身
2. 用结构成员
3. 用结构地址
4. 用数组
1.结构作为参数
struct book
{
float cost;
int series;
};
float fcost(struct book item1,struct book item2)
{
if (item1.series!=item2.series)
return(item1.cost+item2.cost);
else return(item1.cost);
}
#include "stdio.h"
main()
{
struct book book1,book2;
float fcost(struct book,struct book);
printf("Please input series and cost of book1 and book2.\n");
scanf("%d %f %d %f",&book1.series, &book1.cost,&book2.series, &book2.cost);
printf("total cost is %.2f\n", fcost(book1,book2));
}
2.用结构成员作为参数
如例:salary 和 subsidy 是结构clerk 的成员。它们都是简单类型 --float类型。
//它们当然可以用作函数的参数。
struct payment {
char *name;
float salary;
float subsidy;
}clerk={ "Karlis",150.20,30.0};
#include "stdio.h"
main()
{
float total,sum(float,float);
extern struct payment clerk;
printf("clerk %s has a total wage of $%.2f\n",clerk.name,
sum(clerk.salary,clerk.subsidy));
}
float sum(float x,float y)
{
return(x+y);
}
3.用结构地址
结构的地址是一个单个的数。用它作为函数参数是非常好的。&运算符是用来得到结构的地址。如例:把地址 &clerk 传给函数 sum(), 使得 wage 指针指向结构 clerk。
struct payment {
char *name;
float salary;
float subsidy;
}clerk={"Karlis",150.20,30.0};
#include "stdio.h"
main()
{
float sum(struct payment *);
//extern struct payment clerk;
printf("clerk %s has a total wage $%.2f.\n",clerk.name,sum(&clerk));
}
float sum(struct payment *wage)
{
return((wage->salary) + (wage->subsidy));
}
4.用数组
作为结构数组,数组名是指向数组的指针。也就是说, 它是地址的代名词。因此它可以传递给函数。
struct payment
{
char *name;
float salary;
float subsidy;
} clerk[2]={{"Karlis",150.2,20},{"Fred",146.5,20}};
#include "stdio.h"
main()
{
float sum(struct payment *);
printf("The total money is $%.2f\n",sum(clerk));
}
float sum(struct payment *wage)
//struct payment *wage;
{
float total;
int i;
for(i=0,total=0;i<2;i++,wage++)
total+=wage->salary+wage->subsidy;
return(total);
}
二.结构作为函数的返回值
1.直接用结构作为函数的返回值
//Filename: Struct.c
#include<stdio.h>
//定义一个结构体
typedef struct Point
{
int x;
int y;
}Point;//使用一个结构体变量作为函数的参数
void Display(Point point)
{
printf("x is %d\n",point.x);
printf("y is %d\n",point.y);
//------若使用C++,则如下-------------
//std::cout<<"x is "<<point.x<<std::endl;
//std::cout<<"y is "<<point.y<<std::endl;
}//使用结构体变量作为函数的返回值
Point SetPoint(int x,int y)
{
Point point;
point.x=x;
point.y=y;
return point;
}//主函数
int main(int atgc,char * argv[])
{
Point point;
point=SetPoint(2,3);
Display(point);
return 0;
}
2.用结构地址结构作为函数的返回值
这种方法也可通过用指向结构的指针来实现。
格式是:
struct 类型名1 *函数名 ( 类型名2 参数 )
{
...
return( 指向类型 1 的指针 );
}
#include<stdio.h>
#include<string.h>
struct student
{
char*name;
float score[3];
}stu[5]={"Li",{90,87,83},"Ma",{98,85,78},"Wang",{96.89,87},"Huang",{80,82,72},"Zhang",{88,75,68}};
main()
{
struct student *s1,*find(struct student s[]);
s1=find(stu);
printf("%s:%.2f,%.2f,%.2f\n",s1->name,s1->score[0],s1->score[1],s1->score[2]);
}
struct student *find( struct student s[])
{
int i;
char namel[20];
printf("Input student's name:");
scanf("%s",namel);
for(i=0;i<5;i++)
if(strcmp(namel,s[i].name)==0)
return(s+i);
}
注:以上程序样例代码主要来自《经典C语言教程》和网上博客,如涉及侵权,请见谅。
浙公网安备 33010602011771号