|
|
|
|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
constants in C/C++
Hi all
My question is related to following sample code which tries to change consant value by pointers.(I know it is wrong practice but i am surprised by mis-behaviour) The code: #include <stdio.h> int main() { const int x = 10; int *y; const int * const z = &x; y = (int *)&x; *y=11; printf('%p %d ', y, *y); printf('%p %d ', &x, x); printf('%p %d ', z, *z); return 0; } The output: 0012FF7C 11 0012FF7C 10 0012FF7C 11 The above output is when the program was compiled as cpp file and got 11 11 11 when compiled as "c" file. Also const int x=10; int arr[x]; works with c++ compiler and not C. My question is why is this mis-behaviour. |
| Sponsored Links |
|
|
|
|||
|
I agree that compilers are free to optimise the way they like.But i was surprised to see exactly similar behaviour for c and c++ respectively on VC++ ,Turbo,gnu,aCC compilers.
All changed value of constant in C compilation but the value remained same in C++ compilation. similarly for array index being declared with constant. Why all the vendors opted for such behaviour made me suspect if their is some standard. shobhit |
| Sponsored Links |
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| perl: eval and constants | effigy | Shell Programming and Scripting | 5 | 01-23-2005 07:24 AM |