AvinashK
AvinashK

Reputation: 3423

character pointers

is it like char *pch='a'; means pch holds value 97 and is a pointer to another char whereas char *pch="avinash"; means that pch holds the pointer pointing to a of avinash.

Upvotes: 0

Views: 154

Answers (3)

Vijay
Vijay

Reputation: 67211

char *pch="avinash"; is a string literal and what you said is right. but the first one is a compile error. did you try compiling them and seeing the values?

Upvotes: 1

fnokke
fnokke

Reputation: 1161

No.

char * pch='a';

means that the address of pch is the value of 'a' which most likely points to an invalid part of the memory :-). If you want to set pch to point to 'a' you need to first allocate memory and then set

*pch = 'a';

for instance.

Upvotes: -1

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132994

char *pch='a'; shouldn't compile on any standard-compliant compiler.

char* pch = "absljsdf" is deprecated. you must use const.

const char * pcch = "abdsfkjsdf"; means that pcch points to the first character in "abdsfkjdf", but you can't modify the contents of the string with that pointer. hth

Upvotes: 1

Related Questions