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 1 of 3.

Rishabh Singh said:   5 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

while
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

Saloni sahu said:   4 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.
j=20.
Let the address of j is 200.

ptr=&j.

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.

Vineet Yadav said:   1 decade ago
const int *ptr;
int const* ptr;

It means pointer points to const integer value. that means we cant change the value of that integer but pointer can change its pointing location.

int * const ptr;
const * int ptr;

It means that pointer is constant here, it can point to an integer and then it cant change its pointing location.

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.

Shalini said:   8 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.

Ashok said:   1 decade ago
"const int *ptr" means ptr is a pointer to const integer so we can modify ptr value.

Where as "int const *ptr" is different one it means ptr is const pointer to int whose value could not be changed. We have to define it at the declaration of that pointer itself.

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!

Rupinderjit said:   1 decade ago
Here object is constant not pointer pointed to by it. So ptr can point to any other address unless we declare int const*ptr=&i;, here pointer points to particular location is constant not an object.

ChaZ said:   1 decade ago
Simplifying the above explanation,

const int *ptr //cannot modify *ptr

int* const ptr //cannot modify ptr


Difference in ptr and *ptr is self-explanatory.

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.