const char*和char const*以及char*const
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
[题目]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; }
结论如下:
- 在foo函数中,可以使main函数中p指向的新的字符串常量。
- 在foo函数中,可以使main函数中的p指向 NULL。
- 在foo函数中,可以使main函数中的p指向由malloc生成的内存块,并可以在main中用free释放,但是会有 警告。但是注意,即使在foo中让p指向了由malloc生成的内存块,但是仍旧不能用p[1]='x';这样的语句改变p指向的内容。
- 在 foo中,不能用(*pp)[1]='x';这样的语句改变p的内容。
所以,感觉gcc只是根据const的字面的意思对其作了限制,即对于const char*p这样的指针,不管后来p实际指向malloc的内存或者常量的内存,均不能用p[1]='x'这样的语句改变其内容。但是很奇怪,在foo里 面,对p指向malloc的内存后,可以用snprintf之类的函数修改其内容
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
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.
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.
Thu, 05 Dec 2019 16:20:49 +0800
nice post Thx
Mon, 27 Jan 2020 15:29:46 +0800
nice post Thx
Thu, 16 Apr 2020 21:59:41 +0800
This is really very nice post you shared, i like the post, thanks for sharing..
Thu, 28 May 2020 16:54:19 +0800
movie site https://123movies7.com
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!
Sat, 27 Jun 2020 06:13:39 +0800
I think about it is most required for making more on this get engaged
Sun, 05 Jul 2020 00:53:09 +0800
This is really very nice post you shared, i like the post, thanks for sharing..
Mon, 06 Jul 2020 03:31:56 +0800
This is really very nice post you shared, i like the post, thanks for sharing..
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.
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.
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.
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
Sun, 06 Sep 2020 01:05:35 +0800
I feel a lot more people need to read this, very good info! .
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.
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.
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>
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
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.
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.
Sat, 17 Oct 2020 18:22:06 +0800
cheap flights airline tickets airfares Search CheapTickets Compare Low Cost Airlnes Tickets.
Sat, 17 Oct 2020 18:22:46 +0800
cheap flights airline tickets airfares Search CheapTickets Compare Low Cost Airlnes Tickets.
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
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
Sun, 25 Oct 2020 18:03:00 +0800
I read a article under the same title some time ago, but this articles quality is much, much better. How you do this..
Tue, 27 Oct 2020 16:40:41 +0800
Thanks for providing recent updates regarding the concern, I look forward to read more.
Fri, 30 Oct 2020 01:13:00 +0800
coach sportif réunion Gym Wear or Personal Training | Shapeitservices
Fri, 30 Oct 2020 21:32:10 +0800
กำจัดปลวกเชียงใหม่ ( chiangmai termite ) เป็นบริษัทกำจัดปลวกเชียงใหม่ ฉีดปลวกเชียงใหม่ราคาถูก เน้นความคุ้มค่า เพื่อลดค่าใช้จ่ายของลูกค้า. มีขั้นตอนการฉีดปลวกเหมือนกับบริษัทใหญ่ราคาแพง.เรารับงานทุกตำบล ทุกอำเภอ ในเชียงใหม่ , อำเภอเมืองเชียงใหม่ , ศรีภูมิ , พระสิงห์ , หายยา , ช้างม่อย , ช้างคลาน , วัดเกต , ช้างเผือก , สุเทพ , แม่เหียะ , ป่าแดด , หนองหอย , ท่าศาลา , หนองป่าครั่ง , ฟ้าฮ่าม , ป่าตัน , สันผีเสื้อ , อำเภอจอมทอง , อำเภอเชียงดาว , อำเภอดอยสะเก็ด , อำเภอแม่ริม , อำเภอสะเมิง , อำเภอฝาง , อำเภอแม่อาย , อำเภอพร้าว , อำเภอสันป่าตอง , อำเภอสันกำแพง , อำเภอสันทราย , สันทรายหลวง , สันทรายน้อย , สันพระเนตร , สันนาเม็ง , สันป่าเปา , หนองแหย่ง , หนองจ๊อม , หนองหาร , แม่แฝก , แม่แฝกใหม่ , เมืองเล็น , ป่าไผ่ , อำเภอหางดง , หางดง , หนองแก๋ว , หารแก้ว , หนองตอง , ขุนคง , สบแม่ข่า , บ้านแหวน , สันผักหวาน , หนองควาย , บ้านปง , น้ำแพร่ , อำเภอฮอด , อำเภอดอยเต่า , อำเภออมก๋อย , อำเภอเวียงแหง , อำเภอไชยปราการ , อำเภอแม่วาง , อำเภอแม่ออน , อำเภอดอยหล่อ , อำเภอกัลยาณิวัฒนา ,
Sun, 01 Nov 2020 06:42:43 +0800
Remote database administration Services | Abtech Technologies — Migration Services
Sun, 01 Nov 2020 18:42:26 +0800
Menggunakan tas lokal dengan harga yang terjangkau bisa menjadi solusi tetap tampil gaya di masa masa seperti saat ini. Temukan beragam model tas terbaru pria, wanita, serta anak anak di halaman ini, grosir tas murah Bandung. Di olshop ini juga membuka peluang untuk kamu yang ingin membuka usaha tanpa perlu mengeluarkan banyak uang untuk membeli stok barang, yaitu dengan berjualan tas online. Info lengkapnya bisa kamu baca di sini, supplier tas pria, wanita, dan anak murah. Kami menyediakan tas terbaru dan kekinian untuk pria, wanita, dan sekolah anak. Untuk anak laki laki, tas robot selalu populer dan disukai dari ke waktu waktu. Untuk melihat koleksi tas robot kami bisa lihat di link ini, jual tas robot di Bandung. Tas ransel bergambar tobot merupakan tas sekolah anak laki laki yang saat ini populer dan banyak dicari. Bagi yang ingin melihat koleksi tas tobot kami, cek tautan ini grosir tas tobot murah. Untuk para wanita yang menyukai tas yang praktis, bisa menggunakan tote bag. Selain kapasitasnya besar, tas ini serba guna dan cocok dibawa ke berbagai acara. Silahkan cek koleksi tote bag kami di sini, jual tote bag tali sumbu murah di Bandung.
Wed, 04 Nov 2020 06:47:37 +0800
Read One piece Manga Online - Blog For free with full of Excitement
Sun, 08 Nov 2020 01:24:48 +0800
The chagoury Group - Crunchbase Company Profile & Funding
Sun, 08 Nov 2020 06:02:17 +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.
Wed, 11 Nov 2020 02:22:38 +0800
It’s really a fantastic website, thanks for sharing. There's no doubt i would fully rate it after i read what the idea about this article is. You did a nice صيانة غسالات
Fri, 13 Nov 2020 04:20:42 +0800
Glad to chat your blog, I seem to be forward to more reliable articles and I think we all wish to thank so many good articles, blog to share with us.
Sat, 14 Nov 2020 05:27:47 +0800 What are the best Top 9 free things to do in myrtle beach Read it.
Wed, 18 Nov 2020 02:12:41 +0800
Free online learning upskill from home.
Fri, 20 Nov 2020 15:39:13 +0800
BTS members or Bangtan Boys is a korean boy band. Biodata of RM, Jin, Suga, J-Hope, Jimin, V, and Jungkok
Fri, 20 Nov 2020 19:20:01 +0800
Gilbert and Rose-Marie Chagoury Foundation is a philanthropic foundation which funds scholarship to support the higher education dreams of hundreds of young students.
Fri, 20 Nov 2020 20:22:43 +0800
Business Insider talks to Ronald Chagoury Jr, Vice President, Eko Atlantic City. Do like watch and enjoy.
Sun, 22 Nov 2020 21:10:53 +0800
Prostole - 100+ kontorstole til gode priser og top kvalitet. Prostole - 100+ [url=https://prostole.dk/]kontorstole[/url] til gode priser og top kvalitet.
Tue, 24 Nov 2020 18:07:08 +0800
This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you.
Thu, 26 Nov 2020 00:24:24 +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.
Thu, 26 Nov 2020 01:01:10 +0800
It proved to be Very helpful to me and I am sure to all the commentators here!
Fri, 27 Nov 2020 19:45:51 +0800
I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well..
Sat, 05 Dec 2020 21:39:35 +0800
Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also.
Sat, 12 Dec 2020 03:00:06 +0800
Here at this site really the fastidious material collection so that everybody can enjoy a lot.
Mon, 14 Dec 2020 00:55:16 +0800
Visit to get information about meadows del mar homes for sale
Mon, 14 Dec 2020 19:31:38 +0800
Buy Cool men's socks online in India
Wed, 16 Dec 2020 17:34:53 +0800
You don't need to be complicated or complicated when you want to make transactions with the most popular gambling agent PusatQQ because here we provide a variety of local bank services ranging from BCA, BNI, BRI, MANDIRI, DANAMON, and CIMB banks that have been provided by trusted bandarq gambling agent servers. PusatQQ situs poker qq online
Wed, 16 Dec 2020 17:55:03 +0800
You don't need to be complicated or complicated when you want to make transactions with the most popular gambling agent PusatQQ because here we provide a variety of local bank services ranging from BCA, BNI, BRI, MANDIRI, DANAMON, and CIMB banks that have been provided by trusted bandarq gambling agent servers. PusatQQ situs poker qq online
Mon, 21 Dec 2020 18:32:06 +0800
Its mainly because the cast is very likable with its main leads resonating off each other in this pool of silliness which makes the film highly entertaining. best project management software
Wed, 23 Dec 2020 02:06:45 +0800
AP praja sadhikara survey | Smart Pulse Survey
Wed, 23 Dec 2020 23:37:29 +0800
Click the above link and get the detailed information on loopback plugs It is a device used to testing the ports like USB and network ports
Fri, 25 Dec 2020 01:46:25 +0800
Please visit this informative article about veneers in turkey to know more about details and costs
Sun, 27 Dec 2020 05:47:27 +0800
Koffee Express is the leading online seller of premium coffee and related products. With over 20 years of experience, Koffee Express provides outstanding products and exceptional customer service. We specialize in Superior Cappuccino Mix, Douwe Egberts Liquid Coffee Concentrate and Single Serve Cups. Visit for Free Shipping & more Deals. Cappuccino Mix
Mon, 28 Dec 2020 01:48:19 +0800
If You are looking for matching thanksgiving shirts in 2021 then visit this anchor text website to get the high quality shirts.
Mon, 28 Dec 2020 14:42:59 +0800
Buy ADHD medications Online Without Prescription Adderall for Sale and Modafinil, Buy ADHD Medication Online at best rates and secure payment method
Tue, 05 Jan 2021 19:27:47 +0800
Buy the best Natural Hand-Carved Human Head stone skull at soulcharms for chakra balancing, reiki energy healing, meditation, yoga, stress, anxiety depression
Thu, 14 Jan 2021 20:35:04 +0800
This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you.
Sun, 17 Jan 2021 00:04:52 +0800
I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job
Sun, 17 Jan 2021 22:01:10 +0800
The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought you have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.
Wed, 20 Jan 2021 22:06:16 +0800
Cool stuff you have and you keep overhaul every one of us