Go Back   The UNIX and Linux Forums > Top Forums > Programming
.
google site



Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-22-2002
Registered User
 

Join Date: Mar 2002
Posts: 16
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
  #2 (permalink)  
Old 08-22-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,150
Actually, you are misbehaving, not the compiler. When you violate the rules of a language any behavior by the compiler becomes legal. As for why this particular result happens, code like:
const int x = 10;
printf("%p %d \n", &x, x);

can be rewritten by the compiler as something like:
const int x = 10;
printf("%p 10 \n", &x);

Whether or not this happens is up the compiler. Most compilers have options to control optimizations. By fiddling with these options you may be able to induce both behaviors from both compilers. Or maybe not. "Garbage in, garbage out" applys heavily to compilers.
  #3 (permalink)  
Old 08-23-2002
Registered User
 

Join Date: Mar 2002
Posts: 16
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0