標籤:style blog http color ar for 2014 div log
二級指標找出最大的字元 :
char maxchar(char * str, char** max){char da = *str; //先讓最大的執行第一個字元*max = str;while (*str){if (*str > da){da = *str;*max = str;}str++;}return da;}int main(){char str[] = "hello liuwxeia";char * max = NULL; char ch = maxchar(str, &max);printf("%p\t%c\n", max, ch);printf("%p\n", &str);return 0;}
指標數組: 數組裡的每個元素都是指標
int *a[5]; //a是一個數組,5個元素都是int *d的指標 // sizeof(a) -----------> 20個位元組。 因為a是一個數組,不是一個指標 //一個指標4個位元組
對字串數組進行排序:
void show_str(char **s, int n){int i;for (i = 0; i < n; i++){printf("%s\n", s[i]);}}void sort_str(char **s, int n){int i, j, min;char * temp;for (i = 0; i < n; i++){min = i;for (j = i + 1; j<n; j++)if (strcmp(s[min], s[j]) > 0)min = j;temp = s[min];s[min] = s[i];s[i] = temp;}}int main(){char *s[] = { "hello", "world", "liuwei", "xuanyuan", "nima" };show_str(s, 5);sort_str(s, 5);printf("*****************\n");show_str(s, 5);return 0;}
ps :參數裡二級指標等價於指標數組
二維數組等價於數組指標
來自為知筆記(Wiz)
8.2 二級指標 指標數組