大佬们,怎么用循环遍历结构体成员呀?成员类型不一致
大佬们,怎么用循环遍历结构体成员呀?成员类型不一致成员太多,用 . 一个一个访问有点麻烦。用指针和循环可以实现吗?
#define _CRT_SECURE_NO_DEPRECATE #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { struct Mix { int a; char b[20]; int c; float d; char e[10]; }; Mix*mx1 = (Mix*)malloc(sizeof(Mix)); (*mx1).a = 2021; (*mx1).c = 104; (*mx1).d = 22.09; strcpy((*mx1).b, "congratul\0"); strcpy((*mx1).e, "lations\0"); int loop_a = 0; void*_tmp_p1 = (void*)mx1; int*_tmp_d = NULL; float*_tmp_f = NULL; char*_tmp_s = NULL; for (; loop_a <= 4; loop_a++) { switch (loop_a) { case 0: { _tmp_d = (int*)_tmp_p1; printf("%d %d\n", loop_a, *_tmp_d); _tmp_d++; _tmp_p1 = (void*)_tmp_d; } continue; case 1: { _tmp_s = (char*)_tmp_p1; printf("%d %s\n", loop_a, _tmp_s); _tmp_s+=20; _tmp_p1 = (void*)_tmp_s; } continue; case 2: { _tmp_d = (int*)_tmp_p1; printf("%d %d\n", loop_a, *_tmp_d); _tmp_d++; _tmp_p1 = (void*)_tmp_d; } continue; case 3: { _tmp_f = (float*)_tmp_p1; printf("%d %f\n", loop_a, *_tmp_f); _tmp_f++; _tmp_p1 = (void*)_tmp_f; } continue; case 4: { _tmp_s = (char*)_tmp_p1; printf("%d %s\n", loop_a, _tmp_s); } continue; } system("pause"); return 0; } }