C Programming - Const - Discussion

Discussion Forum : Const - Find Output of Program (Q.No. 5)
5.
What will be the output of the program in TurboC?
#include<stdio.h>
int fun(int **ptr);

int main()
{
    int i=10, j=20;
    const int *ptr = &i;
    printf(" i = %5X", ptr);
    printf(" ptr = %d", *ptr);
    ptr = &j;
    printf(" j = %5X", ptr);
    printf(" ptr = %d", *ptr);
    return 0;
}
i= FFE2 ptr=12 j=FFE4 ptr=24
i= FFE4 ptr=10 j=FFE2 ptr=20
i= FFE0 ptr=20 j=FFE1 ptr=30
Garbage value
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
28 comments Page 3 of 3.

Karun das said:   1 decade ago
Yes, here an object is a constant, we can't change pointer here,

Totally confusion, please correct me if I am thinking in incorrect way.

Siva pavan said:   1 decade ago
Coming to 2nd printf statement, since in pointers &p represents address,*p represents value then clearly p is declared as pointer preccedding by const keyword so its value is always constant since it is addressed to x(p=&x) the only possible value for p is x value i.e 10.

Sharath said:   1 decade ago
int i=10;
const int *ptr=10; //correct
*ptr=&i; //correct
*ptr=10; //wrong

Sharath said:   1 decade ago
We can change the value of constant pointer

Ajay said:   1 decade ago
const int *ptr = &i;

It declares a pointer which points to integer of constant type.
Pointer is not constant.So we can change it.

Sundar said:   1 decade ago
Output:

In Turbo C (under DOS - 16 bit platform)

i = FFF4 ptr = 10 j = FFF2 ptr = 20

In GCC (under Linux - 32 bit platform)

i = BF93368C ptr = 10 j = BF933688 ptr = 20

I hope this may be help you a bit. Have a nice day guys!

Himanshu said:   1 decade ago
Can't get it. Please explain it in step by step procedure.

Prashant dixit said:   1 decade ago
There must be an error in this program because ptr is declared as of constant type and it can not be modified so value of j can not be stored in ptr.


Post your comments here:

Your comments will be displayed after verification.