Doubt regarding const......

R

Rajat

Hi all,

See the below code. Can u tell me what will be the output and why??

int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

I tested this on gcc, the result was c=16 *p= 4. As far as I think it
should give error while changing the read only memory content i.e. c,
or if it allow changes then it should reflect for bot c and *p.
 
R

Ravi Uday

Rajat said:
Hi all,

See the below code. Can u tell me what will be the output and why??

int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

C89: 3.5.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*.

Undefined means its not defined as to what the output could be. Could be
anything.
On my box i get c=4 *p=4

- Ravi
 
J

jacob navia

Rajat said:
Hi all,

See the below code. Can u tell me what will be the output and why??

int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

I tested this on gcc, the result was c=16 *p= 4. As far as I think it
should give error while changing the read only memory content i.e. c,
or if it allow changes then it should reflect for bot c and *p.
The error is to declare a pointer to a non-const data.

Using the lcc-win32 compiler you get the warning:

Warning tconst.c: 4 Different const qualifiers

The compiler warns you about this. Maybe you should also get this
warning using gcc if you increase the warning level.

jacob
 
D

Dave

Rajat said:
Hi all,

See the below code. Can u tell me what will be the output and why??

int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

I tested this on gcc, the result was c=16 *p= 4. As far as I think it
should give error while changing the read only memory content i.e. c,
or if it allow changes then it should reflect for bot c and *p.

As someone else said, this is undefined behaviour, so you're lucky you
didn't get monkeys flying out of your nose and other interesting results
of such practices.

Tried it myself but I got 4 for both c and *p, using MinGW 3.2.3. Best
bet if you want to work out what it's doing is to generate the assembler
(gcc -S) and look at that. My guess is that space for c is generated,
then the value 16 optimised into a register, or simply replaced
throughout the function by the literal value 16, either of which would
be reasonable interpretations of the code. Either way c would then
appear to contain one of two values depending how you reference it.

Dave.
 
C

CBFalconer

Rajat said:
See the below code. Can u tell me what will be the output and why??

int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

I tested this on gcc, the result was c=16 *p= 4. As far as I
think it should give error while changing the read only memory
content i.e. c, or if it allow changes then it should reflect
for bot c and *p.

The following shows the result after cleaning up the source and
making it legal by adding the #include statement. On my system
"gcc" exercises gcc through an alias, which supplies the parameters
to make it into a standard C compiler with a respectable warning
level. That includes -W -Wall -ansi -pedantic.

Without that #include the source invokes undefined behaviour, so
you have no reason to complain.

c:\c\junk>gcc junk.c
junk.c: In function `main':
junk.c:5: warning: initialization discards qualifiers from pointer
target type

c:\c\junk>a
c=4, *p=4
c:\c\junk>type junk.c
#include <stdio.h>
int main(void)
{
const int c = 16;
int *p = &c;

*p = 4;
printf("c=%d, *p=%d", c, *p);
return 0;
}
 
G

Gary E. Ansok

Hi all,

See the below code. Can u tell me what will be the output and why??

int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

If you're going to lie to the compiler, why should you trust anything
it says to you?

Gary
 
J

J. J. Farrell

See the below code. Can u tell me what will be the output
No.

and why??

Because the code causes undefined behaviour.
int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

I tested this on gcc, the result was c=16 *p= 4. As far as I think it
should give error while changing the read only memory content i.e. c,
or if it allow changes then it should reflect for bot c and *p.

I'm very surprised that gcc didn't give a warning for this while
compiling. Even the default warning levels (which are way too low)
give me a warning with gcc. It's a good idea to pay attention to
warnings, and ask for as many as the compiler will give you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top