数组字符串与指针字符串的区别 char s[]="***" 和char *s="***"的区别
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语言常用的写法。
#include <iostream>
#include <cstring>
using namespace std; int main()
{
char s1[] = "wangshihui";
char *s2 = "wangshihui";
cout << "size of s1: " << sizeof(s1) << endl;
cout << "size of s2: " << sizeof(s2) << endl;
cout<<"\n----------\n";
for(int i = 0; i != sizeof(s1)-1; ++i)
{
cout << s1[i];
}
cout<<"\n----------\n";
for(int i = 0; i != strlen(s2); ++i)
{
cout << *(s2 + i);
}
cout<<"\n----------\n";
while(*s2)
cout << *s2++;
cout<<"\n----------\n";
/************
while(*s1)//编译不通过:lvalue required as increment operand|
cout << *s1++;
*************/
}
/************************
size of s1: 11
size of s2: 4 ----------
wangshihui
----------
wangshihui
----------
wangshihui
---------- **************************/
数组字符串与指针字符串的区别 char s[]="***" 和char *s="***"的区别的更多相关文章
- c语言指针字符串与字符数组字符串的区别
#include <stdio.h> int main() { //字符串常量,存放于内存常量区. //常量区区的内存具有缓存机制, //当不同指针指向的常量值相同时, //其实这些指针指 ...
- C语言之数组,字符串,指针
一. 数组的定义 1. 数组初始化 初始化方式 int a[3] = {10, 9, 6}; int a[3] = {10,9}; int a[] = {11, 7, 6}; int a[4] = ...
- 字符数组,字符指针,字符串常量,以及sizeof的一些总结
1.以字符串形式出现的,编译器都会为该字符串自动添加一个\0作为结尾 如在代码中写"abc",编译器帮你存储的是"abc\0". 2.数组的类型是由该数组所存放 ...
- 「C」 数组、字符串、指针
一.数组 (一)数组 概念:用来存储一组数据的构造数据类型 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. (二)数组的定义 格式: 类型 数组名[元素个数 ...
- 【C++基础】 指针&字符串&数组
先贴代码,总结以后再写,和5中内存分配方式密切相关 PS: str 返回整个字符串,str[0],*str返回首字符h char *strA(){ char str[]="hello!&qu ...
- C语言程序设计--字符串与指针及数组与指针
数组的基本知识 数组的定义 #define SIZE 5 int array_int[5]; //未声明初始化,默认填零 float array_float[5] = {1.01, 2.23, 3.1 ...
- C++ 字符串、string、char *、char[]、const char*的转换和区别
1.字符串 字符串本质就是一串字符,在C++中大家想到字符串往往第一反应是std::string(后面简称string) 字符串得从C语言说起,string其实是个类,C语言是没有class的,所以C ...
- 指向字符串的指针在printf与cout区别
根据指针用法: * 定义一个指针, &取变量地址, int b = 1; int *a = &b; 则*a =1,但对于字符串而言并非如此,直接打印指向字符串的指针打印的是地址还是字符 ...
- c语言实现字符指针(字符串)数组的排序
需求: "ff555d", "114ddd", "114dd","aaa", "aaab", &qu ...
随机推荐
- 执行oracle函数的四种方法
1.在定义函数时:如果有参数,则参数可有类型但是不加长度. 2.在执行函数: var/variable var_name var_type(如果数据类型是number则没有长度,如果数据类型是varc ...
- 使用UILocalNotification给App添加本地消息通知
使用过的代码,直接贴上 UILocalNotification *notification = [[UILocalNotification alloc] init]; if (notification ...
- c#geckofx文件流下载
备注:内容仅提供参考. ⒈添加引用:using Gecko; ⒉然后根据自己的情况在某个方法内添加事件: LauncherDialog.Download += new EventHandler< ...
- php中双$$与多$$
<?php$a="b";$b="bbb";$c="ccc";echo $$a;?> 输出结果bbb $a的值为b $$a不是输出 ...
- 武汉科技大学ACM :1001: A+B for Input-Output Practice (I)
Problem Description Your task is to Calculate a + b. Too easy?! Of course! I specially designed the ...
- Leetcode 371: Sum of Two Integers(使用位运算实现)
题目是:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...
- mysql-1862、1820、java.sql.SQLException: Your password has expired. To log in you must change it using a client that supports expired passwords.
之前一直运行的好好的,突然mysql就无法工作了.请求命令后报错误:ERROR 1820 (HY000): You must SET PASSWORD before executing this st ...
- install pip3 for python 3.x
前言: 我目前使用的服务器为centos6.x 系统自带的python的版本为2.6.x,但是目前无论是学习还是使用python,python3都是首选,那么问题来了.---如何安装python3环境 ...
- html文字有光晕
<style> .tb{ font-size:40px; filter:glow(color=pink,direction=); font-family:华文行楷; } </styl ...
- debug 输出 以及宏定义--备
使用NSLog的一个风险是:它的运行会占用时间和设备资源. 所以在编译版本前一定不要有nslog. 同时当你的工程中有很多log 输出的时候 查找起来很不方便 ,下面介绍一种方法 可以使我们事半功倍. ...