eg:
typedef struct _STU
{
char name[32];
char male;
char *favorrite;
int score;
}STUDENT;
不想直接给male赋值,而是想通过指针找到male这块的地址再赋值,怎么办呢,解决方案如下:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
typedef struct _STU
{
char name[32];
char male;
char *favorrite;
int score;
}STUDENT;
int main()
{
STUDENT student = { "xxj",'F',"math", 100 };
//student.male = 'M';
char *p = &student;
printf("%d\n", *((int *)(p+ offsetof(STUDENT,score))));
system("pause");
return EXIT_SUCCESS;
}
796

被折叠的 条评论
为什么被折叠?



