您好,登錄后才能下訂單哦!
二維指針三種內(nèi)存模型圖:
統(tǒng)計(jì)字符串兩頭,非空字符的長(zhǎng)度
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { //統(tǒng)計(jì)字符串兩頭,非空字符的長(zhǎng)度 char *p = " abc "; int i = 0; int j = strlen(p) - 1; int count = 0; while (isspace(p[i]) && p[i] != '\0') { i++; } while (isspace(p[j]) && p[j] != '\0') { j--; } count = j + 1 - i; printf("%d \n",count); printf("Hello World!\n"); system("pause"); } /* 編譯運(yùn)行: 3 Hello World! 請(qǐng)按任意鍵繼續(xù). . . */
//函數(shù)封裝,統(tǒng)計(jì)字符串兩頭,非空字符的長(zhǎng)度
#include <stdio.h> #include <stdlib.h> #include <string.h> //函數(shù)封裝,統(tǒng)計(jì)字符串兩頭,非空字符的長(zhǎng)度 int getstrlen(char *str,int *count) { if (str == NULL || count == NULL) { return -1; } char *p =str; int i = 0; int j = strlen(p) - 1; while (isspace(p[i]) && p[i] != '\0') { i++; } while (isspace(p[j]) && p[j] != '\0') { j--; } *count = j + 1 - i; } int main() { int count = 0; char *s = " hello "; getstrlen(s,&count); printf("%d \n", count); system("pause"); } /* 編譯運(yùn)行: 5 請(qǐng)按任意鍵繼續(xù). . . */
【兩頭堵模型】
昨天的第一題作業(yè)--函數(shù)封裝,去除一個(gè)字符串的首尾空格
#include <stdio.h> #include <stdlib.h> #include <string.h> //函數(shù)封裝,去除一個(gè)字符串的首尾空格 int trimSpace(char *str,char *newstr) { if (str == NULL || newstr == NULL) { return -1; } char *p =str; int i = 0; int j = strlen(p) - 1; while (isspace(p[i]) && p[i] != '\0') { i++; } while (isspace(p[j]) && p[j] != '\0') { j--; } int count = j + 1 - i; strncpy(newstr, str+i,count); newstr[count] = '\0'; return 0; } int main() { char newstr[100] ; char *str = " hello "; trimSpace(str,newstr); printf("%s", newstr); return 0; } /* 編譯運(yùn)行: C:\Users\chunli>gcc main.c & a hello C:\Users\chunli> */
再次優(yōu)化,不需要另外使用新的數(shù)組
#include <stdio.h> #include <stdlib.h> #include <string.h> //函數(shù)封裝,去除一個(gè)字符串的首尾空格 int trimSpace(char *str) { if (str == NULL ) { return -1; } char *p =str; int i = 0; int j = strlen(p) - 1; while (isspace(p[i]) && p[i] != '\0') { i++; } while (isspace(p[j]) && p[j] != '\0') { j--; } int count = j + 1 - i; strncpy(str, str+i,count);//必須是地址 str[count] = '\0'; return 0; } int main() { char str[100] = " hello "; trimSpace(str);//不能使用常量區(qū)的內(nèi)存 printf("%s<--end", str); return 0; } /* 編譯運(yùn)行: C:\Users\chunli>gcc -o myfun main.c & myfun hello<--end C:\Users\chunli> */
字符串翻轉(zhuǎn)模型:
1,兩頭堵模型,字符串翻轉(zhuǎn):
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int ret = 0; char s[100] = "hello world!"; char *p1 = s; char *p2 = s+strlen(s)-1; while(p1 < p2 ) { *p1 = *p1 ^ *p2; *p2 = *p1 ^ *p2; *p1 = *p1 ^ *p2; p1++; p2--; } printf("%s<--end",s); return ret; } /* 編譯運(yùn)行: C:\Users\chunli>gcc -o myfun main.c & myfun !dlrow olleh<--end C:\Users\chunli> */
2, 兩頭堵模型,字符串翻轉(zhuǎn),函數(shù)封裝
#include <stdio.h> #include <stdlib.h> #include <string.h> int inverse(char *s) { if(s == NULL) { return -1; } char *p1 = s; char *p2 = s+strlen(s)-1; while(p1 < p2 ) { *p1 = *p1 ^ *p2; *p2 = *p1 ^ *p2; *p1 = *p1 ^ *p2; p1++; p2--; } } int main() { int ret = 0; char s[100] = "hello world!"; inverse(s); printf("%s<--end",s); return ret; } /* 編譯運(yùn)行: C:\Users\chunli>gcc -o myfun main.c & myfun !dlrow olleh<--end C:\Users\chunli> */
3, 字符串翻轉(zhuǎn),遞歸---全局變量
#include <stdio.h> #include <stdlib.h> #include <string.h> char g_buf[1024]={0}; int inverse(char *s) { static char buf[200]; int i = 0; if(s == NULL) return -1; if(*s == '\0') return 0; inverse(s+1); strncat(g_buf,s,1); } int main() { int ret = 0; char s[100] = "hello world!"; inverse(s); printf("%s \n",g_buf); return ret; } /* 編譯運(yùn)行: C:\Users\chunli>gcc -o myfun main.c & myfun !dlrow olleh */
4, 字符串翻轉(zhuǎn),遞歸---非全局變量
#include <stdio.h> #include <stdlib.h> #include <string.h> int inverse(char *s,char *to) { static char buf[200]; int i = 0; if(s == NULL || to == NULL) return -1; if(*s == '\0') return 0; inverse(s+1,to); strncat(to,s,1); } int main() { char g_buf[1024]={0}; int ret = 0; char s[100] = "hello world!"; inverse(s,g_buf); printf("%s \n",g_buf); return ret; } /* 編譯運(yùn)行: C:\Users\chunli>gcc -o myfun main.c & myfun !dlrow olleh */
函數(shù)的集成:
昨天的第三題作業(yè):
#include <stdio.h> #include <stdlib.h> #include <string.h> int trimSpace(char *str,char *newstr) { if (str == NULL || newstr == NULL) { return -1; } char *p =str; int i = 0; int j = strlen(p) - 1; while (isspace(p[i]) && p[i] != '\0') { i++; } while (isspace(p[j]) && p[j] != '\0') { j--; } int count = j + 1 - i; strncpy(newstr, str+i,count); newstr[count] = '\0'; return 0; } int get_Valude_By_Key(char *Key_And_Value,char *Key,char *Value) { if(Key_And_Value == NULL ||Key == NULL || Value == NULL ) { printf("ERROR FUN get_Valude_By_Key:" "Key_And_Value == NULL ||Key == NULL || Value == NULL"); return -1; } int ret = 0; char *p = Key_And_Value; p = strstr(p,Key); if(p == NULL) { printf("FUN get_Valude_By_Key: strstr(p,Key) == NULL \n"); return -1; } p = p + strlen(Key); p = strstr(p,"="); if(p == NULL) { printf("FUN get_Valude_By_Key: strstr(p,\"=\") == NULL \n"); return -1; } p = p+ strlen("="); ret = trimSpace(p,Value); if(ret!=0) { printf("FUN get_Valude_By_Key: trimSpace(p,Value) != 0 \n"); return -2; } return 0; } int main() { int ret = 0; char *Key_value = " id=666 = value=hahaha "; char *key = "id=666"; char value[100]={0}; ret = get_Valude_By_Key(Key_value,key,value); printf("[%s]\n",value); return ret; } /* 編譯運(yùn)行: C:\Users\chunli>gcc main.c & a [value=hahaha] */
C Const 指針 與變量:
#include <stdio.h> #include <stdlib.h> #include <string.h> void fun1(const char *p)//指針?biāo)赶虻膬?nèi)存空間不能修改 { printf("%s",p); //指針?biāo)赶虻膬?nèi)存空間可以讀取 p = (char *)0x99;//指針的本身的值可以修改 // p[1] = 1;//指針?biāo)赶虻膬?nèi)存空間不能修改 // *(p+1) = 1;//指針?biāo)赶虻膬?nèi)存空間不能修改 } void fun2(char *const p)//指針的本身的值不可以修改 { // p = (char *)0x99;//指針的本身的值不可以修改 p[1] = 'A';//指針?biāo)赶虻膬?nèi)存空間可以修改 *(p+1) = 'A';//指針?biāo)赶虻膬?nèi)存空間可以修改 printf("%s",p);//指針?biāo)赶虻膬?nèi)存空間可以讀取 } void fun3(const char *const p)//只能讀這個(gè)指針 { // p = (char *)0x99;//指針的本身的值不可以修改 // p[1] = 'A';//指針?biāo)赶虻膬?nèi)存空間不可以修改 // *(p+1) = 'A';//指針?biāo)赶虻膬?nèi)存空間不可以修改 printf("%s",p);//指針?biāo)赶虻膬?nèi)存空間可以讀取 } int main() { char p[] = "Hello World!\n";//定義一個(gè)普通指針 fun1(p); fun2(p); fun3(p); const int a = 10; int const b = 11; // a= 11; 編譯不通過(guò) // int *p2 = &a; //編譯這個(gè)不通過(guò) // int *p3 = &b; //編譯這個(gè)不通過(guò) return 0; } /* 編譯運(yùn)行: C:\Users\chunli>gcc main.c & a Hello World! HAllo World! HAllo World! */
指針做輸出模型,被調(diào)函數(shù)分配內(nèi)存
#include <stdio.h> #include <stdlib.h> #include <string.h> int fun1(char **myp1,int *mylen1,char **myp2,int *mylen2) { char *tmp1 = NULL; tmp1 = (char *)malloc(100); if(tmp1 == NULL) { printf("ERROR in tmp = (char *)malloc(100); \n"); return -1; } strcpy(tmp1,"Hello "); *mylen1 = strlen(tmp1); *myp1 = tmp1; char *tmp2 = NULL; tmp2 = (char *)malloc(100); if(tmp2 == NULL) { printf("ERROR in tmp2 = (char *)malloc(100); \n"); return -2; } strcpy(tmp2,"World !"); *mylen2 = strlen(tmp2); *myp2 = tmp2; return 0; } int my_free(char **p) { if(p == NULL) { printf("ERROR in my_free p == NULL \n"); return -1; } //方法1 /* *p 是一級(jí)指針的地址 **p ,*(*p)就是取二級(jí)指針?biāo)赶虻闹? free(*p);//釋放完指針變量所指向的內(nèi)存空間 *p = NULL;//把實(shí)參改成NULL */ //方法2 char *tmp = NULL; tmp = *p; free(tmp); tmp = NULL; } int main() { int ret = 0; char *p1 = NULL; char *p2 = NULL; int len1 = 0; int len2 = 0; ret = fun1(&p1,&len1,&p2,&len2); if(ret != 0) { printf("ERROR in fun1 :%d \n",ret); return ret; } printf("[1]%s,%d\n",p1,len1); printf("[2]%s,%d\n",p2,len2); my_free(&p1);//p1所指向的內(nèi)存釋放了,但是p1沒(méi)有改成NULL,有野指針現(xiàn)象 my_free(&p2); return 0; } /* 編譯運(yùn)行: C:\Users\chunli>gcc main.c & a [1]Hello ,6 [2]World !,7 */
//二級(jí)指針三種模型
// 第一種輸入,內(nèi)存模型
#include <stdio.h> #include <stdlib.h> #include <string.h> //二級(jí)指針三種模型 // 第一種輸入,內(nèi)存模型 //遍歷二級(jí)指針 void print_arr(char **array,int num) { int i = 0 ; for(i = 0;i<num;i++) { printf("%s\n",*(array + i )); } } // 排序 void sort_arr(char **array,int num) { int i = 0; int j = 0; for(i = 0;i<num;i++) { for(j=i+1;j<num;j++) { if(strcmp(array[i],array[j]) < 0) { char *tmp = array[i]; // 交換指針 array[i] = array[j]; array[j] =tmp; } } } } int main() { int i = 0; int j = 0; int num = 0; char *array[] = {"aaaa","abaaa","ac"};//數(shù)組的每一個(gè)元素都是一個(gè)普通指針 num = sizeof(array)/sizeof(array[0]); print_arr(array,num); sort_arr(array,num); printf("------------------------\n"); print_arr(array,num); return 0; } /* 編譯運(yùn)行: C:\Users\chunli>gcc main.c & a aaaa abaaa ac ------------------------ ac abaaa aaaa */
//二級(jí)指針三種模型
//第2種內(nèi)存模型,注意本次指針的步長(zhǎng)是30個(gè)char
#include <stdio.h> #include <stdlib.h> #include <string.h> //二級(jí)指針三種模型 //第2種內(nèi)存模型,注意本次指針的步長(zhǎng)是30個(gè)char int main() { char myBuf[30]; char array[10][30] = {"aa","ab","ac"}; int i = 0; int j = 0; int num =4; for(i = 0;i<num;i++) { printf("%s\n",array[i]); } for(i= 0;i<num;i++) { for(j=i+1;j<num;j++) { if(strcmp(array[i],array[j]) < 0) { char tmp[30]; strcpy(tmp,array[i]); // 內(nèi)存塊交換 strcpy(array[i],array[j]); strcpy(array[j],tmp); } } } for(i = 0;i<num;i++) { printf("%s\n",array[i]); } return 0; } /* 編譯運(yùn)行: C:\Users\chunli>gcc main.c & a aaaa abaaa ac ------------------------ ac abaaa aaaa */
二級(jí)指針三種模型--第3種內(nèi)存模型
#include <stdio.h> #include <stdlib.h> #include <string.h> //二級(jí)指針三種模型 //第3種內(nèi)存模型, int main() { char **p = NULL; int num = 5; int i = 0; int j = 0; //在堆中申請(qǐng)一片空間,能裝num個(gè)char 類(lèi)型的指針 p = malloc(sizeof(char *) * num); //在這num個(gè)指針,每個(gè)都指向不同的內(nèi)存塊 for(i = 0;i<num;i++) { p[i] = malloc(sizeof(char) * 100); sprintf(p[i],"%d,%d,%d",i,i+10,i*i); } //遍歷 printf("-----------------------\n"); for(i = 0;i<num;i++) { printf("%s \n",p[i]); } //指針交換 for(i = 0;i<num;i++) { for(j=i+1;j<num;j++) { if(strcmp(p[i],p[j])<0) { char * tmp = p[i]; p[i] = p[j]; p[j] = tmp; } } } //遍歷 printf("-----------------------\n"); for(i = 0;i<num;i++) { printf("%s \n",p[i]); } //內(nèi)存數(shù)據(jù)交換 for(i = 0;i<num;i++) { for(j=i+1;j<num;j++) { if(strcmp(p[i],p[j])> 0) { char tmp[100] ; strcpy(tmp,p[i]); strcpy(p[i],p[j]); strcpy(p[j],tmp); } } } //遍歷 printf("-----------------------\n"); for(i = 0;i<num;i++) { printf("%s \n",p[i]); } for(i = 0;i<num;i++) { if(p[i] != NULL) { free(p[i]); p[i] = NULL; } } return 0; } /* 編譯運(yùn)行: C:\Users\chunli>gcc main.c & a ----------------------- 0,10,0 1,11,1 2,12,4 3,13,9 4,14,16 ----------------------- 4,14,16 3,13,9 2,12,4 1,11,1 0,10,0 ----------------------- 0,10,0 1,11,1 2,12,4 3,13,9 */
//二級(jí)指針三種模型
//二維指針輸入 輸出
//第3種內(nèi)存模型,堆中開(kāi)辟,我們自己定義的數(shù)組,不需要編譯在棧中為我們那樣開(kāi)辟
#include <stdio.h> #include <stdlib.h> #include <string.h> //二級(jí)指針三種模型 //二維指針輸入 輸出 //第3種內(nèi)存模型,堆中開(kāi)辟,我們自己定義的數(shù)組,不需要編譯在棧中為我們那樣開(kāi)辟 char **getmem(num) { int i =0; char **p = NULL; //在堆中申請(qǐng)一片空間,能裝num個(gè)char* 類(lèi)型的指針 p = (char **)malloc(sizeof(char *) * num); if(p == NULL) { return NULL; } //在這num個(gè)指針,每個(gè)都指向不同的內(nèi)存塊 for(i = 0;i<num;i++) { p[i] = malloc(sizeof(char) * 100); sprintf(p[i],"%d,%d,%d",i,i+10,i*i); } return p; } void print_arr(char **p,int num) { int i = 0; for(i = 0;i<num;i++) { printf("%s \n",p[i]); } } void sort_arr1(char **p,int num) { int i = 0; int j = 0; //指針交換 for(i = 0;i<num;i++) { for(j=i+1;j<num;j++) { if(strcmp(p[i],p[j])<0) { char * tmp = p[i]; p[i] = p[j]; p[j] = tmp; } } } } void sort_arr2(char **p,int num) { int i = 0; int j = 0; //內(nèi)存數(shù)據(jù)交換 for(i = 0;i<num;i++) { for(j=i+1;j<num;j++) { if(strcmp(p[i],p[j])> 0) { char tmp[100] ; strcpy(tmp,p[i]); strcpy(p[i],p[j]); strcpy(p[j],tmp); } } } } void free_arr(char **p,int num) { int i =0; for(i = 0;i<num;i++) { if(p[i] != NULL) { free(p[i]); p[i] = NULL; } } } int main() { char **p = NULL; int num = 5; int i = 0; int j = 0; p = getmem(num); printf("--------\n"); print_arr(p,num); sort_arr1(p,num); printf("--------\n"); print_arr(p,num); sort_arr2(p,num); printf("--------\n"); print_arr(p,num); free_arr(p,num); if(p != NULL) { free(p); p = NULL; } return 0; } /* 編譯運(yùn)行: C:\Users\chunli>gcc main.c & a -------- 0,10,0 1,11,1 2,12,4 3,13,9 4,14,16 -------- 4,14,16 3,13,9 2,12,4 1,11,1 0,10,0 -------- 0,10,0 1,11,1 2,12,4 3,13,9 4,14,16 */
//玩轉(zhuǎn)多級(jí)指針
//玩轉(zhuǎn)多級(jí)指針 #include <stdio.h> #include <stdlib.h> #include <string.h> //玩轉(zhuǎn)多級(jí)指針 int getmem(char ***p,int num) { int i =0; if(p == NULL)//里面應(yīng)該是實(shí)參的取地址 { return -1; } char **tmp = (char **)malloc(sizeof(char *) * num); if(tmp == NULL) { return -1; } for(i = 0;i<num;i++) { tmp[i] = malloc(sizeof(char) * 100); sprintf(tmp[i],"%d -> %d -> %d",i,i+10,i*i); } *p = tmp; } int freemem(char ***p,int num) { if(p == NULL ) { return -1; } char **tmp = NULL; tmp = *p; int i; for(i = 0;i<num;i++) { if(tmp[i] != NULL) { free(tmp[i]); tmp[i] = NULL; } } free(tmp); *p = NULL; } int print_arr(char ***p,int num) { if(p == NULL) { printf("2 Hello \n"); return -1; } int i; for(i =0;i<num;i++) { printf("%s \n",*(*(p)+i)); } } int main() { char **p = NULL; int num = 5; getmem(&p,num); print_arr(&p,num); freemem(&p,num); return 0; } /* 編譯運(yùn)行: C:\Users\chunli>gcc main.c & a 0 -> 10 -> 0 1 -> 11 -> 1 2 -> 12 -> 4 3 -> 13 -> 9 4 -> 14 -> 16 */
作業(yè)題:
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。