const char*和char const*以及char*const

redswallow posted @ Fri, 09 Jul 2010 15:12:22 +0800 in c/c++/c# , 1710 readers

const char*, char const*, char*const的区别问题几乎是C++面试中每次都会有的题目。

事 实上这个概念谁都有,只是三种声明方式非常相似很容易记混。
Bjarne在他的The C++ Programming Language里面给出过一个助记的方法:
把一个声明从右向左读。

char * const cp; ( * 读成 pointer to )
cp is a const pointer to char

const char * p;
p is a pointer to const char;

char const * p;
同上因为C++里面没有const*的运算符, 所以const只能属于前面的类型。


C++标准规定,const关键字放在类型或变量名之前等价的。

 

const int n=5;    //same as below
int const m=10;

 

const int *p;    //same as below  const (int) * p
int const *q;    // (int) const *p

 

char ** p1; 
//    pointer to    pointer to    char 
const char **p2;
//    pointer to    pointer to const char 
char * const * p3;
//    pointer to const pointer to    char 
const char * const * p4;
//    pointer to const pointer to const char 
char ** const p5;
// const pointer to    pointer to    char 
const char ** const p6;
// const pointer to    pointer to const char 
char * const * const p7;
// const pointer to const pointer to    char 
const char * const * const p8;
// const pointer to const pointer to const char

 

说到这里,我们可以看一道以前Google的笔试题:

[题目]const char *p="hello";
       foo(&p);//函数foo(const char **pp)
下面说法正确的是[]
A.函数foo()不能改变p指向的字符串内容
B.函数foo()不能使指针p指向malloc 生成的地址
C.函数foo()可以使p指向新的字符串常量
D.函数foo()可以把p赋值为 NULL.

至于这道题的答案是众说纷纭。针对上面这道题,我们可以用下面的程序测试:

 

#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>


void foo(const char **pp)
{
//    *pp=NULL;
//    *pp="Hello world!";
        *pp = (char *) malloc(10);
        snprintf(*pp, 10, "hi google!");
//       (*pp)[1] = 'x';

}

int
main()
{
    const char *p="hello";
    printf("before foo %s\n",p);
    foo(&p);
    printf("after foo %s\n",p);
    p[1] = 'x';

    return;
}



结论如下:

  1. 在foo函数中,可以使main函数中p指向的新的字符串常量。
  2. 在foo函数中,可以使main函数中的p指向 NULL。
  3. 在foo函数中,可以使main函数中的p指向由malloc生成的内存块,并可以在main中用free释放,但是会有 警告。但是注意,即使在foo中让p指向了由malloc生成的内存块,但是仍旧不能用p[1]='x';这样的语句改变p指向的内容。
  4. 在 foo中,不能用(*pp)[1]='x';这样的语句改变p的内容。

      所以,感觉gcc只是根据const的字面的意思对其作了限制,即对于const char*p这样的指针,不管后来p实际指向malloc的内存或者常量的内存,均不能用p[1]='x'这样的语句改变其内容。但是很奇怪,在foo里 面,对p指向malloc的内存后,可以用snprintf之类的函数修改其内容

 

Avatar_small
Rylan Landon said:
Sat, 29 Dec 2018 21:22:49 +0800

Definitely stable, brilliant, fact-filled facts in this article. Ones threads Do not sadden, and this absolutely is true in this article likewise. People generally produce a motivating understand. Would you say to Now i am fascinated?: )#) Sustain the great articles or blog posts. Accountants East Grinstead

Avatar_small
edubirdy said:
Sat, 21 Sep 2019 16:04:22 +0800

O yes how come I could forget this post.Actually I was badly busy in completing some forum urgent assignments so I could not reach you in time.Its good to see all details shared in this post.Thanks buddy.

Avatar_small
Smart schools said:
Wed, 25 Sep 2019 01:49:41 +0800

Its very interesting and thought provoking story.I like to cover such realistic cases in my writing services online articles and blogs.I really liked this post.Thanks for sharing this.

Avatar_small
Gay Tube said:
Thu, 16 Apr 2020 21:59:41 +0800

