【发布时间】:2020-12-10 08:31:48
【问题描述】:
我想知道,如何通过指针访问结构的第一个成员。我试过这个:
#include <stdio.h>
#include <stdlib.h>
struct foo
{
int a;
char *str;
};
int main()
{
struct foo *p = malloc(sizeof(struct foo));
int val = 10;
*(int**)p = &val; //set the value of the first member of struct foo
printf("%i\n",p->a);
}
但是打印一些垃圾。我怎样才能以类似的方式设置它?
【问题讨论】:
-
我更好奇为什么你想做这样的事情?您需要解决的实际问题是什么?如果只是单纯的好奇也没关系,但请在问题正文中说明。
标签: c pointers struct double-pointer