C语言--结构体

#include <stdio.h>
#include <string.h>
//无名结构体:没有类型名
struct { 					
	int id; 			
	float score;
	char name[32];
}s1 = {1, 91.38, "里斯"};	
							


int main(int argc, const char *argv[])
{

	printf("id=%d score=%.1f name=%s\n",s1.id, s1.score, s1.name);

	s1.id = 0;
	s1.score = 79.23;
	strcpy(s1.name,"张三");
	printf("id=%d score=%.1f name=%s\n",s1.id, s1.score, s1.name);
	return 0;
}


#include <stdio.h>
#include <string.h>
//结构体嵌套
 
struct stu{ 					
	int id; 			
	char name[32];
	struct { 				//结构体嵌套,内部一般使用无名结构体
		int month;
		int year;
	}d;
}s;
							
int main(int argc, const char *argv[])
{
	s.id = 3;
	strcpy(s.name, "王五");
	s.d.month = 10;
	s.d.year = 2016;


	printf("id   = %d\n",s.id);
	printf("name = %s\n",s.name);
	printf("mont = %d\n",s.d.month);
	printf("year = %d\n",s.d.year);
	return 0;
}



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

struct stu{ 					
	int id; 			
	char name[32];
	struct day{ 			
		int month;
		int year;
	}d;
}s = {4, "赵", {5,2014}}; 	
							
int main(int argc, const char *argv[])
{
	printf("id   = %d\n",s.id);
	printf("name = %s\n",s.name);
	printf("mont = %d\n",s.d.month);
	printf("year = %d\n",s.d.year);

	s.id = 3;
	strcpy(s.name, "王五");
	s.d.month = 10;
	s.d.year = 2016;

	puts("***************");

	printf("id   = %d\n",s.id);
	printf("name = %s\n",s.name);
	printf("mont = %d\n",s.d.month);
	printf("year = %d\n",s.d.year);

	struct day d1 = {12,2015};  //struct day 可以单独使用
	printf("%d\n",d1.month);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值