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;
}
Discussion:
28 comments Page 1 of 3.
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.
Himanshu said:
1 decade ago
Can't get it. Please explain it in step by step procedure.
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!
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!
Ajay said:
1 decade ago
const int *ptr = &i;
It declares a pointer which points to integer of constant type.
Pointer is not constant.So we can change it.
It declares a pointer which points to integer of constant type.
Pointer is not constant.So we can change it.
Sharath said:
1 decade ago
We can change the value of constant pointer
Sharath said:
1 decade ago
int i=10;
const int *ptr=10; //correct
*ptr=&i; //correct
*ptr=10; //wrong
const int *ptr=10; //correct
*ptr=&i; //correct
*ptr=10; //wrong
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.
Karun das said:
1 decade ago
Yes, here an object is a constant, we can't change pointer here,
Totally confusion, please correct me if I am thinking in incorrect way.
Totally confusion, please correct me if I am thinking in incorrect way.
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.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers