结构体输入

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. struct student
  5. {
  6. int num;
  7. int name[20];
  8. int age;
  9. char sex;
  10. float score;
  11. };
  12. int main()
  13. {
  14. struct student stu[2];//创建一个结构体数组
  15. for(int i = 0; i < 2; i++)//与数组操作一致
  16. {
  17. cin >> stu[i].num >> stu[i].name >> stu[i].age >> stu[i].sex >> stu[i].score;
  18. }
  19. for(int i = 0; i < 2; i++)
  20. {
  21. cout << stu[i].num << ""
  22. << stu[i].name << " "
  23. << stu[i].age << " "
  24. << stu[i].sex << " "
  25. << stu[i].score << endl;
  26. }
  27. return 0;
  28. }

结构体调用自己

  1. struct node
  2. {
  3. int a;
  4. //struct node n;//无法调用因为还没定义结构体,还不知道结构体n的空间大小
  5. struct node* n;//用指针指向一个地址,只需要知道存在就行
  6. };