欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

结构体指针和数组理解

发布时间:2025/7/25 编程问答 60 豆豆
生活随笔 收集整理的这篇文章主要介绍了 结构体指针和数组理解 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

(1)第一个

1 # include<iostream> 2 using namespace std; 3 struct student 4 { 5 int num; 6 char name[12]; 7 char sex; 8 int age; 9 }//stu[3] = {{1,"miao",'M',19},{2,"chuang",'F',23},{3,"hello",'M',24}}; 10 *st; 11 struct student stu[3] = {{1,"miao",'M',19},{2,"chuang",'F',23},{3,"hello",'M',24}}; 12 int main() 13 { 14 //struct student *st; 15 16 for(st = stu; st < stu+3; st++) 17 { 18 cout<<(*st).num<<" "<<(*st).name<<" "<<(*st).sex<<" "<<(*st).age<<endl; 19 20 } 21 cout<<"------------------------------------------------"<<endl; 22 for(st = stu; st < stu+3; st++) 23 { 24 cout<<st->num<<" "<<st->name<<" "<<st->sex<<" "<<st->age<<endl; 25 } 26 27 } View Code

(2)第二个

1 # include<iostream> 2 using namespace std; 3 struct student 4 { 5 int num; 6 char name[12]; 7 char sex; 8 int age; 9 }//stu[3] = {{1,"miao",'M',19},{2,"chuang",'F',23},{3,"hello",'M',24}}; 10 ; 11 struct student stu[3] = {{1,"miao",'M',19},{2,"chuang",'F',23},{3,"hello",'M',24}}; 12 int main() 13 { 14 struct student *st; 15 16 for(st = stu; st < stu+3; st++) 17 { 18 cout<<(*st).num<<" "<<(*st).name<<" "<<(*st).sex<<" "<<(*st).age<<endl; 19 20 } 21 cout<<"------------------------------------------------"<<endl; 22 for(st = stu; st < stu+3; st++) 23 { 24 cout<<st->num<<" "<<st->name<<" "<<st->sex<<" "<<st->age<<endl; 25 } 26 27 } View Code

(3)第三个

1 # include<iostream> 2 using namespace std; 3 struct student 4 { 5 int num; 6 char name[12]; 7 char sex; 8 int age; 9 }stu[3] = {{1,"miao",'M',19},{2,"chuang",'F',23},{3,"hello",'M',24}}; 10 11 //struct student stu[3] = {{1,"miao",'M',19},{2,"chuang",'F',23},{3,"hello",'M',24}}; 12 int main() 13 { 14 struct student *st; 15 16 for(st = stu; st < stu+3; st++) 17 { 18 cout<<(*st).num<<" "<<(*st).name<<" "<<(*st).sex<<" "<<(*st).age<<endl; 19 20 } 21 cout<<"------------------------------------------------"<<endl; 22 for(st = stu; st < stu+3; st++) 23 { 24 cout<<st->num<<" "<<st->name<<" "<<st->sex<<" "<<st->age<<endl; 25 } 26 27 } View Code

(4)第四个(注意看typedef)

1 # include<iostream> 2 using namespace std; 3 typedef struct student 4 { 5 int num; 6 char name[12]; 7 char sex; 8 int age; 9 }//stu[3] = {{1,"miao",'M',19},{2,"chuang",'F',23},{3,"hello",'M',24}}; 10 stt; 11 stt stu[3] = {{1,"miao",'M',19},{2,"chuang",'F',23},{3,"hello",'M',24}}; 12 int main() 13 { 14 stt *st; 15 16 for(st = stu; st < stu+3; st++) 17 { 18 cout<<(*st).num<<" "<<(*st).name<<" "<<(*st).sex<<" "<<(*st).age<<endl; 19 20 } 21 cout<<"------------------------------------------------"<<endl; 22 for(st = stu; st < stu+3; st++) 23 { 24 cout<<st->num<<" "<<st->name<<" "<<st->sex<<" "<<st->age<<endl; 25 } 26 27 } View Code

 

转载于:https://www.cnblogs.com/sxmcACM/p/3463609.html

总结

以上是生活随笔为你收集整理的结构体指针和数组理解的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。