C Programming - Const - Discussion

Discussion Forum : Const - Find Output of Program (Q.No. 5)
What will be the output of the program in TurboC?
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
No answer description is available. Let's discuss.
28 comments Page 1 of 3.

Saloni sahu said:   2 years ago
const int *ptr=&i;
*ptr=&j :WRONG
*ptr=10 :WRONG
ptr=&j :RIGHT

As is this question i=10

Let address of i is 100 so ptr stored the value 100.
Let the address of j is 200.


It means the value of ptr is changed to 200 and it points to j.
So the value of *ptr is 20 at the end.

Here *ptr is const but we can change ptr.

Rishabh Singh said:   3 years ago
const int* ptr;

It declares ptr a pointer to const int type. You can modify ptr itself but the object pointed to by ptr shall not be modified.

const int a = 10;
const int* ptr = &a;
*ptr = 5; // wrong
ptr++; // right

int * const ptr;
declares ptr a const pointer to int type. You are not allowed to modify ptr but the object pointed to by ptr can be modified.

int a = 10;
int *const ptr = &a;
*ptr = 5; // right
ptr++; // wrong
int const *ptr; // ptr is a pointer to constant int
int *const ptr; // ptr is a constant pointer to int

Pooja said:   3 years ago
I can't understand the output. Please explain me.

Dhivya.s said:   4 years ago
Please explain how FFF4 from ptr?

Aditya said:   5 years ago
What will be the output of the following statement with an explanation?


Appu said:   5 years ago
Please explain the given code.

Selvakumar said:   5 years ago
I m not getting the given code. Please anyone explain this.

Shalini said:   6 years ago
Here FFE4 and FFE2 are address of I and j variable respectively.

We can't predict address of variables every time.

I think some peoples doubt is same, why address of i is higher than address of j answer is because stack grows downwards in memory.

I hope you get it.

Bhavani said:   6 years ago
How FFE4 and FFE2 came in output? Please explain me.

Suyog said:   6 years ago
Please, can anybody explain it detail?

Post your comments here:

Your comments will be displayed after verification.