• C


    结构的定义、初始化、访问和结构数组

    #include <stdio.h>
    #include <string.h>
    
    #define MAXTITL 41
    #define MAXAUTL 31
    #define MAXBOOK 10
    // 结构定义
    struct book {
    	char title[MAXTITL];
    	char author[MAXAUTL];
    	float value;
    };
    
    int main(int argc, char *argv[])
    {
    	struct book crusoe; // 声明book类型变量
    	strcpy(crusoe.title, "鲁滨逊漂流记");
    	strcpy(crusoe.author, "迪福");
    	crusoe.value = 66.66;
    	printf("book crusoe,title: %s ,author: %s , value:%f 
    ", crusoe.title, crusoe.author, crusoe.value);
    	// 初始化结构
    	struct book stone= {
    		"红楼梦",
    		"雪里红芹菜",
    		100.00
    	};
    	printf("book stone,title: %s ,author: %s , value:%f 
    ", stone.title, stone.author, stone.value);
    	// 指定初始化项目
    	struct book piao = {
    		.author = "斯嘉丽",
    		.title = "乱世佳人",
    		.value = 22.22
    	};
    	printf("book piao,title: %s ,author: %s , value:%f 
    ", piao.title, piao.author, piao.value);
    	// 结构数组
    	struct book library[MAXBOOK];
    	library[0] = crusoe;
    	library[2] = stone;
    	library[3] = piao;
    	printf("sizeof struct book is %d , sizeof library is %d 
    ", sizeof(stone), sizeof(library));
    	return 0;
    }
    
    
    

    结构嵌套

    #include <stdio.h>
    
    #define MAXTITL 41
    // 结构定义
    struct Author {
    	char name[30];
    	int age;
    };
    
    struct book {
    	char title[MAXTITL];
    	struct Author author;
    	float value;
    };
    
    int main(int argc, char *argv[])
    {
    	struct Author author = {
    		"曹雪芹",
    		66
    	};
    	// 初始化结构
    	struct book stone = {
    		"红楼梦",
    		{"曹雪芹",66	}, // author
    		100.00
    	};
    	printf("book stone,title: %s ,author: %s,author'age: %d, value:%f 
    ", 
    		stone.title, stone.author.name, stone.author.age, stone.value);
    
    	return 0;
    }
    
    

    结构与指针

    #include <stdio.h>
    
    #define MAXTITL 41
    // 结构定义
    struct Author {
    	char name[30];
    	int age;
    };
    
    struct book {
    	char title[MAXTITL];
    	struct Author author;
    	float value;
    };
    
    int main(int argc, char *argv[])
    {
    	// 初始化结构数组
    	struct book library[2] = {
    		{
    			"红楼梦",
    			{ "曹雪芹",66 },
    			100.00
    		} ,
    		{
    			"西游记",
    			{ "吴承恩",88 },
    			101.00
    	} };
    	struct book *ptr = &library[0]; // 指向结构的指针
    
    	printf("book stone,title: %s ,author: %s,author'age: %d, value:%f 
    ", 
    		ptr->title, ptr->author.name, ptr->author.age, ptr->value);
    	ptr++;	// 指向下一个
    	printf("book stone,title: %s ,author: %s,author'age: %d, value:%f 
    ", 
    		ptr->title, ptr->author.name, ptr->author.age, ptr->value);
    
    	return 0;
    }
    
    

    结构与函数

    #include <stdio.h>
    
    struct swaper {
    	int a;
    	int b;
    };
    void swap(struct swaper *); // 参数为结构指针
    void to_string(struct swaper pair); // 参数为结构
    
     // 使用结构交换
    int main(int argc, char *argv[])
    {
    	struct swaper pair = {
    		1,2
    	};
    	to_string(pair);
    	swap(&pair);
    	to_string(pair);
    	return 0;
    }
    
    void swap(struct swaper * pair)
    {
    	int temp = pair->a;
    	pair->a = pair->b;
    	pair->b = temp;
    }
    
    void to_string(struct swaper pair)
    {
    	printf("swaper{a:%d,b:%d} 
    ", pair.a, pair.b);
    }
    
    
    
    
  • 相关阅读:
    【转载】Linux下各文件夹的含义和用途
    【转载】Linux 通过mount -o loop 配置本地.iso镜像为yum源(yum仓库)
    Fedora 和 RedHat 以及 SUSE 中 YUM 工具的使用
    【转】下载对应内核版本的asmlib
    【转】VMWare vCenter 6.0安装配置
    【转】在VMware中为Linux系统安装VM-Tools的详解教程
    【转】虚拟化(五):vsphere高可用群集与容错
    html拼接时onclick事件传递json对象
    bootstrap table 解析写死的json.并且把进度条放进列中。
    开发规范实体和值对象
  • 原文地址:https://www.cnblogs.com/hiqianqian/p/6848090.html
Copyright © 2020-2023  润新知