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


Please Help Members By Posting Answers For Below Questions

What is extern storage class in c?

515


what is recursion in C

618


regarding pointers concept

1579


How many levels of indirection in pointers can you have in a single declaration?

598


Explain how do you list files in a directory?

620






Explain about C function prototype?

613


What are the differences between Structures and Arrays?

614


How can I get the current date or time of day in a c program?

654


What is the usage of the pointer in c?

608


How can I write functions that take a variable number of arguments?

631


When was c language developed?

706


How can I discover how many arguments a function was actually called with?

637


Why do we need functions in c?

563


How can I determine whether a machines byte order is big-endian or little-endian?

624


What is a null string in c?

591