Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

c - const char pointer assignments

Are the following assignments valid? Or will any of these create problems. Please suggest.

const char * c1;  
const char * c2; 
const char * c3;
char * c4;

c2 = c1;
c2 = c3;
c2 = c4;

What if I do the following, is that ok thing to do?

const char * c5 = "xyz";
char * c6 = "abc";

c2 = c5;
c2 = c6;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your mind draw a line through the asterik. To the left is what is being pointed to and to the right what type of pointer

For example

  1. const char * const p - The pointer p is constant and so are the characters that p points to - i.e. cannot change both the pointer and the contents to what p points to
  2. const char * p - p points to constant characters. You can change the value of p and get it to point to different constant characters. But whatever p points to, you cannot change the contents.
  3. char * const p - You are unable to change the pointer but can change the contents

and finally

  1. char * p - Everything is up for grabs

Hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...