【发布时间】:2015-06-29 06:59:07
【问题描述】:
在下面的代码中,我无法单独更改 x 和 y 的值。有人可以帮我单独分配这些值吗?
#include <stdio.h>
struct p
{
int x;
int y;
};
int main()
{
int p2 = 55;
int p3 = 99;
//const struct p *ptr1 = {&p2,&p3}; --- giving the expected result
const struct p *ptr1;
ptr1->x = &p2; //error
ptr1->y = &p3; //error
printf("%d %d \n", ptr1->x, ptr1->y);
}
注意:我已经搜索过这样的示例,但我无法找到并且我的时间不多了。如果问题已经被问到,我真的很抱歉浪费您的时间,请提供链接供我参考。
【问题讨论】:
-
p2和p3的地址似乎被强制转换为整数值?什么是预期的结果? -
所以问题是,当您尝试取消引用
const时,unallocated用于编写编译器的指针不会执行从int *到int的隐式转换并静默编写。跨度>
标签: c pointers structure constants