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
modify()by value?