This is really very nice post you shared, i like the post, thanks for sharing..

Avatar_small
123mvoies said:
Thu, 28 May 2020 16:54:19 +0800

movie site https://123movies7.com

Avatar_small
sexting sites said:
Mon, 15 Jun 2020 01:05:59 +0800

Hi! This is my first visit to your blog! We are a team of volunteers and new initiatives in the same niche. Blog gave us useful information to work. You have done an amazing job!

Avatar_small
aumentar followers said:
Sat, 27 Jun 2020 06:13:39 +0800

I think about it is most required for making more on this get engaged

Avatar_small
https://www.pierdetu said:
Sun, 05 Jul 2020 00:53:09 +0800

This is really very nice post you shared, i like the post, thanks for sharing..

Avatar_small
búlgaros de leche pa said:
Mon, 06 Jul 2020 03:31:56 +0800

This is really very nice post you shared, i like the post, thanks for sharing..

Avatar_small
post a job said:
Sat, 22 Aug 2020 21:21:49 +0800

I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more.

Avatar_small
job posting said:
Sat, 22 Aug 2020 21:50:55 +0800

It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing.

Avatar_small
Investmentmakler said:
Thu, 27 Aug 2020 02:17:31 +0800

I’m impressed, I must say. Really rarely can i encounter a weblog that’s both educative and entertaining, and let me tell you, you have hit the nail for the head. Your idea is outstanding; the problem is something which there are not enough persons are speaking intelligently about. My business is very happy i found this around my search for something concerning this.

Avatar_small
hot men caps melhor said:
Fri, 04 Sep 2020 15:35:42 +0800

I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work

Avatar_small
how to fax documents said:
Sun, 06 Sep 2020 01:05:35 +0800

I feel a lot more people need to read this, very good info! .

Avatar_small
new orleans insuranc said:
Sun, 13 Sep 2020 18:58:56 +0800

And with most origin films, this one spent considerable time in establishing the ground rules for discovery of powers, the relationships between characters, all primed for action sequences in between long drawn narratives building up toward that inevitable big bang conclusion which contained everything but the kitchen sink.

Avatar_small
immobilien rendite b said:
Wed, 30 Sep 2020 19:55:01 +0800

I’m impressed, I must say. Genuinely rarely do I encounter a blog that’s both educative and entertaining, and without a doubt, you have hit the nail to the head. Your concept is outstanding; the issue is something that too little people are speaking intelligently about. My business is delighted that I came across this in my seek out something relating to this.

Avatar_small
pak24tv said:
Mon, 05 Oct 2020 20:25:12 +0800

I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post. <a href="https://www.pak24tv.com/blogs/think-and-grow-rich">think and grow rich</a> <a href="https://www.pak24tv.com/blogs/biggest-cities-in-the-world">top 10 biggest cities in the world</a> <a href="https://www.pak24tv.com/blogs/wonders-of-world">7 wonders of the world</a>

Avatar_small
pak24tv said:
Mon, 05 Oct 2020 20:25:26 +0800

I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post. think and grow rich top 10 biggest cities in the world 7 wonders of the world

Avatar_small
perfect ten showroom said:
Tue, 06 Oct 2020 00:18:51 +0800

It’s actually a cool and useful piece of info. I am glad that you just shared this useful info with us. Please keep us up to date like this. Thank you for sharing.

Avatar_small
Property listing man said:
Fri, 16 Oct 2020 20:57:25 +0800

Realestatecrm.io is the perfect cloud software for Property listing management, be more efficient close more deals.

Avatar_small
RajaQQ said:
Thu, 22 Oct 2020 17:30:01 +0800

If you don"t mind proceed with this extraordinary work and I anticipate a greater amount of your magnificent blog entries

Avatar_small
토토사이트 said:
Sat, 24 Oct 2020 03:11:27 +0800

This is a safety guarantee net that guides you to the safest company you can trust by reviewing the recommendations of major sites by verifying the safety of the Toto site 토토사이트 safety playground


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter