char s[] = "wangshihui";

char *s = "wangshihui";

皆宣告了s字符串,在C-style string的函数皆可使用,但两者背后意义却不相同。

char s[] = "wangshihui";   s指向栈内存

的s是个char array,含11个byte(包含结尾\0),"wangshihui"对s来说是initializer,将字符一个一个地copy进s阵列。

char *s = "wangshihui";   s指向文字常量区,指向的内容为const  相当于const char *s="wangshihui"

的s是一个pointer指向char,由于"wangshihui"本身就是一个string literal,所以s指向"wangshihui"这个string literal的起始存储器位置。

char s1[] = "wangshihui";

char *s2 = "wangshihui";

cout << "size of s1: " << sizeof(s1) << endl;

cout << "size of s2: " << sizeof(s2) << endl;

s1是字符串数组,所以占了11byte(包含字符串结束标志);

而s2只是pointer,所以占了4 byte

实际使用有什么不同吗?两种写法皆可使用substring和pointer写法,但只有char *s可以直接使用*s++写法。

char s[]为阵列,虽然s == &s[0],但s是『常数』,恒等于&s[0]无法改变,但char *s为pointer,指向s[0],但却是变量,可以任意改变,故可用*s++任意更改pointer值。

Conclusion

一般人很少分辨char s[]和char *s的差异,大部分状况下用法相同,但char *s速度略快,因为不需copy的动作,且*s++为C语言常用的写法。

  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. char s1[] = "wangshihui";
  8. char *s2 = "wangshihui";
  9. cout << "size of s1: " << sizeof(s1) << endl;
  10. cout << "size of s2: " << sizeof(s2) << endl;
  11. cout<<"\n----------\n";
  12. for(int i = 0; i != sizeof(s1)-1; ++i)
  13. {
  14. cout << s1[i];
  15. }
  16. cout<<"\n----------\n";
  17. for(int i = 0; i != strlen(s2); ++i)
  18. {
  19. cout << *(s2 + i);
  20. }
  21. cout<<"\n----------\n";
  22. while(*s2)
  23. cout << *s2++;
  24. cout<<"\n----------\n";
  25. /************
  26. while(*s1)//编译不通过:lvalue required as increment operand|
  27. cout << *s1++;
  28. *************/
  29. }
  30. /************************
  31. size of s1: 11
  32. size of s2: 4
  33.  
  34. ----------
  35. wangshihui
  36. ----------
  37. wangshihui
  38. ----------
  39. wangshihui
  40. ----------
  41.  
  42. **************************/

数组字符串与指针字符串的区别 char s[]="***" 和char *s="***"的区别的更多相关文章

  1. c语言指针字符串与字符数组字符串的区别

    #include <stdio.h> int main() { //字符串常量,存放于内存常量区. //常量区区的内存具有缓存机制, //当不同指针指向的常量值相同时, //其实这些指针指 ...

  2. C语言之数组,字符串,指针

    一. 数组的定义 1.  数组初始化 初始化方式 int a[3] = {10, 9, 6}; int a[3] = {10,9}; int a[] = {11, 7, 6}; int a[4] = ...

  3. 字符数组,字符指针,字符串常量,以及sizeof的一些总结

    1.以字符串形式出现的,编译器都会为该字符串自动添加一个\0作为结尾 如在代码中写"abc",编译器帮你存储的是"abc\0". 2.数组的类型是由该数组所存放 ...

  4. 「C」 数组、字符串、指针

    一.数组 (一)数组 概念:用来存储一组数据的构造数据类型 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. (二)数组的定义 格式: 类型 数组名[元素个数 ...

  5. 【C++基础】 指针&字符串&数组

    先贴代码,总结以后再写,和5中内存分配方式密切相关 PS: str 返回整个字符串,str[0],*str返回首字符h char *strA(){ char str[]="hello!&qu ...

  6. C语言程序设计--字符串与指针及数组与指针

    数组的基本知识 数组的定义 #define SIZE 5 int array_int[5]; //未声明初始化,默认填零 float array_float[5] = {1.01, 2.23, 3.1 ...

  7. C++ 字符串、string、char *、char[]、const char*的转换和区别

    1.字符串 字符串本质就是一串字符,在C++中大家想到字符串往往第一反应是std::string(后面简称string) 字符串得从C语言说起,string其实是个类,C语言是没有class的,所以C ...

  8. 指向字符串的指针在printf与cout区别

    根据指针用法: * 定义一个指针, &取变量地址, int b = 1; int *a = &b; 则*a =1,但对于字符串而言并非如此,直接打印指向字符串的指针打印的是地址还是字符 ...

  9. c语言实现字符指针(字符串)数组的排序

    需求: "ff555d", "114ddd", "114dd","aaa", "aaab", &qu ...

随机推荐

  1. spring05配置文件之间的关系

    一:配置文件包含关系 1.创建对应的实体类 public class Student { //学生实体类 private String name; //姓名 private Integer age; ...

  2. Android开发环境搭建简介

    Android的开发工具,可以使用Eclipse,Idea,Android Studio,其中Eclipse是开源中国大部分使用的IDE,Idea是号称最好用的开发工具,有很多用处,Android S ...

  3. pt-query-digest 安装及使用

    打个草稿 介绍:pt-query-digest 可用于mysql的慢查询的日志分析,分析统计出每种慢查询的基本信息,如响应时间.最大执行时间.最小执行时间.执行时间的中位数等.(当然不只是这个功能) ...

  4. php pdo_mysql使用方法

    <?php $dsn='mysql:host=127.0.0.1;port=3306;dbname=bisai'; $username='root'; $password=''; $driver ...

  5. js获取url?后的参数

    function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new O ...

  6. part3

    我就不刁旭概念什么的问题了哈~直接进入工作中所通用的一些有关object的方法和属性 1).for/in 属性的遍历~他遍历的对象没有特定的顺序,而且只能枚举出所有用户自定义的属性,不能枚举出某些预定 ...

  7. Array and its point.

    a is the array name. &a is the ponit of 2-D array which contains a[5]. the type of &a should ...

  8. csv内存流文件流

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  9. Ubuntu 下升级git到最新版

    $ sudo add-apt-repository ppa:git-core/ppa $ sudo apt-get update $ sudo apt-get install git

  10. 如何判断一个C++对象是否在堆栈上(通过VirtualQuery这个API来获取堆栈的起始地址,然后就可以得到答案了),附许多精彩评论

      昨天有人在QQ群里问到如何判断一个C++对象是否在堆栈上, 我在网上搜索了下, 搜到这个么一个CSDN的帖子http://topic.csdn.net/t/20060124/10/4532966. ...