结构体

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. struct student {
  5. //结构体的字节数因为对齐与理论值不一致
  6. int no;
  7. char name[20];
  8. char sex;
  9. }; // 结构体类型声明,注意最后一定要加分号
  10. int main() {
  11. /// 1003"mama"'m'
  12. /// 1008"Mike"'f'
  13. /// 1009"Math"'f'
  14. struct student s = { 1001, "lele", 'm'};
  15. //struct student sarr[3];
  16. int i;
  17. printf("%d %s %c", s.no, s.name, s.sex);
  18. //for (int i = 0; i < 3; i++)
  19. //{
  20. // scanf("%d%s%c", &sarr[i].no, &sarr[i].name, &sarr[i].sex);
  21. //}
  22. //for (int i = 0; i < 3; i++)
  23. //{
  24. // printf("%d%s%c", sarr[i].no, sarr[i].name, sarr[i].sex);
  25. //}
  26. return 0;
  27. }

结构体指针【指针偏移】

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. struct student {
  5. //结构体的字节数因为对齐与理论值不一致
  6. int no;
  7. char name[20];
  8. char sex;
  9. }; // 结构体类型声明,注意最后一定要加分号
  10. int main() {
  11. struct student s = {100, "Lizhilong", 'F'};
  12. struct student* p;
  13. struct student sarr[3] = { 101, "lili", 'F', 103, "huahua", 'm', 105, "Gogo", 'f' };
  14. p = &s;
  15. //注意运算符的优先级 结构体访问 . 大于 -> 大于 * 解引用运算符
  16. //printf("%d %s %c", (*p).no, (*p).name, (*p).sex);
  17. //printf("%d %s %c", (*p).no, (*p).name, (*p).sex);
  18. int no;
  19. p = sarr;
  20. printf("----------------------------\n");
  21. no = (p->no)++;
  22. printf("num=%d, p->num=%d\n", no, p->no);
  23. no = (p++)->no;
  24. //no = (p++)->no;
  25. printf("num=%d, p->num=%d\n", no, p->no);
  26. return 0;
  27. }