What is the difference between constant pointer and pointer
to a constant. Give examples.

Answers were Sorted based on User's Feedback



What is the difference between constant pointer and pointer to a constant. Give examples...

Answer / vignesh1988i

Constant pointer :
it is a pointer which points to the same memory location or
to same address and and whatever value the variable which is
pointed by the pointer holds.
for eg :
char a;
char const *p;
p=&a;
here var. a is a memory location having a address and that
variable contains some character data . but this pointer
p points to the same address ( a ) however the value in
var. a changes. finally, THE POINTER POINTED TO AN ADDRESS
IS A CONSTANT ,WHATEVER THE VALUE INSIDE THE VARIABLE MAY BE..

POINTER TO A CONSTANT :
this is a pointer which points to a constant variable
assigned to that pointer. and another pointer can also be
assigned to a same const. variable to point to.

for eg :
char Const a;
char *p,*q;
p=&a;
q=&a;



thank u

Is This Answer Correct ?    32 Yes 0 No

What is the difference between constant pointer and pointer to a constant. Give examples...

Answer / santosh

Pointer to constant: If pointer is pointing to constant
variable is caller pointer to constant. We can not change
the value of that constant.
const int constVariable = 6;
int *ptrConstVar = &constVariable;

Constant Pointer: We declare a pointer as constant. We can
change the content pointed by pointer. But we can not do any
airthmatic operation on the pointer like increment or decrement.
int localVariable =10;
const int *p = &localVariable;

we can not do p++ or p--;

Is This Answer Correct ?    23 Yes 8 No

What is the difference between constant pointer and pointer to a constant. Give examples...

Answer / abhradeep chatterjee

ya, vignesh, your answer is correct. thanx for giving such
a good answer.

Is This Answer Correct ?    9 Yes 0 No

What is the difference between constant pointer and pointer to a constant. Give examples...

Answer / prashant

the example given by Santosh for "Constant Pointer" is wrong .
Use
int * const p = &localVariable;
instead of const int *p = &localVariable;.

then we cant do p++ or p--;

Is This Answer Correct ?    5 Yes 1 No

Post New Answer

More C Interview Questions

what are two kinds of java

2 Answers  


How does selection sort work in c?

0 Answers  


Give a one-line C expression to test whether a number is a power of 2. [No loops allowed - it's a simple test.]

0 Answers  


What is the use of clrscr?

0 Answers  


In C, What is the #line used for?

2 Answers  






Explain how can I manipulate strings of multibyte characters?

0 Answers  


logic for generating all the combinations of the any number of given letters. ex::::::::: if a,b,c,d are given the o/p should be abcd,dcba,dbac,bcad,................ 4*3*2*1 combinations............

2 Answers   Infosys,


what is the difference between getch() and getche()?

7 Answers   Infosys,


How can I set an array's size at run time?

9 Answers  


List some of the static data structures in C?

0 Answers  


Why doesnt that code work?

0 Answers  


24.what is a void pointer? 25.why arithmetic operation can’t be performed on a void pointer? 26.differentiate between const char *a; char *const a; and char const *a; 27.compare array with pointer? 28.what is a NULL pointer? 29.what does ‘segmentation violation’ mean? 30.what does ‘Bus Error’ mean? 31.Define function pointers? 32.How do you initialize function pointers? Give an example? 33.where can function pointers be used?

0 Answers  


Categories