ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   To Refer this Site to Your Friends   Click Here
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
wats the diference btwen constant pointer and pointer to a 
constant.pls give examples.
 Question Submitted By :: Uttejana
I also faced this Question!!     Rank Answer Posted By  
 
  Re: wats the diference btwen constant pointer and pointer to a constant.pls give examples.
Answer
# 1
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 ?    4 Yes 0 No
Rani
 
  Re: wats the diference btwen constant pointer and pointer to a constant.pls give examples.
Answer
# 2
char * const p;  ->it is const pointer to char
  const char * p;  ->it is pointer to const char
 
Is This Answer Correct ?    6 Yes 0 No
Satish
 
 
 
  Re: wats the diference btwen constant pointer and pointer to a constant.pls give examples.
Answer
# 3
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 ?    2 Yes 10 No
Rudra Prasad Panda
 
  Re: wats the diference btwen constant pointer and pointer to a constant.pls give examples.
Answer
# 4
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 ?    7 Yes 1 No
Sanjay
 
  Re: wats the diference btwen constant pointer and pointer to a constant.pls give examples.
Answer
# 5
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 ?    1 Yes 0 No
Monika Sethi
 
  Re: wats the diference btwen constant pointer and pointer to a constant.pls give examples.
Answer
# 6
//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 ?    1 Yes 0 No
Surya Mukherjee
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
C program code int zap(int n) { if(n<=1)then zap=1; else zap=zap(n-3)+zap(n-1); } then the call zap(6) gives the values of zap [a] 8 [b] 9 [c] 6 [d] 12 [e] 15 Wipro4
HOW DO YOU HANDLE EXCEPTIONS IN C? AppLabs2
how to exchnage bits in a byte b7<-->b0 b6<-->b1 b5<-->b2 b4<-->b3 please mail me the code if any one know to rajeshmb4u@gmail.com Honeywell3
how the size of an integer is decided? - is it based on processor or compiler or OS? nvidia16
How can I return multiple values from a function?  4
how many argument we can pas in in a function CTS20
Write a C program that reads a series of strings and prints only those ending in "ed"  2
Function to find the given number is a power of 2 or not? Motorola12
Study the code: void show() main() { show(); } void show (char *s) { printf("%sn",s); } What will happen if it is compiled & run on an ANSI C Compiler? A)It will compile & nothing will be printed when it is executed B)it will compile but not link C)the compiler will generate an error D)the compiler will generate a warning Accenture4
what is differnence b/w macro & functions  1
difference between function & structure Verizon5
plz answer..... a program that reads non-negative integer and computes and prints its factorial  2
Write a c code segment using a for loop that calculates and prints the sum of the even integers from 2 to 30, inclusive?  2
Why is conio.h not required when we save a file as .c and use clrscr() or getch() ?  2
main() { int x=5; printf("%d %d %d\n",x,x<<2,x>>2); } what is the output? Ramco7
how to generate the length of a string without using len funtion?  3
Please list all the unary and binary operators in C.  1
write an algorithm which can find the largest number among the given list using binary search ............... this was asked in the interview Satyam2
let's take a code struct FAQ { int a; char b; float c; double d; int a[10]; }*temp; now explain me how the memory will be allocated for the structure FAQ and what address will be in the structure pointer (temp)....................  7
what is the use of getch() function in C program.. difference b/w getch() and getche()?? Wipro12
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com