首页
/
每日頭條
/
科技
/
c語言指針數組和字符串比較
c語言指針數組和字符串比較
更新时间:2026-01-13 00:58:16

c語言指針數組和字符串比較?指針變量也是一種變量,占有内存空間,用來保存内存地址測試指針變量占有内存空間大小,接下來我們就來聊聊關于c語言指針數組和字符串比較?以下内容大家不妨參考一二希望能幫到您!

c語言指針數組和字符串比較(C語言中字符串與指針的詳細使用)1

c語言指針數組和字符串比較

指針變量與其指向内存的關系

指針變量也是一種變量,占有内存空間,用來保存内存地址測試指針變量占有内存空間大小。

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> int main0101() { char* p = NULL; char buf[] = "abcde"; printf("p1=%d\n", p); //改變指針變量的值 p = buf; printf("p2=%d\n", p); //指針變量和它指向的内存塊是兩個不同的概念 p = p 1;//改變指針變量的值,即改變了指針的指向 printf("p3=%d\n", p); printf("buf=%s\n", buf); printf("*p=%c\n", *p);//b printf(" 改變指針指向的内存,并不會改變指針的值\n"); buf[1] = '1'; printf("p4=%d\n", p); printf("buf2=%s\n", buf); *p = 'm'; printf("p5=%d\n", p); printf("buf3=%s\n", buf); //寫内存時,一定要确保内存可寫 //char* buf2 = "aaawwweee";//該字符串在文字常量區 不可修改 //buf2[2] = '1';//err char buf3[] = "wwweerrr"; buf3[1] = 's';//ok //不允許向NULL和未知非法地址拷貝内存。 char* p3 = NULL;//err //char* p3 = 0x1111;//err //給p3指向的内存中拷貝字符串 p3 = buf3;//ok strcpy(p3, "123"); return 0; }

修改指針變量的結果

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> int main0201() { char* p = NULL; char* q = NULL; char buf[100] = "asdzcx"; p = &buf[0]; printf("p=%d,%c\n", p,*p); p = &buf[1]; printf("p2=%d,%c\n", p, *p); printf("\n"); for (int i = 0; i < strlen(buf); i ) { p = &buf[i]; printf("p3=%d,%c\n", p, *p); } q = (char*)malloc(100); if (q == NULL) { return -1; } strcpy(q, "qqqwww"); for (int i = 0; i < strlen(q); i ) { p = q i; printf("%c\n", *p); } return 0; }

3.通過指針間接複賦值

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> int get_a() { int a = 10; return a; } void get_a2(int b) { b = 20; } void get_a3(int* p) { *p = 20;//通過*操作内存 } void get_a4(int* a1, int* a2, int* a3, int* a4) { *a1 = 1; *a2 = 2; *a3 = 3; *a4 = 4; } int main0301() { int a = 100; int* p = NULL; //建立關系 //指針指向誰,就将誰的地址賦值給指針 p = &a; //通過*操作内存 *p = 22; /* 通過指針間接賦值 1.兩個變量 2.建立關系 3.通過*操作内存 */ int b = get_a(); printf("b===%d\n", b); get_a2(b); printf("b2===%d\n", b); //如果想通過形參改變實參的值,必須地址傳遞 get_a3(&b);//函數調用時建立關系 printf("b3===%d\n", b); int a1, a2, a3, a4; get_a4(&a1,&a2,&a3,&a4); printf("a1=%d,a2=%d,a3=%d,a4=%d", a1, a2, a3, a4); return 0; } static void fun2(int* p) { p = 0xaabb; printf("fun2:p=%p\n", p); } static void fun3(int** p) { *p = 0xaabb; printf("fun3:p=%p\n", *p); } int main0302() { //一個變量,應該定義一個怎樣類型的指針來保存它的地址 //在原來的基礎上加一個* //int a = 10; //int* p = &a; //int** q = &p; //int********* t = NULL; //int********** t2 = &t; int* p = 0x1122; printf("p1=%p\n", p); fun2(p);//值傳遞 printf("p2=%p\n", p); fun3(&p);//值傳遞 printf("p3=%p\n", p); return 0; }

4.指針作為函數參數的輸入輸出特性

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> int get_a() { int a = 10; return a; } void get_a2(int b) { b = 20; } void get_a3(int* p) { *p = 20;//通過*操作内存 } void get_a4(int* a1, int* a2, int* a3, int* a4) { *a1 = 1; *a2 = 2; *a3 = 3; *a4 = 4; } int main0301() { int a = 100; int* p = NULL; //建立關系 //指針指向誰,就将誰的地址賦值給指針 p = &a; //通過*操作内存 *p = 22; /* 通過指針間接賦值 1.兩個變量 2.建立關系 3.通過*操作内存 */ int b = get_a(); printf("b===%d\n", b); get_a2(b); printf("b2===%d\n", b); //如果想通過形參改變實參的值,必須地址傳遞 get_a3(&b);//函數調用時建立關系 printf("b3===%d\n", b); int a1, a2, a3, a4; get_a4(&a1,&a2,&a3,&a4); printf("a1=%d,a2=%d,a3=%d,a4=%d", a1, a2, a3, a4); return 0; } static void fun2(int* p) { p = 0xaabb; printf("fun2:p=%p\n", p); } static void fun3(int** p) { *p = 0xaabb; printf("fun3:p=%p\n", *p); } int main0302() { //一個變量,應該定義一個怎樣類型的指針來保存它的地址 //在原來的基礎上加一個* //int a = 10; //int* p = &a; //int** q = &p; //int********* t = NULL; //int********** t2 = &t; int* p = 0x1122; printf("p1=%p\n", p); fun2(p);//值傳遞 printf("p2=%p\n", p); fun3(&p);//值傳遞 printf("p3=%p\n", p); return 0; }

5.字符串初始化

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> /* C語言沒有字符串類型,而是通過字符數組模拟 C語言字符串以字符'\0'即數字0結尾 */ int main0501() { //不指定長度,沒有結束符0,有多少個元素就有多長 char buf[] = { 'a','b','c' };//3個元素 printf("buf=%s\n", buf);//不加\0的話 末尾亂碼 //指定長度,後面沒有賦值的元素位置,自動補0 char buf1[100]= { 'a','b','c' }; printf("buf2=%s\n", buf1); //所謂元素都賦值為0 char buf3[100] = { 0 }; //char buf4[2] = { '1','2','3' };//err 數組越界 char buf5[50] = { '1','a','b','0','7' }; printf("buf5=%s\n", buf5); char buf6[50] = { '1','a','b',0,'7' }; printf("buf6=%s\n", buf6);// 1ab char buf7[50] = { '1','a','b','\0','7' }; printf("buf7=%s\n", buf7);// 1ab //使用字符串初始化,常用此類方式 char buf8[] = "qaaasss"; //strlen:測字符串長度,但不包含數字0與字符'\0' //sizeof:測數組長度,包含數字0和字符'\0' printf("strlen=%d,sizeof=%d\n", strlen(buf8), sizeof(buf8));//7 8 char buf9[100] = "qaaasss"; printf("strlen=%d,sizeof=%d\n", strlen(buf9), sizeof(buf9));//7 100 printf("test"); // \012相當于\n char str[] = "\0129"; printf("%s\n", str); return 0; } int main0502() { char buf[] = "aaazzzzssssdddd"; char* p = NULL; //[]方式 for (int i = 0; i < strlen(buf); i ) { printf("%c", buf[i]); } printf("\n"); //指針法 //數組名字,是數組首元素地址 p = buf; for (int i = 0; i < strlen(buf); i ) { printf("%c", p[i]); } printf("\n"); for (int i = 0; i < strlen(buf); i ) { printf("%c",*(p i));//編譯器方式 } printf("\n"); for (int i = 0; i < strlen(buf); i ) { printf("%c", *(buf i)); } printf("\n"); //buf 和 p完全等價嗎 //p ;//ok //buf ;//err //buf隻是一個常量,不可修改 return 0; }

6.字符串拷貝

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> int main0601() { char src[] = "qqqqqqqqqq"; char dst[100] = { 0 }; int i = 0; for (; src[i] != 0; i ) { dst[i] = src[i]; } //補齊結束符 dst[i] = 0; printf("%s", dst); return 0; } void my_strcpy(char* dst, char* src) { int i = 0; for (; *(src i) != 0; i ) { *(dst i) = *(src i);//dst[i]=src[i] } // dst[i] = 0; *(dst i) = 0; } void my_strcpy2(char* dst, char* src) { while (*src!=0) { *dst = *src; src ; dst ; } *dst = 0; } void my_strcpy3(char* dst, char* src) { //*dst=*src //dst ,src //判斷*dst是否為0,為0跳出循環 while (*dst = *src ) { NULL;//先執行,再自加;先*dst=*src,dst ,src } } //成功返回0,失敗返回非零 //1.判斷形參指針是否為NULL //2.不要直接使用形參 以防止将形參的指針指向末尾 int my_strcpy4(char* dst, char* src) { if (dst == NULL || src == NULL) { return -1; } //使用輔助變量将形參的值接來 char* to = dst; char* from = src; //*dst=*src //dst ,src //判斷*dst是否為0,為0跳出循環 while (*from = *to ) { NULL;//先執行,再自加;先*dst=*src,dst ,src } printf("my_strcpy4:dst=%s\n", dst); return 0; } int mai0602n() { char src[] = "qqqqqqqqqq"; char dst[100] = { 0 }; int ret = 0; ret = my_strcpy4(dst, src); if (ret != 0) { printf("my_strcpy4 err:%d\n", ret); return ret; } printf("%s\n", dst); int i = 0; int n = i ; printf("n=%d,i=%d\n", n, i); return 0; }

7.strstr中的while與do-while的模型

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> int main01() { char* p = "sadzxaaadaszxaaaass---zxaa231312zxaa4";//zxaa int n = 0; do { p = strstr(p, "zxaa"); if (p != NULL) { n ;//累計個數 //重新設置查找起點 p = p strlen("zxaa"); } else//若沒有匹配的字符串跳出循環 { break; } } while (*p!=0);//如果沒到結尾 printf("n=%d\n", n); return 0; } int main02() { char* p = "sadzxaaadaszxaaaass---zxaa231312zxaa4";//zxaa int n = 0; while ((p = strstr(p, "zxaa")) != NULL) { //能進循環一定有匹配到子串 //重新設置起點位置 p = p strlen("zxaa"); n ; if (*p == 0)//如果到結束符 { break; } } printf("n=%d\n", n); return 0; } int my_strstr(char* p, int* n) { //兩個輔助變量 int i = 0; char* temp = p; while ((temp = strstr(temp, "zxaa")) != NULL) { //能進循環一定有匹配到子串 //重新設置起點位置 temp = temp strlen("zxaa"); i ; if (*temp == 0)//如果到結束符 { break; } } *n = i; return 0; } int main07() { char* p = "sadzxaaadaszxaaaass---zxaa231312zxaa4";//zxaa int n = 0; int ret = 0; ret = my_strstr(p, &n); if (ret != 0) { return ret; } printf("n=%d\n", n); return 0; }

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
超級詳細的手機發展史
超級詳細的手機發展史
作為全球第一的手機品牌,三星為何會退出中國市場?或許現在很多的年輕人不太清楚,但8090後肯定還記得,當年Note7事件至今也沒有給一個說法,三星既然不把中國消費者當回事兒,國内消費者自然也不會買賬了。但自從華為被斷芯之後,空出來的市場份額...
2026-01-13
内存條是ddr3還是ddr4在哪裡看
内存條是ddr3還是ddr4在哪裡看
内存條是ddr3還是ddr4在哪裡看?如今,新電腦已經完全普及DDR4内存,已經是目前主流從内存的發展曆史來看,曆代内存發展都是往高頻、低功耗的路線發展的,而DDR4内存也不例外,那麼DDR3與DDR4内存有什麼區别?DDR4相比DDR3内...
2026-01-13
西安兒童室内遊樂場設備
西安兒童室内遊樂場設備
西安戶外遊樂場所安裝哪些遊樂設備合适,随着城市基礎建築的數量越來越多,小區、公園、文化館等場地的建立,也使得戶外遊樂場的需求量增加。戶外遊樂場所安裝哪些遊樂設備合适?這是很多創業者在投資前期都會有所顧慮的一件事情,因為不知道怎樣的設備更合适...
2026-01-13
小米解bl鎖對使用有影響嗎
小米解bl鎖對使用有影響嗎
有時為了線刷小米手機,會進行解BL鎖操作。不過為了保證手機安全性,不影響保修售後,最好重新進行加鎖,今天我就教大家如何為小米手機加BL鎖。1、首先,去MIUI官網下載【解鎖工具包】,并在電腦上解壓;2、将手機關機,按【電源鍵音量下】,進入f...
2026-01-13
老年人如何使用智能手機
老年人如何使用智能手機
科技在不斷進步,智能手機的普及程度也越來越高,大部分情況下并不是手機不好用,而是上了年紀的我們不知道怎麼用,這篇文章我就來說說使用智能手機4個技巧,每一條都很實用,一起來看看:第一,通話錄音随着年齡的增長,我們的記憶力也在逐漸衰退,大部分人...
2026-01-13
Copyright 2023-2026 - www.tftnews.com All Rights Reserved