GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Tech FAQs  >  OOPS
Go To First  |  Previous Question  |  Next Question 
 OOPS  |  Question 229 of 258    Print  
Please give me clear idea about these following declaration.

const char *q="hello";
*q='m';/*error*/
q="bye";

char const *s="hello";
*s='m';/*error*/
s="bye";

char *const t="hello";
*t='m';/*works*/
t="bye";/*error*/


  
Total Answers and Comments: 3 Last Update: November 17, 2007     Asked by: pradyumna amanta 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
July 18, 2006 04:30:13   #1  
ashish        

RE: Please give me clear idea about these following de...

hi

when we declare a pointer like int *a anything before * tels ihe type of variable of which addres the ponter will store.

like in this case a will store the address of a int type

and everything after * tells about the tupe of the pointr

like in const char * const p

const char is the type of var whose address will bestored in p while const after * tells about the ponter itself that is it is a constant pointer

now in ur prog. when u write const char *q hello it means that hello is a constant and can;t be modified hence *q 'm' an error

while in char *const p hai p is aconst ponter so p bye is not valid.


 
Is this answer useful? Yes | No
September 13, 2006 03:22:20   #2  
prema        

RE: Please give me clear idea about these following de...

HI

const char *q hello ;
*q 'm';/*error*/
q bye ;

char const *s hello ;
*s 'm';/*error*/
s bye ;

char *const t hello ;
*t 'm';/*works*/
t bye ;/*error*/


 
Is this answer useful? Yes | No
November 17, 2007 14:42:19   #3  
ardashev Member Since: November 2007   Contribution: 6    

RE: Please give me clear idea about these following de...
#include <stdlib.h>
#include <stdio.h>
int main()
{
const char *q "hello";
// "hello" sits in a memory location and cannot be changed
printf(" p sn" q q);
q "bye";
// "bye" is created in a different location in memory and a new pointer is created for it
// original "hello" is not changed and cannot be since it is const only the pointer which is not const is changed ( beware though that you loose pointer to "hello" altogether here)
printf(" p sn" q q);
return 0;
}
// output:
// 0x4005e8 hello
// 0x4005f6 bye

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape