`

c语言—struct

 
阅读更多
#include <stdio.h>

//结构体,结构化数据,实际上就是把相关的数据放在一起作为一个独立的单元来使用
//定义结构体类型
struct horse {
	int age;
	int height;
	char name[40];
}; //注意分号

//未命名的结构体,
struct {
	int age;
	int height;
	char name[40];
} my_first_horse = { 10, 50, "first horse" };

#define COUNT 5;

int main() {
	printf("struct used in c:\n");
	//在c中使用struct结构体,
	//声明结构体变量
	struct horse little = { 12, 20, "little" }; //定义一个结构体变量,要有关键字struct
	//每一个结构体类型都作为一个新的类型,与基本类型一样,都可以使用指针来操作
	struct horse *phorse = &little; //结构指针
	printf("%s\n", (*phorse).name);
	printf("%s\n", phorse->name); //->成员指针运算符  通过指针直接访问变量的成员

	printf("horse name:%s\n", little.name); //使用点来访问"结构体成员"
	//	little.name = "littlehorse";
	printf("wrong horse age:%i\n", little.age);
	little.age = 20; //修改结构体成员值
	printf("true horse age:%i\n", little.age);
	printf("%s\n", my_first_horse.name);


	return 0;
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics