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

Showing Answers 1 - 5 of 5 Answers

ashish

  • Jul 18th, 2006
 

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.

  Was this answer useful?  Yes

prema

  • Sep 13th, 2006
 

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

 

  Was this answer useful?  Yes

ardashev

  • Nov 17th, 2007
 

#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

  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