char s[]字串和char *s字串有什麼差別? (C/C++) (C)

Abstract
C語言有兩種字串宣告方式char s[]和char *s,兩者有什麼差異呢?

Introduction

char  s[]  =   " Hello World " ;
char   * s   =   " Hello World " ;


皆宣告了s字串,在C-style string的函數皆可使用,但兩者背後意義卻不相同。

char  s[]  =   " Hello World " ;


的s是個char array,含12個byte(包含結尾\0),"Hello World"對s來說是initializer,將字元一個一個地copy進s陣列。

char   * =   " Hello World " ;


的s是一個pointer指向char,由於"Hello World"本身就是一個string literal,所以s指向"Hello World"這個string literal的起始記憶體位置。

做個簡單的實驗證明兩者不同

 1 #include  < iostream >
 2
 3 using   namespace  std;
 4
 5 int  main()  {
 6 char s1[] = "Hello World";
 7 char *s2  = "Hello World";
 8 
 9 cout << "size of s1: " << sizeof(s1) << endl;
10 cout << "size of s2: " << sizeof(s2) << endl;
11}


執行結果

size of s1:  12
size of s2: 
4


s1是陣列,所以占了12 byte,而s2只是pointer,所以占了4 byte,實驗結果與預期相同。

實際使用有什麼不同嗎?兩種寫法皆可使用substring和pointer寫法,但只有char *s可以直接使用*s++寫法。

char s[]

 1 #include  < iostream >
 2
 3 using   namespace  std;
 4
 5 int  main()  {
 6 char s[] = "Hello World";
 7 
 8 for(int i = 0; i != 11++i) {
 9  cout << s[i];
10 }

11 cout << endl;
12 
13 for(int i = 0; i != 11++i) {
14  cout << *(s + i);
15 }

16 cout << endl;
17}

18


執行結果

Hello World
Hello World


char *s

 1 #include  < iostream >
 2
 3 using   namespace  std;
 4
 5 int  main()  {
 6 char *= "Hello World";
 7 
 8 for(int i = 0; i != 11++i) {
 9  cout << s[i];
10 }

11 cout << endl;
12 
13 for(int i = 0; i != 11++i) {
14  cout << *(s + i);
15 }

16 cout << endl;
17}


執行結果

Hello World
Hello World


但卻只有char *s可以使用*s++寫法。

 1 #include  < iostream >
 2
 3 using   namespace  std;
 4
 5 int  main()  {
 6 char *= "Hello World";
 7 
 8 while(*s) 
 9  cout << *s++;
10}

11
12


執行結果

Hello World


理由何在呢?

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語言常用的寫法,只有char *s才支援。

Reference
C Primer Plus 5/e中文版 p.480

从键盘输入一个长度不超过100个字符的字符串,然后做如下操作: (1)将字串中的小写字母转为大写,大写字母转为小写,而其它字符不作处理。 (2)将字串输出保存到一个名为“ex801.txt”的文本文件中。注:文本文件ex801.txt应与源码文件ex801.c保存在同一个文件夹中。 目前,已编写完成main函数,请用C++编程实现writeToFile函数,具体功能和要求如下所示。 /* @Filename: ex801.c @Author: Ju Chengdong @Version: 1.0 @Date: 2021-03-18 @Description: File Character Reading and Writing */ #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc,char *argv[]){ /*(1)声明函数及变量*/ int writeToFile(char *str, char *fileName, char *mode); char str[100]; char fileName[] = "ex801.txt"; /*(2)获取键盘输入字串*/ fgets(str, 100, stdin); //gets(str); //将回车看作字串输入结束标志,字串中可以有空格 //scanf("%s", str); //将空格看作字串输入结束标志,字串中不能有空格 /*(3)将字串写入文件*/ int charNum = writeToFile(str, fileName, "w"); if(charNum < 0){ //printf("write error");//用于调试 return -1; } return 0; } /* * 函数名称:writeToFile * 函数功能:将字串写入文件 * 形式参数:char *str,一维字符数组(字符串)首地址 * 形式参数:char *fileName,待写入的文件路径及名称 * 形式参数:char *mode,文件使用方式 * 返 回 值:int型,若文件打开异常,返回 -1;否则返回写入到文件的字符数 */ int writeToFile(char *str, char *fileName, char *mode){ // 请编程实现本函数 } 其他说明:无 【源文件名】ex801.c 【输入形式】标准输入:从键盘任意输入不超过100个字符的字串 【输出形式】文件输出:将字串转换后输出到文件
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值