#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
struct Person
{
char a; //0~3
int b; //4~7
char buf[64]; //8~71
int d; //72~75
};
void test() {
struct Person p = { 'a',10,"hello world",20 };
printf("%c\n", *((char*)&p + 0));
printf("%d\n", *((char*)&p + 4));
printf("%s\n", ((char*)&p + 8));
printf("%d\n", *((char*)&p + 72));
printf("%c\n", *((char*)&p+offsetof(struct Person, a)));
printf("%d\n", *((char*)&p+offsetof(struct Person, b)));
printf("%s\n", (char*)&p+offsetof(struct Person, buf));
printf("%d\n", *((char*)&p+offsetof(struct Person, d)));
}
int main(int argc, char* argv[])
{
test();
system("pause");
return EXIT_SUCCESS;
}
C语言-指针步长
于 2024-08-07 17:35:09 首次发布