2

The output of the following program is 50 on gcc. How is it possible as x is constant variable and *p is x itself as p is a constant pointer pointing to value at x. Where as turbo c gives compiler error. Is it an undefined behaviour? please explain.

#include<stdio.h>

int main()
{
    const int x = 25;
    int * const p = &x;
    *p = 2 * x;
    printf("%d", x);
    return 0;
}
dhein
  • 6,431
  • 4
  • 42
  • 74
Krishna Kittu
  • 106
  • 1
  • 12
  • 2
    If you do so, its Undefined behaviour. – Grijesh Chauhan Aug 24 '13 at 08:57
  • 1
    ur compiler shouldn't allow that, not allowed to convert from `const int*` to `int* const`: http://ideone.com/AnuWLM – Kal Aug 24 '13 at 08:57
  • 2
    Removed C++ tag: this will not compile as C++. A C compiler will probably emit a scary warning. – Mat Aug 24 '13 at 08:58
  • Compiler could allow it because it can't know that in any xase – dhein Aug 24 '13 at 08:58
  • 5
    ***int*** `main()`... –  Aug 24 '13 at 08:58
  • "Is it an undefined behaviour?" - Sure, because it's a constraint violation. –  Aug 24 '13 at 08:59
  • possible duplicate of [constants and pointers in C](http://stackoverflow.com/questions/945640/constants-and-pointers-in-c) –  Aug 24 '13 at 08:59
  • i executed on compileonline.com and got 50 as ouput which is a gcc compiler – Krishna Kittu Aug 24 '13 at 08:59
  • 1
    @Krishna did gcc not worn about the bad pointer assingment `p=&x`? – Adrian Ratnapala Aug 24 '13 at 09:01
  • I believe @H2CO3 has a trigger to fire every time someone posts code with `main` function declared as `void` and/or written without `return` statement. :) – Nemanja Boric Aug 24 '13 at 09:02
  • 1
    @NemanjaBoric True dat! –  Aug 24 '13 at 09:02
  • Did you compile with gcc with all the warnings turned on? – Ed Heal Aug 24 '13 at 09:03
  • @NemanjaBoric `main` doesn't need return statement – Kal Aug 24 '13 at 09:03
  • this has been asked many times before, please check SO before asking. possible duplicate of [Const variable changed with pointer in C](http://stackoverflow.com/questions/9371892/const-variable-changed-with-pointer-in-c) – Jens Gustedt Aug 24 '13 at 09:03
  • 1
    @Kal It doesn't, technically (not in C99 and C++, at least), but as I've explained it yesterday, it's horrible style to omit the `return`. But I don't want to get into the non-constructive flamewar again, so let's not discuss this topic. –  Aug 24 '13 at 09:03
  • I am just referring yesterday's discussion: http://stackoverflow.com/questions/18402853/must-the-int-main-function-return-a-value-in-all-compilers/18402896#comment27029402_18402896 – Nemanja Boric Aug 24 '13 at 09:04
  • @H2CO3 can you link to the flamewar? i have not seen it – Kal Aug 24 '13 at 09:04
  • @Kal [Here you are](http://stackoverflow.com/questions/18402853/must-the-int-main-function-return-a-value-in-all-compilers/18402896#comment27029221_18402896). –  Aug 24 '13 at 09:07
  • @H2CO3 do you think every `void` function needs a `return;` at the end? – Kal Aug 24 '13 at 09:08
  • @Kal No, I don't. That's because `void` means "return no value", and "return upon reaching end-of-function" is intuitive. "return 0 implicitly" isn't. –  Aug 24 '13 at 09:10
  • @Krishna Kittu have you any additions to your Question, or want to know something additional to satisfy you with an answer? – dhein Aug 31 '13 at 15:43

2 Answers2

9

It is possible to change it but the behavior is undefined, as its mentioned in the standard!

Its in c11 under 6.7.3

If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined. If an attempt is made to refer to an object defined with a volatile-qualified type through use of an lvalue with non-volatile-qualified type, the behavior is undefined.

dhein
  • 6,431
  • 4
  • 42
  • 74
3
int * const p=&x;

This is not a valid program. &x is of type const int * but you are assigning the pointer value to an object of type int * const: the compiler has to issue a warning and is allowed to stop compilation.

ouah
  • 142,963
  • 15
  • 272
  • 331