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:
4 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.
Am12cs said:
1 decade ago
#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;
}
}
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;
}
}
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;
}
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;
}
Barcelona said:
1 decade ago
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.
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.
Kareena said:
5 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:
5 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.
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?
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.
Ravi said:
1 decade ago
const pointer cannot be passed to non-const parameter.
In line :fun(&ptr);
ptr in non-const, but argument required is const **ptr .
In line :fun(&ptr);
ptr in non-const, but argument required is const **ptr .
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.
Before changing ptr = 29ff0c and after change ptr = 29fed8.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers