const char 指针赋值 [英] const char pointer assignments

查看:21
本文介绍了const char 指针赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下分配是否有效?或者这些中的任何一个都会产生问题.请提出建议.

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;

推荐答案

在你的脑海中用星号画一条线.左边是指向什么,右边是什么类型的指针

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

例如

  1. const char * const p - 指针 p 是常量,p 指向的字符也是常量 - 即不能同时更改p 指向的指针和内容
  2. const char * p - p 指向常量字符.您可以更改 p 的值并使其指向不同的常量字符.但是无论 p 指向什么,你都不能改变内容.
  3. char * const p - 你不能改变指针但可以改变内容
  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

最后

  1. char * p - 一切尽在掌握
  1. char * p - Everything is up for grabs

希望对您有所帮助.

这篇关于const char 指针赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