C Programming - Const - Discussion

Discussion :: Const - Find Output of Program (Q.No.3)

3. 

What will be the output of the program?

#include<stdio.h>
int fun(int **ptr);

int main()
{
    int i=10;
    const int *ptr = &i;
    fun(&ptr);
    return 0;
}
int fun(int **ptr)
{
    int j = 223;
    int *temp = &j;
    printf("Before changing ptr = %5x\n", *ptr);
    const *ptr = temp;
    printf("After changing ptr = %5x\n", *ptr);
    return 0;
}

[A]. Address of i
Address of j
[B]. 10
223
[C]. Error: cannot convert parameter 1 from 'const int **' to 'int **'
[D]. Garbage value

Answer: Option C

Explanation:

No answer description available for this question.

Shammas said: (May 18, 2011)  
I can't understand the concept..

Gopu said: (May 26, 2011)  
Please explain this.

Xyz said: (Jun 14, 2011)  
Please anybody explain this one.

Ravi said: (Jul 2, 2011)  
const pointer cannot be passed to non-const parameter.

In line :fun(&ptr);
ptr in non-const, but argument required is const **ptr .

Indu said: (Aug 5, 2011)  
Can Anyone Explain?

Ranjit said: (Aug 23, 2011)  
Ravi already said it.

Ptr is a const pointer. The argument of fun should be const too.

Am12Cs said: (Aug 31, 2011)  
#include<stdio.h>
int fun(int **ptr);

int main()
{
int i=10;
const int *ptr = &i;{{Probably CONST INT*PTR IS ASSIGNED TO REFERENCE OF 'I' WHICH CNT BE ALTERED BUT PTR IN FUTURE CAN BE ASSIGNED SOMETHING ELSE WHICH WILL NOT GIVE AN ERROR.
const int *ptr = &i;
*PTR=12;//ERROR
ptr=10;//NO ERROR

const int *ptr = &i;
int fun(int **ptr)
{
int j = 223;
int *temp = &j;
printf("Before changing ptr = %5x\n", *ptr);
const *ptr = temp;// HERE IS THE ERROR
printf("After changing ptr = %5x\n", *ptr);
return 0;
}
}

Bond said: (Sep 8, 2011)  
The ptr is declared twice in the function. How come that is not an error?

Shru said: (Sep 23, 2011)  
Ravi explanation is correct.

Barcelona said: (Apr 26, 2012)  
ptr is not a constant pointer but the value it points to is constant.
ptr is constant pointer, if it should declare like this
int * const ptr;
But it is declared like
const int *ptr;
ptr may points to some other location but the fact here is that it should contain value 10. however in question value changes to 223. that's why error is there.

Ghengha said: (Nov 8, 2014)  
#include<stdio.h>
int fun(const int **ptr);//*

int main()
{
int i=10;
const int *ptr = &i;
fun(&ptr);
return 0;
}

int fun(const int **ptr)//*

{
int j = 223;
int *temp = &j;
printf("Before changing ptr = %5x\n", *ptr);
*ptr = temp;
printf("After changing ptr = %5x\n", *ptr);
return 0;
}

Rahul said: (Aug 31, 2015)  
How did this happen?

Prakash said: (Mar 31, 2016)  
Yes, @Ghengha's answer is right and the result will come as,

Before changing ptr = 29ff0c and after change ptr = 29fed8.

Arshita said: (May 19, 2016)  
How will the result come as ptr=29ff0c ptr=29fed8 how it comes?

Can anyone explain me?

Siva said: (Aug 28, 2016)  
Can anyone explain to me?

How will the result come ptr=29ff0c ptr=29fed8?

Kavya said: (Sep 26, 2016)  
How to rectify the error? Anyone help me.

Uma said: (Jul 14, 2017)  
Can anyone tell what does this **ptr here?

Does this tell the address of address?

Babu said: (Jan 27, 2018)  
What is the meaning of %5x ? what type of format specifier is this?

Prathyusha said: (Feb 15, 2018)  
In my ubuntu os, error: \'ptr\' redeclared as different kind of symbol
puzzle4.c:11:15: note: previous definition of \'ptr\' was here
int fun(int **ptr)

Can anyone help me to solve this?

Ramana said: (Sep 11, 2019)  
In the function defination **ptr is integer type and it is redeclared as const int *. So it will come error as ptr is redeclared as different kind of symbol.

Tdas said: (Apr 12, 2020)  
Const *ptr=temp; here *ptr is already a constant pointer which is holding the address of i. Therefore,it can not be reinitialized with another pointer variable temp which is holding the address of j.

Kareena said: (Jun 13, 2020)  
Hello everyone.

int i=10;
const int *ptr = &i;

Here, ptr is a pointer to a constant integer.
And therefore it should point to a constant integer.
But ptr is pointing to i, which is not a constant integer.

It should be like this:

const int i=10;
const int *ptr = &i;

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.