Following declarations are sameconst char *s; char const *s;

A) True
B) False

Showing Answers 1 - 6 of 6 Answers

kiranck007

  • Jan 24th, 2006
 

False ..the reason is

first case : s is a pointer to a character which is constant. i.e the value at the address location which is being pointed by s cannot be changed.

second case: s is a constant pointer : ie the address at which it is being pointed to cannot be changed but the char variable is changeable.

  Was this answer useful?  Yes

kiranck007

  • Jan 27th, 2006
 

i am sorry....actually there is no difference b/w

const char *s;

and

char const *s;

but there is a difference b/w above two and and the below one

The first two are interchangeable; they declare a pointer to a constant character (you can't change any pointed-to characters).

char * const p;

declares a constant pointer to a (variable) character (i.e. you can't change the pointer).

mihir_void

  • Mar 5th, 2009
 

No, the declarations are not same
Let us take see each one of them

1. const char *s;


Here the char is constant hence
const char *s="hello" ; //allowed
s[3]='o' ;//error as char is constant
s="bye";//this is allowed as the pointer is not constant

2. char const *s;
here the pointer is constant
const char *s="hello" ; //allowed initialization
s[3]='o'; //Allowed!! as content of s ie the address of string hello is unchanged
s="bye"; //Error as the address of the pointer is changed

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions