C Programming - Const - Discussion

Discussion Forum : 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;
}
Address of i
Address of j
10
223
Error: cannot convert parameter 1 from 'const int **' to 'int **'
Garbage value
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
23 comments Page 2 of 3.

Ghengha said:   1 decade ago
#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:   10 years ago
How did this happen?

Prakash said:   9 years ago
Yes, @Ghengha's answer is right and the result will come as,

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

Arshita said:   9 years ago
How will the result come as ptr=29ff0c ptr=29fed8 how it comes?

Can anyone explain me?

Siva said:   9 years ago
Can anyone explain to me?

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

Kavya said:   9 years ago
How to rectify the error? Anyone help me.

Uma said:   8 years ago
Can anyone tell what does this **ptr here?

Does this tell the address of address?

Babu said:   8 years ago
What is the meaning of %5x ? what type of format specifier is this?

Prathyusha said:   7 years ago
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:   6 years ago
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.


Post your comments here:

Your comments will be displayed after verification.