首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C/C++三拇指针的“ ++ 与 * “的故事

2012-09-17 
C/C++中指针的“ ++ 与 * “的故事前面写过一篇关于C/C左的文章(http://blog.csdn.net/love_cppandc/article

C/C++中指针的“ ++ 与 * “的故事

前面写过一篇关于C/C++左值的文章(http://blog.csdn.net/love_cppandc/article/details/7782606),那篇文章里提到过i++与++i的区别,但是只是在i为整型时的一些情况。现在,我们讨论一下当指针++与*混用的一些情况。

1. (i)      char *ptr = "Hello";

             printf("%c",*ptr++);

结果为 H。

   (ii)      char *ptr = "Hello";

             printf("%c",*(ptr++));

结果仍为H。

这两种情况等价,原因如下:

*和++都属于二级运算符,为右结合优先,都是先++,后*。

<==> char* temp;

          temp = ptr;

          ptr = ptr + 1;

          return *temp;

2. 

         char *ptr = "Hello";

         printf("%c",*++ptr);

结果为 e。

<==>  ptr = ptr + 1;

           return *ptr;


热点排行