码迷,mamicode.com
首页 > 其他好文 > 详细

字符串指针比较

时间:2017-09-16 13:26:34      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:安全   不用   new   char   ast   printf   c++   ble   pre   

 1 char str1[] = "123";
 2 char str2[] = "123"; //1
 3 const char str3[] = "123";
 4 const char str4[] = "123"; //1
 5 char *str5 = "123";
 6 char *str6 = "123"; //0
 7 const char *str7 = "123";
 8 const char *str8 = "123"; //0
 9 cout << (str7!= str8) << endl;
10 cout << static_cast<void*>(str5) << endl; //输出字符指针地址
11 cout << static_cast<const void*>(str7) << endl;
12 printf("%p,\n",str8);

double*转换为int*后移动距离

1 A *p = new A;
2 double *p2 = new double[2];
3 p2[0] = 1; p2[1] = 2;
4 int *q = (int*)(p2);
5 printf("%p,%p,%p\n", q, q + 1, p2);
6 printf("%d,%d", q[0], q[1]);

c旧式转换 double *可以转换为int*,用static_cast<int *>会直接提示语法错误,都不用编译报错,所以 C++风格的转换更安全;

字符串指针比较

标签:安全   不用   new   char   ast   printf   c++   ble   pre   

原文地址:http://www.cnblogs.com/hchacha/p/7530711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!