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.
Saloni sahu said:
3 years ago
const int *ptr=&i;
*ptr=&j :WRONG
*ptr=10 :WRONG
ptr=&j :RIGHT
As is this question i=10
Let address of i is 100 so ptr stored the value 100.
j=20.
Let the address of j is 200.
ptr=&j.
It means the value of ptr is changed to 200 and it points to j.
So the value of *ptr is 20 at the end.
Here *ptr is const but we can change ptr.
*ptr=&j :WRONG
*ptr=10 :WRONG
ptr=&j :RIGHT
As is this question i=10
Let address of i is 100 so ptr stored the value 100.
j=20.
Let the address of j is 200.
ptr=&j.
It means the value of ptr is changed to 200 and it points to j.
So the value of *ptr is 20 at the end.
Here *ptr is const but we can change ptr.
Rishabh Singh said:
4 years ago
const int* ptr;
It declares ptr a pointer to const int type. You can modify ptr itself but the object pointed to by ptr shall not be modified.
const int a = 10;
const int* ptr = &a;
*ptr = 5; // wrong
ptr++; // right
while
int * const ptr;
declares ptr a const pointer to int type. You are not allowed to modify ptr but the object pointed to by ptr can be modified.
int a = 10;
int *const ptr = &a;
*ptr = 5; // right
ptr++; // wrong
int const *ptr; // ptr is a pointer to constant int
int *const ptr; // ptr is a constant pointer to int
It declares ptr a pointer to const int type. You can modify ptr itself but the object pointed to by ptr shall not be modified.
const int a = 10;
const int* ptr = &a;
*ptr = 5; // wrong
ptr++; // right
while
int * const ptr;
declares ptr a const pointer to int type. You are not allowed to modify ptr but the object pointed to by ptr can be modified.
int a = 10;
int *const ptr = &a;
*ptr = 5; // right
ptr++; // wrong
int const *ptr; // ptr is a pointer to constant int
int *const ptr; // ptr is a constant pointer to int
Pooja said:
4 years ago
I can't understand the output. Please explain me.
Dhivya.s said:
5 years ago
Please explain how FFF4 from ptr?
Aditya said:
6 years ago
What will be the output of the following statement with an explanation?
printf("%X%x%ci%x",11,10,'s',12);
printf("%X%x%ci%x",11,10,'s',12);
Appu said:
6 years ago
Please explain the given code.
Selvakumar said:
6 years ago
I m not getting the given code. Please anyone explain this.
Shalini said:
7 years ago
Here FFE4 and FFE2 are address of I and j variable respectively.
We can't predict address of variables every time.
I think some peoples doubt is same, why address of i is higher than address of j answer is because stack grows downwards in memory.
I hope you get it.
We can't predict address of variables every time.
I think some peoples doubt is same, why address of i is higher than address of j answer is because stack grows downwards in memory.
I hope you get it.
Bhavani said:
7 years ago
How FFE4 and FFE2 came in output? Please explain me.
Suyog said:
8 years ago
Please, can anybody explain it detail?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers