Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


wats the diference btwen constant pointer and pointer to a
constant.pls give examples.

Answers were Sorted based on User's Feedback



wats the diference btwen constant pointer and pointer to a constant.pls give examples...

Answer / rani

Constant pointer cannot be reassigned to different objects
once its initialized. It can be used to modify the object
that it points to.

Eg.
int *const constPtr;

*constPtr =0; //can be modified like this

Pointer to constant can be reassigned to point to another
object of same type.

int const *ptrConst;

ptrConst = 0;

Is This Answer Correct ?    37 Yes 3 No

wats the diference btwen constant pointer and pointer to a constant.pls give examples...

Answer / monika sethi

CONSTANT POINTER: A pointer will always point to one
object.it is initialized at the time of declaration.
e.g
int i=20,j;
int* const p=&i;
cout<<*p; //will print 20
*p=30;//works....i.e the value pointed by the constant
pointer can be changed
//now if write
p=&j;//error

POINTER TO CONSTANT

it can be declared as
const int *p;
or
int const *p;
int i=10,j=30;
p=&i;
cout<<*p;
*p=10//error...the value is constant pointed by p
//pointer p is not constant so it can now point to another
variable of integer type
//so if we write
p=&j //it will now point to a variable j

that's all.........

Is This Answer Correct ?    27 Yes 0 No

wats the diference btwen constant pointer and pointer to a constant.pls give examples...

Answer / satish

char * const p; ->it is const pointer to char
const char * p; ->it is pointer to const char

Is This Answer Correct ?    29 Yes 3 No

wats the diference btwen constant pointer and pointer to a constant.pls give examples...

Answer / sanjay

Pointer to a consttant ::: such that the value to which
pointer points can't be changed
const int *ptr1=7
*ptr=8 // invalid
constant pointer :::: pointer direction can't be cheged .
int *const x =6;
x=7 or x=&i // invalid
Location of pointer can't be chenged.

Is This Answer Correct ?    18 Yes 2 No

wats the diference btwen constant pointer and pointer to a constant.pls give examples...

Answer / 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

wats the diference btwen constant pointer and pointer to a constant.pls give examples...

Answer / satyajit nayak

Hello All,
AS name suggested things are very simple.
1.Constant pointer
======================
it's nothing but a
(i)pointer which is pointing to a constant memory
location.
(ii)Once this pointer is assigned to point some location
we cann't make it to locate some other address location.
(iii)So constant pointer should be initialized at the
point of declaration only.

Example
=========
1. int main()
{
int * const my_ptr; //now my_ptr point to garbage
location
int var=10;
my_ptr=&var; //trying to modify my_ptr location.so
gives ERROR
}
so correct way of assignment is like
int main()
{
int var=10;
int * const my_ptr=&var;
printf("Addr=%x,val=%d",my_ptr,*my_ptr);
}

Is This Answer Correct ?    8 Yes 1 No

wats the diference btwen constant pointer and pointer to a constant.pls give examples...

Answer / jose j pothoor

I see ur explanation...then what would be the answer of
below program

void main()
{
int const *p=5;
printf("%d",++(*p));
}
a)5
b)6
c) Run time error
d) Compiler error

Is This Answer Correct ?    7 Yes 1 No

wats the diference btwen constant pointer and pointer to a constant.pls give examples...

Answer / jamili reddy

int i =12;
int * const ptr = 24; //constant pointer

you can't change the ptr value

const int *ptr = 24;//ptr to a constant

int const * ptr = 24;//ptr to a constant


you can't change the value at ptr

Is This Answer Correct ?    2 Yes 1 No

wats the diference btwen constant pointer and pointer to a constant.pls give examples...

Answer / rudra prasad panda

Pointer is a variable containing the addres of another
variable;
Constant pointer is a variable which points to a variable
constantly.it means that , once it is initialised,it cannot
be changed.Till the end of program,it points to that
variable only;
EXAMPLE:
char k,m;
const char *p=&k;
p=&m;//(syntax error)

********
A pointer to a constant is a pointer such that the contents
of the variable(pointed by the pointer)cannot be modified
throuhg the pointer;
EXAMPLE:
char m='l';
char * const p;
p=&m;
*p='u';//syntax error
m='k';//no syntax error

Is This Answer Correct ?    8 Yes 42 No

Post New Answer

More C Interview Questions

why little endian and big endian came?y they using differently? y they not used commonly ?wt is application of little and big ?

1 Answers  


What is the explanation for cyclic nature of data types in c?

0 Answers  


how to add two numbers without using arithmetic operators?

4 Answers  


When can you use a pointer with a function?

0 Answers  


What are the advantages of using Unions?

0 Answers   IBS,


What is Generic pointer? What is the purpose of Generic pointer? Where it is used?

3 Answers  


Write a program to compare two strings without using the strcmp() function

42 Answers   Accenture, Arba Minch University,


Q.1 write a program to create binary tree 1 to 16 numbers? Q.2 write a program to creat a binary search tree for the member that is given by user?

0 Answers   Case, IBM,


explain what is fifo?

0 Answers  


What is volatile variable in c?

0 Answers  


void main(){ int a; a=1; while(a-->=1) while(a-->=0); printf("%d",a); }

0 Answers  


How do you list a file’s date and time?

0 Answers  


Categories