© / Posted in 知识如海 / September 7, 2009

const char * 转换为 char *
指向const的指针不能被赋给指向非const的指针,所以应该用strcpy,也就是另开一块内存,把字符一个个复制过去

const char *expr = "goodidea";

char *buf = new char[strlen(expr)+1];
strcpy(buf, expr);


strcpy
原型:extern char *strcpy(char *dest,char *src);
用法:#include <string.h>
功能:把src所指由NULL结束的字符串复制到dest所指的数组中。
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。

 

附:指针常量,常量指针

什么是指针常量?指针常量即指针类型的常量。

例:char *const name1="John";

    name1="abc"; //错误,name1指针,不能变,一个指针类型的变量,存放的是地址,所以不能把'"abc"的地址赋给name1
    char * name2= name1; //可以

什么是常量指针?常量指针即是指向常量的指针,指针的值可以改变,指针所指的地址中的内容为常量不能改变,

例:const char *name1="John";

    char s[]="abc"; name1=s; //正确,name1存放的地址可以改变
   
    char * name2= name1; //不可以,因为name2 和 name1存放的是同一块地址,如果name2地址中的内容改了,则name1的内容也改了,那么name1就不再是指向常量的指针了。
  

一句话,靠近哪个哪个不能改变!

本文有 13 篇评论 ↓↓

    1. 不懂 -----------------
      我闪

    2. 俺是不是也应该好好学习学习c++了

    3. 门外汉

    4. @就是不填
      @仁心博客
      呵呵,用不着的话就不用懂啊

    5. 阅读,并送ip。

    6. O(∩_∩)O~完全不懂

    7. 我觉得我对这个很了解。
      const char * 是 const char 的指针,指针的本身是可变的,指向的对象是不可变的。
      char const * 是 char 的指针,指针的本身是常量,在定义后就不可以指向另外的对象(当然有一种方法可以的),但是指向的对象是可变的。
      建议博主去了解一下 static_cast,它可以做到这些事情(如果我没有记错):
      char *p = new char[256];
      const char *q = p;

      char *fake = static_cast(q);
      fake[0] = 'a';
      // ...

      1. @Euyuil, 指针常量和常量指针在文中也有说明了~~而你所说的static_cast是C++的强制类型转换,文中的是C的

    8. 你博客把我的大于号和小于号屏蔽了,上面的某行改成:
      char *fake = static_cast<char *>(q);
      我只能用中文符号代替……

    9. 麻烦了……C和C++我已经不分了……

      1. @Euyuil, 呵呵,要求不严格,自己写些小程序的时候,不分也无所谓~~只要支持就行~~

    10. 对了……不应该是static_cast,应该是const_cast,太顺手……


    11. welcome to our burberry outlet store ,there are many fashion and hight quality burberry bags wait for

      you!
      do you want to buy burberry bags for yourslf or your beat friends in sometimes?

      please come to our burberry outlet store.
      our burberry outlet store offer many new style burberry watches for you.do

      not miss the fashion burbery Burberry T-shirts.
      In burberry sunglasses stores it really is typically some sort of

      most effective style which precisely the top notch and several middle-class are typically in a region to afford
      Fouthy-six Putting on a new burberry watches for men and relish the

      summertime Does one prefers to invest in Burberry bags?


      as we all know the burberry brand is very famouse in the world.in burberry bags outlet store you

      will have a good time!
      there is new store,the name is burberry outlet !you are interested in new store,you will have a good

      time in there.
      If you use burberry bags ,you will feel very Summer feeling, so many girls put it

      down.
      evey women love the burberry shirts in life,the burberry can give you more

      confidence in sometimes.
      Jack bought the burberry sunglasses and the next day he came to the

      interview in the Burberry cheak shirt.


      Join forums that areparticularlymeant to talk about watches. If you are searching for burberry sale for women, then

      obviouslycheck out women's forum.
      One of the most popular British exports is the burberry outlet online .
      The particular burberry bags typically reveal the actual Burberry renowned examine layout

      which has been made popular by the garment range.
      The color of burberry ties are bright but with the characters of mature and steady
      the burberry shoes collection had occipied a large markey share and enjoys a massive

      fan following.
      I believe one's destiny of your burberry wallets long decrease jackets are

      often more large-scale progress.

    添加新评论 ↑↑