wats the diference btwen constant pointer and pointer to a
constant.pls give examples.
Answer Posted / surya mukherjee
//POINTER TO CONSTANT vs CONSTANT POINTER
#include<iostream.h>
#include<conio.h>
//POINTER TO CONSTANT
void f1()
{
int i=10,j=20;
const int* pi=&i;
cout<<*pi<<endl;
//*pi = 200; ERROR : CANNOT MODIFY A CONST OBJECT IN f1()
pi=&j; // IT CAN POINT ANOTHER CONSTANT
cout<<*pi<<endl;
}
//CONSTANT POINTER
void f2()
{
int i=100,j;
int* const pi=&i;
cout<<*pi<<endl;
*pi = 200; // IT CAN ASSIGN ANOTHER VALUE AT THIS ADDRESS
cout<<*pi<<endl;
//pi=&j; ERROR : CANNOT MODIFY A CONST OBJECT IN f2()
}
void main()
{
clrscr();
f1();
f2();
getch();
}
| Is This Answer Correct ? | 10 Yes | 1 No |
Post New Answer View All Answers
I was asked to write a program in c which when executed displays how many no.of clients are connected to the server.
the real constant in c can be expressed in which of the following forms a) fractional form only b) exponential form only c) ascii form only d) both a and b
Explain what are run-time errors?
Explain enumerated types in c language?
What is function definition in c?
Are local variables initialized to zero by default in c?
When should the register modifier be used? Does it really help?
What is getch?
illustrate the use of address operator and dereferencing operator with the help of a program guys plzzz help for this question
Which is best book for data structures in c?
What is the use of c language in real life?
How macro execution is faster than function ?
What are 3 types of structures?
Is using exit() the same as using return?
Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal. [ I gave the obvious solution of taking % 10 and / 10, which gives us the decimal value in reverse order. This requires an array since we need to print it out in the correct order. The interviewer wasn't too pleased and asked me to give a solution which didn't need the array ].