-2

I am learning C and discovered that wecan change the value of a constant variables by using pointers. I tried to do so using the following code:

Int main (void)
{ const int i = 10;
   int *ptr;
   *ptr = &i;
   printf("value before : %d",i);
   *ptr = 50;
   printf("value after : %d",i);
   return 0;
 } 

Output
 Value before : 10
  Value after : 10 

The value didn't change

But when i did this

Int main (void)
{ const int i = 10;
   int *ptr = &i; //notice the change here

   printf("value before : %d",i);
   *ptr = 50;
   printf("value after : %d",i);
   return 0;
 } 

Output
 Value before : 10
  Value after : 50 

How are the two codes producing different results?

Techievent.in
  • 75
  • 1
  • 9
  • 3
    Both pieces of code are undefined, but the first is somewhat "more" undefined than the last In `int *ptr; *ptr = &i;` `ptr` you're assigning the address to the target integer through the uninitialized address stored in `ptr`. – Petr Skocik Sep 09 '17 at 19:43
  • 2
    Possible duplicate of [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) – rustyx Sep 09 '17 at 19:44
  • 1
    Possible duplicate of [Can we change the value of an object defined with const through pointers?](https://stackoverflow.com/questions/3801557/can-we-change-the-value-of-an-object-defined-with-const-through-pointers) – Bo Persson Sep 10 '17 at 02:06

5 Answers5

4
const int i = 10;
int *ptr;
*ptr = &i; // this line is wrong

You've made a mistake here that doesn't have anything to do with const. When you declare a pointer you write TYPE *ptr, but the star is not part of the name of the pointer. When you write *ptr = EXPRESSION;, that means to store the value of EXPRESSION in the location that ptr points to. But you didn't set ptr to point to anything in particular yet, so the program will malfunction.

To set the location, which is what you're trying to do, you must instead write ptr = EXPRESSION with no star:

int *ptr;
ptr = &i; // corrected

In the second test program, you had instead

int *ptr = &i;

which declares the pointer and sets its location in one step. It is shorthand for the "corrected" code above.

This is Just One Of Those Things You Have To Memorize when you are learning C.


Independent of all that, when you have

const int i = 10;

you can write code that looks like it modifies the value of i using a non-const pointer, but that code -- however it is structured, however the pointer comes to point to i -- is incorrect. A "better" programming language would refuse to compile that code. C implementations, almost entirely for historical reasons, will usually compile that code with maybe a warning if you're lucky, but the program you get is said to have "undefined behavior" -- it might do exactly what it looks like it does, it might behave as-if you had never modified the constant variable at all, it might crash, it might make demons fly out of your nose, none of these are considered to be wrong.

("Dereferencing" a pointer that hasn't been set to point to anything in particular also produces a program with undefined behavior.)

zwol
  • 135,547
  • 38
  • 252
  • 361
0

When u make an "pointer = adress" when creating pointer u are saying the adress are the same. In ur first program u are dereferencing pointer and set the value equal to i adress, what make no sense for u study case.

int main (void)
{ 
    const int i = 10;
    int *ptr;

    //*ptr = &i; <<< VALUE of pointer PTR is EQUAL to i ADRESS
    //ptr = &i; <<< ADRESS of pointer PTR is EQUAL to i ADRESS

    printf("value before : %d\n",i);
    *ptr = 50;
    printf("value after : %d",i);

    return 0; 
} 
Nefisto
  • 517
  • 3
  • 9
0

In your first piece of code the address of the constant variable is never assigned to the pointer so its value never changes Your code should be as

 int *ptr;
 ptr = &i;

for the code to work

Meow
  • 140
  • 2
  • 1
    The behaviour of both pieces of code is undefined, and your answer neither corrects nor explains that. When two bits of different code have undefined behaviour, it doesn't matter why they give the same or different results. Because the behaviour can change with all sorts of things, including compiler options or phase of the moon. – Peter Sep 09 '17 at 22:34
0

your code "works" only because the auto variable is usually (on the implementation level) RW. But you have got some warnings (unless you have not switched them off as too annoying ), or (most probable) you have ignored them (compiler was trying to say: dude, I thought that you know what you are doing - but compiling your code I stopped to be so sure). But if you make your variable global, it very likely will be placed in the RO area and it is very probable to have a SEGFAULT.

const const_i = 5;

int foo(int f)
{
    const int a = 3;
    int *ptr = &a;

    *ptr = f;

    return a;
}



int main(int argc, char **argv)
{
    int *ptr = &const_i;

    printf("%d\n", foo(255));

    *ptr = 10;

    printf("%d\n", const_i);


}
0___________
  • 60,014
  • 4
  • 34
  • 74
0
First Part:-
Int main (void)
{ const int i = 10;
   int *ptr;
   *ptr = &i;
   printf("value before : %d",i);
   *ptr = 50;
   printf("value after : %d",i);
   return 0;
 }
Consider i address as 100 where value 10 is store.
Consider p address as 200 which will store the address of integer variable.
And value will be acess by *.
/* Meaning of below statement*/
*ptr = &i;
This means you are trying to store some value in this case 100(since address of i is 100) to address which is pointed by ptr which is currently invalid.
if you will try to acess *ptr this itself is invalid.(it will give u invalid *ptr = 50;read/write with valgrind).
Since there is no change in value store in i address.So it will print proper value.
------------------------------------
second case:-
int *ptr = &i; //notice the change here
Here you are storing address of i in ptr ,suppose address of i is 100.ptr will also store 100.so if you do any change it will be reflected .
------------------------------------
In place of *ptr=&i;
if you write ptr =&i;
This will reflect the value now you are storing address of i in p.

-----------------------------------------
Rohit
  • 142
  • 8