 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mohanasundaram Guest
|
Posted: Sun Mar 07, 2004 6:03 pm Post subject: Tricky const question |
|
|
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
|
Posted: Sun Mar 07, 2004 8:52 pm Post subject: Re: Tricky const question |
|
|
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 |
|
 |
|
|
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
|
|