我对将结构指针去结构化为 结构变量有点混淆。解引用指向结构变量的结构指针
如果我用一个例子证明我的问题,那将会很好。
所以我在这里:
struct my_struct{
int num1;
int num2;
}tmp_struct;
void Display_struct(void * dest_var){
struct my_struct struct_ptr;
struct_ptr = *((struct my_struct *)dest_var);
printf("%d\t%d\n",struct_ptr.num1,struct_ptr.num2);
}
int main()
{
tmp_struct.num1 = 100;
tmp_struct.num2 = 150;
Display_struct(&tmp_struct);
return 0;
}
现在,当我运行这个例子我能得到的代码在一个非常干净的方式进行编译,并输出是正确的。
但是我不能够得到是这个结构指针引用到结构变量的正确的方式,因为我们在其他简单 数据类型的情况下,这样做:
int example_num;
void Display_struct(void * dest_var){
int example_num_ptr;
example_num_ptr = *((int *)dest_var);
printf("%d\t%d\n",struct_ptr.num1,struct_ptr.num2);
}
int main()
{
example_num = 100;
Display_struct(&example_num);
return 0;
}
这里我们可以将int指针取消引用到int变量,因为它是一个简单的数据类型,但在我看来,我们不能像结构变量那样以类似的方式取消引用结构指针,因为它不是简单的数据类型,而是复杂的数据类型或数据结构。
请帮我解决这个背后的概念。
为什么你不能取消引用指针结构?在一个指针中,你不能访问底层对象的地方是什么? –
你的意思是这样的:https://ideone.com/lduzh0? – mch
这里的实际问题是什么?顺便说一句第二个例子是错误的,你尝试printf'struct_ptr.num1',但在第二个例子中没有'struct_ptr'。 –