指向结构的指针 struct结构名称 *结构指针变量名
//指向结构的指针 struct结构名称 *结构指针变量名
//(*结构指针变量名).成员变量名
//结构指针变量->成员变量名
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 6 struct Martial //门派 7 { 8 int id; //门派 id 9 char name[50]; //门派m名称 10 int count; //门派人数 11 char type[50]; //门派的类型 正派 中立 邪派 12 13 14 }; 15 16 struct Player 17 { 18 int id; 19 char name[50]; //玩家名称 20 char pass[50]; //玩家的登录密码 21 char sex[10]; //玩家性别 男 M 女 X 22 23 struct Martial martial; //玩家的门派 24 25 26 }; 27 28 int main() 29 { 30 31 struct Player player = {1,"UZI","123456","M",{1,"RNG",500,"ADC"}}; 32 33 34 struct Player player2 = {2,"Theshy","123456","M",{2,"IG",500,"TOP"}}; 35 //struct结构名称 *结构指针变量名 36 struct Player *ptr_player2 = &player2; 37 38 39 40 struct Player player3 = {3,"Clearlove7","123456","M",{3,"EDG",500,"Jungle"}}; 41 struct Player *ptr_player3= &player3; 42 43 44 45 struct Player player4 = {4,"Faker","123456","M",{4,"STK",500,"Mid"}}; 46 47 struct Player *ptr_player4= &player4; 48 49 50 printf("%d\t %s\t\t %s\t %s\t %d\t %s\t %d\t %s\n",player.id,player.name,player.pass,player.sex,player.martial.id,player.martial.name,player.martial.count,player.martial.type); 51 52 printf("%d\t %s\t\t %s\t %s\t %d\t %s\t %d\t %s\n",player2.id,player2.name,player2.pass,player2.sex,player2.martial.id,player2.martial.name,player2.martial.count,player2.martial.type); 53 //使用指针来访问 54 55 //(*结构指针变量名).成员变量名 56 57 printf("%d\t %s\t %s\t %s\t %d\t %s\t %d\t %s\n",(*ptr_player3).id,(*ptr_player3).name,(*ptr_player3).pass,(*ptr_player3).sex,(*ptr_player3).martial.id,(*ptr_player3).martial.name,(*ptr_player3).martial.count,(*ptr_player3).martial.type); 58 //结构指针变量->成员变量名 59 printf("%d\t %s\t\t %s\t %s\t %d\t %s\t %d\t %s\n",ptr_player4->id,ptr_player4->name,ptr_player4->pass,ptr_player4->sex,ptr_player4->martial.id,ptr_player4->martial.name,ptr_player4->martial.count,ptr_player4->martial.type); 60 61 62 63 }
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15072650.html