網(wǎng)站設(shè)置安全,哈爾濱seo優(yōu)化,如何制作短視頻教程,網(wǎng)站建設(shè)學(xué)習(xí)學(xué)校文章目錄 C語言開發(fā),指針進(jìn)階。1.字符串與指針的關(guān)系2.指針獲取字符串具體內(nèi)容3.字符串比較,查找,包含,拼接4.字符串大小寫 C語言開發(fā),指針進(jìn)階。
1.字符串與指針的關(guān)系
//
// Created by MagicBook on 2023-10-22.
…
文章目錄
C語言開發(fā),指針進(jìn)階。
1.字符串與指針的關(guān)系
2.指針獲取字符串具體內(nèi)容
3.字符串比較,查找,包含,拼接
4.字符串大小寫
C語言開發(fā),指針進(jìn)階。
1.字符串與指針的關(guān)系
//// Created by MagicBook on 2023-10-22.//#include<stdio.h>intmain11(){char str[]={'d','1','s','\0'};//str數(shù)組的字符串是存在全局區(qū),靜態(tài)區(qū)域,修改的時(shí)候拷貝一份到main函數(shù)的棧區(qū),再進(jìn)行修改操作str[1]='a';//printf遇到\0結(jié)束printf("%s\n", str);//開辟內(nèi)存地址,指向靜態(tài)區(qū)域里面的aaaa,指針不能修改靜態(tài)全局區(qū)域內(nèi)的字符串內(nèi)容,char*str2 ="aaaa";//崩潰str2[1]='2';return0;}
2.指針獲取字符串具體內(nèi)容
//// Created by MagicBook on 2023-10-22.//#include<stdio.h>intmain11(){char str[]={'d','1','s','\0'};//str數(shù)組的字符串是存在全局區(qū),靜態(tài)區(qū)域,修改的時(shí)候拷貝一份到main函數(shù)的棧區(qū),再進(jìn)行修改操作str[1]='a';//printf遇到\0結(jié)束printf("%s\n", str);//開辟內(nèi)存地址,指向靜態(tài)區(qū)域里面的aaaa,指針不能修改靜態(tài)全局區(qū)域內(nèi)的字符串內(nèi)容,char*str2 ="aaaa";//崩潰str2[1]='2';return0;}
//// Created by MagicBook on 2023-10-22.//#include<stdio.h>intgetLen(char*string){int cnt =0;//移動指針 ,不等于'\0',一直循環(huán),while(*string){string++;cnt++;}return cnt;}//數(shù)組作為參數(shù)傳遞,會把數(shù)組優(yōu)化成指針voidgetLen2(int*res,int arr[]){/* int len = sizeof(arr) / sizeof(int);printf("%d\n", len);*/int cnt =0;while(*arr){arr++;cnt++;}*res = cnt;}intmain(){char str[]={'d','1','s','\0'};int len =getLen(str);printf("%d\n", len);int arr[]={1,2,3,'\0'};int len2 =sizeof(arr)/sizeof(int);int res;getLen2(&res, arr);printf("%d\n", len2);return0;}
3.字符串比較,查找,包含,拼接
//// Created by MagicBook on 2023-10-22.//#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){//字符串轉(zhuǎn)換char*aaa ="1222";int res =atoi(aaa);//true,不等于0,if(res){printf("%d\n", res);}else{printf("----");}//doubledouble res2 =atof(aaa);printf("%lf\n", res);//字符串比較char*string1 ="aaa";char*string2 ="aaasss";//區(qū)分大小寫int ress =strcmp(string1, string2);//不區(qū)分大小寫int resss =stricmp(string1, string2);//0是相等,非0不相等if(!ress){printf("yes");}else{printf("no");}return0;}
//// Created by MagicBook on 2023-10-22.//#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){char*aa ="1234";char*aaa ="1";char*test =strstr(aa, aaa);//非NULL,進(jìn)ifif(test){printf("%s\n", test);}else{}//包含if(test){}else{}//取位置int index = test - aa;//拼接字符串char test1[22];char*a1 ="11",*a2 ="22",*a3 ="33";//先拷貝到數(shù)組容器中,再拼接字符串strcpy(test1, a1);strcat(test1, a2);strcat(test1, a3);return0;}
4.字符串大小寫
//// Created by MagicBook on 2023-10-22.//#include<stdio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>voidlow(char*b,char*c){while(*c){*b =tolower(*c);//移動指針c++;//一邊移動一邊存儲。b++;}*b ='\0';}intmain(){//都變成小寫char*a ="qDHaAA";//char test[20];low(test, a);return0;}