[英]How to assign values to a const structure pointer's variable?
In the below piece of code, I am not able to change the values of x and y individually. 在下面的代码中,我无法单独更改x和y的值。 Can some one help me with assigning these values individually?
有人可以帮我分别分配这些值吗?
#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);
}
Note: I have searched for such an example, I could not able to find and I am running out of time. 注意:我已经搜索了这样的示例,但找不到,并且时间已用完。 If the question is already asked, I am really very sorry to waste your time and please provide me the link for the same to refer.
如果已经问过问题,非常抱歉浪费您的时间,请向我提供链接以供参考。
There are two important issues to consider: 有两个重要的问题需要考虑:
const struct p*
is a "pointer to const p
", which means you cannot modify the instance it points to. const struct p*
是“指向const p
指针”,这意味着您无法修改其指向的实例。 It can point to a non- const
object, but you can't use the pointer to modify said object. 它可以指向非
const
对象,但是您不能使用指针来修改该对象。
A pointer must point to a valid object before it can be de-referenced. 指针必须指向有效对象,然后才能取消引用。
You need to create a valid p
instance, then make the pointer point to it: 您需要创建一个有效的
p
实例,然后使指针指向它:
struct p x = {p2, p3};
const struct p *ptr1 = &x;
In this example, a p
instance is created in automatic storage. 在此示例中,在自动存储中创建了一个
p
实例。 You can also instantiate one dynamically using malloc
if that suits your needs better. 如果更适合您的需求,您还可以使用
malloc
动态实例化一个实例。 For example, 例如,
struct p *px = malloc(sizeof (struct p));
px->x = p2;
px->y = p3;
const struct p *ptr1 = px;
In both examples, you can modify the instance ptr1
points to via x
and px
respectively, but not via ptr1
. 在两个示例中,您都可以分别通过
x
和px
修改实例ptr1
指向的点,但不能通过ptr1
修改实例。
const struct p *ptr1 = {&p2,&p3}; // --- giving the expected result
It compiles, but with this warning; 它会编译,但带有此警告; and either way, it probably doesn't do what you want anyway:
无论哪种方式,它可能都无法满足您的需求:
warning: incompatible pointer types initializing 'const struct p *' with an expression of type 'int *' [-Wincompatible-pointer-types]
警告:不兼容的指针类型使用类型为'int *'的表达式初始化'const struct p *'[-Wincompatible-pointer-types]
To create a constant pointer to struct, you could use this: 要创建指向struct的常量指针,可以使用以下命令:
const struct p *ptr1 = &(struct p){p2, p3};
Quick note about lifetime : 关于寿命的简要说明:
If the compound literal occurs outside the body of a function, the object has static storage duration;
如果复合文字出现在函数主体之外,则对象具有静态存储持续时间; otherwise, it has automatic storage duration associated with the enclosing block.
否则,它具有与封闭块关联的自动存储时间。
Since the author seemed to want to change a struct that she was declaring const, this might in fact be a very relevant answer. 由于作者似乎想更改她声明为const的结构,因此实际上这可能是一个非常相关的答案。
A common gotcha directly related to this question is that 与这个问题直接相关的常见陷阱是
const struct p *ptr1
is a pointer to a "const struct p", which means the pointer variable ptr1 can change and point to a different struct p later, but regardless of where it points, you can't write to members of the struct using that pointer (eg ptr1->x = blah;
). 是指向“ const struct p”的指针,这意味着指针变量ptr1可以在以后更改并指向另一个struct p,但是无论指针指向何处,您都无法使用该指针写入该结构的成员(例如
ptr1->x = blah;
)。
What some might be looking for is a pointer which is const, and thus can't ever point to a different piece of memory, after being assigned one at its initialization. 有些人可能正在寻找的是一个const指针,因此在初始化时被分配一个指针,因此永远无法指向不同的内存。
That would be 那会是
struct p * const ptr2 = ptr1 // whatever ptr1 currently points to, ptr2 will point to there, from now to forever (for the lifetime / scope of ptr2).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.