C/C++混淆点-字符串指针
c語言中沒有字符串的類型,
所以對(duì)字符串操作,有兩種形式:可以用字符指針,或者字符串?dāng)?shù)組(這里的指針變量c, 系統(tǒng)會(huì)為其重新分配內(nèi)存)
(1)用字符數(shù)組存放一個(gè)字符串
????????char string[]="Linux C";
????????printf("%s\n",string);
????????string是一個(gè)字符數(shù)組名,它同時(shí)也是該字符數(shù)組的首地址。
??(2)用字符串指針來指向字符串
????????如果不想定義字符數(shù)組,就可以只用一個(gè)指向字符串的指針,叫“字符串指針”,例如:
????????char *p="Linux C";
????????printf("%s\n",p);
????????“Linux C”是一個(gè)字符串常量。C語言對(duì)于字符串常量的處理方法:在內(nèi)存中開辟一個(gè)字符數(shù)組來存儲(chǔ)該字符串常量,并把開辟出的字符數(shù)組的首地址賦給p。
遍歷字符串代碼:
#include<stdio.h> #include<string.h>void main() {char str[] = "hello world";int len = strlen(str), i;for (i = 0; i < len; i++) {printf("%c", str[i]);}printf("\n");char *p = str;for (i = 0; i < len; i++) {printf("%c", *(p++));}printf("\n");for (i = 0; i < len; i++) {printf("%c", *(str + i));}system("pause"); } /* 運(yùn)行結(jié)果: hello world hello world hello world */字符數(shù)組歸根結(jié)底還是一個(gè)數(shù)組,上面中定義的字符串?dāng)?shù)組,str也可以認(rèn)為是一個(gè)指針,指針以及數(shù)組方面操作均適用。
除了字符數(shù)組外,C語言還支持直接使用一個(gè)指針指向字符串的方式來表示字符串,代碼如下:
char *str = "hello C language";
以此方式定義的字符串,同樣可以對(duì)此字符串進(jìn)行多樣的操作。為什么可以用字符數(shù)組表示字符串,非要弄個(gè)指針來表示字符串呢?這其中定有深意。原來它們最根本的區(qū)別是在內(nèi)存中的存儲(chǔ)區(qū)域不一樣,字符數(shù)組存儲(chǔ)在全局?jǐn)?shù)據(jù)區(qū)或棧區(qū),而以指針形式表示的字符串卻存儲(chǔ)在常量區(qū)。全局?jǐn)?shù)據(jù)區(qū)和棧區(qū)的字符串(也包括其他數(shù)據(jù))有讀取和寫入的權(quán)限,而常量區(qū)的字符串(也包括其他數(shù)據(jù))只有讀取權(quán)限,沒有寫入權(quán)限。一句話概括:數(shù)組形字符串存放在全局?jǐn)?shù)據(jù)區(qū)或棧區(qū),可讀可寫。指針字符串存放在常量區(qū),只讀不能寫。
驗(yàn)證了https://www.cnblogs.com/kelamoyujuzhen/p/10454129.html中的說法
參考自https://blog.csdn.net/u013812502/article/details/81196367
拷貝字符串方式
#include <stdio.h>int main() {char a[] = "Linux C Program", b[20], c[20], d[20];int i;//(1)數(shù)組for (i = 0; a[i] != '\0'; i++)b[i] = a[i];b[i] = '\0';//(2)以數(shù)組首地址做基準(zhǔn)的指針運(yùn)算for (i = 0; *(a + i) != '\0'; i++)*(c + i) = *(a + i);*(c + i) = '\0';//(3)用字符串指針char *p1, *p2;p1 = a;p2 = d;for (; *p1 != '\0'; p1++, p2++)*p2 = *p1;*p2 = '\0';printf("%s\n", a);printf("%s\n", b);printf("%s\n", c);printf("%s\n", d);return 0; } /* 運(yùn)行結(jié)果: Linux C Program Linux C Program Linux C Program Linux C Program */參考自https://blog.csdn.net/str999_cn/article/details/78596812
練習(xí):
#include<iostream> #include<string> using namespace std; //c語言中沒有字符串的類型, //所以對(duì)字符串操作,有兩種形式:可以用字符指針,或者字符串?dāng)?shù)組(這里的指針變量c, 系統(tǒng)會(huì)為其重新分配內(nèi)存。) //c++編譯器把char* 當(dāng)成c語言字符串 //編譯器把char*當(dāng)成數(shù)組 //編譯器直接把char* p[]當(dāng)成字符串?dāng)?shù)組 void test01() {char ch[] = "word";char* p = ch;std::cout << *p << std::endl;// 輸出:wstd::cout << p << std::endl;// 輸出:word }void test02() {char ch = 's';char* p = &ch;std::cout << *p << std::endl;//輸出sstd::cout << p << std::endl;//有時(shí)候會(huì)輸出亂碼,//因?yàn)榫幾g器把char*當(dāng)成數(shù)組,會(huì)從字符開始輸出直至遇到一個(gè)空字符 }void test03() {char c[10] = "hello";//字符數(shù)組,也即是C風(fēng)格字符串char ca[] = "hello world";char ch[] = "word";char* p = ch;//這里表明char* 其實(shí)是字符數(shù)組char* pc[] = { c,ca,p };//正確賦值,數(shù)組名也是指針(指向數(shù)組首元素)std::cout << p << std::endl;//wordstd::cout << pc[0] << std::endl;//hellostd::cout << pc[1] << std::endl; //hello worldstd::cout << pc[2] << std::endl;//word}void test04() {string s = "string";string *p = &s;char * c = "hello";cout << *c << endl; //hcout << c << endl; //hellocout << s << endl; //stringcout << *p << endl; //string //*p 解引用輸出的是整個(gè)字符串//而 p 輸出的是字符串的首地址cout << p << endl; //000000565A8FFA78 } int main() {//test01();//test02();//test03();test04();return 0;}參考自https://blog.csdn.net/holyweng/article/details/82121436
char *s=NULL ;char *ss= "123";int len = strlen(ss);s = (char*)malloc(len + 1); //strlen長度計(jì)算不包括'\0' 所以此處加一strcpy(s, ss);cout << s << endl; //123strcpy的使用必須是在兩個(gè)char * 之間
總結(jié)
以上是生活随笔為你收集整理的C/C++混淆点-字符串指针的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 卷积核旋转180度理解
- 下一篇: C++基础16-类和对象之联编,重写,虚