经典代码,解释指针步长问题
#include <iostream> using namespace std; int main() { char *p1 = "hello"; char *p2 = "world"; char *p3 = "a piece of cake"; char *str[] = { p1, p2, p3 }; cout << sizeof(*str[0]) << " " << typeid(str[0]).name() << " " << str[0] + 1<< endl;//typeid是类型 cout << sizeof(*&str[0]) << " " << typeid(&str[0]).name() << " " << *(&str[0] + 1) << endl; cout << sizeof(*str) << " " << typeid(str).name() << " " << *(str + 1) << endl; cout << sizeof(*&str) << " " << typeid(&str).name() << " " << *(&str + 1) << endl; return 0; }
能看懂这个你就知道了,这个地方+1的时候都是说步长,步长就是说+1前面的这个对象 所指向的 数据类型的长度,比如 &str[0]类型是
char
* * 所指向的是
char
* 长度是指针的长度(不同机器不同)
posted on 2016-07-13 10:03 笑对人生Charles 阅读(1451) 评论(0) 编辑 收藏 举报