简体   繁体   中英

C - Modifying data of a pointer inside a const struct

So I have been trying to learn C over holidays. Right now I came across const , so I have been playing around with it. After a while I came across this

#include <stdio.h>

typedef struct StructA {
    int* ptr;
} struct_a;

void modify(const struct_a value)
{
    *value.ptr = 0;
}

int main()
{
    int x = 5;
    const struct_a y = { .ptr = &x };
    printf("%d\n", x);
    modify(y);
    printf("%d", x);
    return 0;
}

// Output:
// 5
// 0

My general notion is that while the structure is constant, the value being pointer at is not, therefore you can modify it. Can someone explain what exactly is going on here?

Edit: Minor rephrasing

Nothing in your program changes the value of a pointer. *value.ptr = 0; changes the value of a thing the pointer points to. Since value.ptr is inside a const structure, it is const , meaning you should not try to change it. But it is a pointer to something else. That something else is not const . By analogy, if I write my address in ink, so I cannot change the written address, that does not mean I cannot change anything inside my house. value.ptr just tells us where x is. We can change x (even using *value.ptr , which is x ) even though we cannot change value.ptr .

The function accepts its argument by value

void modify(const struct_a value)
{
    *value.ptr = 0;
}

You can imagine the function and its call the following way

const struct_a y = { .ptr = &x };
//...
modify(y);
//...

void modify( /*const struct_a value*/)
{
    const struct_a value = y;
    *value.ptr = 0;
}

It means that the data member ptr of the object value is constant.

That is the structure within the function has in fact the following definition

typedef struct StructA {
    int * const ptr;
} struct_a;

So you may not change the data member ptr itself (the value stored in the data member ptr). But you may change the object pointed to by the data member ptr

*value.ptr = 0;

As the pointer ptr in the function parameter and in the argument point to the same object you can change the object pointed to by the pointer of the structure declared in main

Consider these declarations

const int *ptr = &x;

It means that the object pointed to by the pointer ptr is considered as a constant.

In thsi declaration

int * const ptr = &x;

It is the pointer iyself that is a constant.

And at last in this declaration

const int * const ptr = &x;

the both the pointer itself and the object pointed to by the pointer are constants.

From my understanding, the memory block the pointer is pointing to does not change. However, the value in the memory block being pointed to changes.

So if you were to try to update the pointer to point to a different address you would not be able to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM