结构体的使用概述

#include <stdio.h>
#include <string.h> 
struct Student{//一共占用了208个字节 
	int sid;
	char name[200];
	int age; 
}; 
void f(struct Student);
void f1(struct Student*);
void g(struct Student);
void g1(struct Student*);
void main(){
	struct Student st={1,"zhangsan",24};
	printf("%d\n",st.sid);
	printf("%s\n",st.name); 
	printf("%d\n",st.age);
	strcpy(st.name,"lisi"); //java和c++可能内部也是这么实现的 
	printf("%s\n",st.name); 
 	struct Student *pst=&st;
	printf("%d\n",pst->age); //相当于(*pst).age 
	//访问结构体变量的两种方法
	//1.  st.sid
	//2.  st->sid
	
	printf("-------------------------------------------\n"); 
	//f(st); 
	struct Student st1;//在声明的时候就已经分配了内存
	f1(&st1); 
	g(st1);
	g1(&st1); 
	 
} 
//直接传递结构体变量进行赋值 ----这样子赋值是不会成功的,因为是直接传递值而非地址 
void f(struct Student st1){
	st1.sid=2;
	strcpy(st1.name,"lisi");
	st1.age=22; 
}
//直接传递结构体变量的地址也就是指针来进行赋值 
void f1(struct Student *pst1){
	pst1->sid=2;
	strcpy(pst1->name,"lisi");
	pst1->age=22; 
}
//直接传递结构体变量进行打印,耗内存,速度慢,因为在传递的过程中需要传递208个字节
void g(struct Student st1){
	printf("%d %s %d\n",st1.sid,st1.name,st1.age); 
}
//直接传递结构体变量的地址也就是指针来进行传递,因为指针只占用4个字节
void g1(struct Student *pst1){
	printf("%d %s %d\n",pst1->sid,pst1->name,pst1->age); 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值