C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Tricky const question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Mohanasundaram
Guest





PostPosted: Sun Mar 07, 2004 6:03 pm    Post subject: Tricky const question Reply with quote



Hi All,

I have the following question asked by my friend.

/*Copiled using C++ compiler*/
#include <stdio.h>

int main()
{
const int i = 10;
int *p = (int *)&i;
++(*p);
printf("%dn",i);
printf("%dn",*p);
return 0;
}

The output is
10
11

/*Copiled using C compiler*/
#include <stdio.h>

int main()
{
const int i = 10;
int *p = (int *)&i;
++(*p);
printf("%dn",i);
printf("%dn",*p);
return 0;
}

The output is
11
11

We tried this in VC++ 6.0, gcc and solaris native compiler. All the
compilers give the same answer. My friend says it is the expected
behaviour of the languages. Is it the standard behaviour of the
languages? Can anyone please explain? I don't have any clue about
this?

Regards,
Mohan.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Douglas A. Gwyn
Guest





PostPosted: Sun Mar 07, 2004 8:52 pm    Post subject: Re: Tricky const question Reply with quote



Mohanasundaram wrote:
Quote:
My friend says it is the expected
behaviour of the languages.

No, your program invokes undefined behavior.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.