What would be the output of the following program? main() { const int x=5; int *ptrx; ptrx=&x; *ptrx=10; printf("%d",x); }

A) 5
B) 10
C) Error
D) Garbage Value

Showing Answers 1 - 23 of 23 Answers

answer will be option (c).that is error.

Here we are trying to modify the  constant value of x to 10 which should not be done.. so it will pop an error saying that cant modify a constant value

  Was this answer useful?  Yes

Bhanu

  • Mar 17th, 2006
 

Ans: a) 5

Hello,

Here we are assiging address of x to ptrx, so we are not changing

the value of X, So value of x is 5

  Was this answer useful?  Yes

louisyang

  • Mar 29th, 2006
 

It depends on compiler.

In C++, it is not allowed to convert const int* to int*. So error

In C, it is ok, and i = 10; So dont use C compiler, it is time to use C++ compiler, just my one cent

satyanarayana.p

  • Apr 11th, 2006
 

hai all,

Ans is 10;

Because we r using pointer to chane the value of the variable.So it should be ans is 10.

if u give ptr=10 in above question then ans is 5 only

  Was this answer useful?  Yes

it is not a matter of compiler it is matter of our fundamental
so fundamental is that we can not change constant .
so there is error. if talk to turbo compiler then run this program without error.
and i m telling that turbo compiler has sevrak mistakes that confuse a begginer.

  Was this answer useful?  Yes

it is not a matter of compiler it is matter of our fundamental
so fundamental is that we can not change constant .
so there is error. if talk to turbo compiler then run this program without error.
and i m telling that turbo compiler has sevral mistakes that confuse a begginers.

  Was this answer useful?  Yes

fkhanoom

  • Mar 19th, 2008
 

Remember that const variable in principle can not be changed. However, this may not be enforced with some compilers.

If compiling with a C compiler, there is a warning, but the assignment discards qualifier const. So, x = 10.

If compiling with a C++ compiler, the above assignment is not allowed. Thus, there will be an error (invalid conversion from const int * to int * ).

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions