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;
}
Discussion:
23 comments Page 1 of 3.
Rajlaxmi said:
2 years ago
const int *ptr = &i; This is a pointer to the constant integer, where the integer is stored in RODATA.(We can't modify the value pointed by ptr anymore through ptr i.e. *ptr = ; is not allowed further).
int fun(int **ptr)
Here ptr is a double pointer to an integer. As fun is a different function new stack frame will be initialized for fun().
*ptr = temp;
Overriding the value inside the function is local to the fun and *ptr is within the newly initialized stack frame for fun. So this won't throw any errors.
const *ptr = temp;
As we already have a declaration for this as int **, this leads to a compilation error.
The analysis is according to GCC compiler.
int fun(int **ptr)
Here ptr is a double pointer to an integer. As fun is a different function new stack frame will be initialized for fun().
*ptr = temp;
Overriding the value inside the function is local to the fun and *ptr is within the newly initialized stack frame for fun. So this won't throw any errors.
const *ptr = temp;
As we already have a declaration for this as int **, this leads to a compilation error.
The analysis is according to GCC compiler.
Kareena said:
3 years ago
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;
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;
TDas said:
3 years ago
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.
Ramana said:
4 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.
Prathyusha said:
6 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?
puzzle4.c:11:15: note: previous definition of \'ptr\' was here
int fun(int **ptr)
Can anyone help me to solve this?
Babu said:
6 years ago
What is the meaning of %5x ? what type of format specifier is this?
Uma said:
6 years ago
Can anyone tell what does this **ptr here?
Does this tell the address of address?
Does this tell the address of address?
Kavya said:
7 years ago
How to rectify the error? Anyone help me.
Siva said:
7 years ago
Can anyone explain to me?
How will the result come ptr=29ff0c ptr=29fed8?
How will the result come ptr=29ff0c ptr=29fed8?
Arshita said:
7 years ago
How will the result come as ptr=29ff0c ptr=29fed8 how it comes?
Can anyone explain me?
Can anyone explain me?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers