3

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

2
  • You are aware, that you pass the value to modify() by value? Commented Sep 5, 2022 at 18:05
  • @BitTickler Given I'm modifying value being pointed at, it should not matter Commented Sep 5, 2022 at 18:13

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Rephrased the question a bit to reflect what I meant more clearly. Anyway this seems like the correct answer, will mark it when SO allows me to
2

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.