1. 程式人生 > >C語言指標訓練

C語言指標訓練

去空字串

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 char *  removeSpace(char * arr)
 7 {
 8     //char temp[100];
 9     char * start = arr;
10     //字串有效長度需要-1為陣列元素下標
11     char * end = arr + strlen(arr) - 1;
12     while (*end == '
' && end > start) 13 { 14 end--; 15 } 16 *(end + 1) = '\0'; 17 while (*start == ' ' && start < end) 18 { 19 start++; 20 } 21 return start; 22 23 } 24 25 int main() 26 { 27 28 char arr[] = " 你好 "; 29 30 char * p= removeSpace(arr);
31 printf("%s\n", p); 32 33 34 printf("%d\n", sizeof(int **)); 35 printf("%d\n", sizeof(int ***)); 36 printf("%d\n", sizeof(void *)); 37 system("pause"); 38 return EXIT_SUCCESS; 39 }

 指標和函式

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string
.h> 4 #include<stdlib.h> 5 6 void tab1(int a, int b) 7 { 8 int temp = a; 9 a = b; 10 b = temp; 11 printf("%d %d\n", a, b); 12 return; 13 } 14 15 int main1() 16 { 17 int a = 10; 18 int b = 20; 19 tab1(a, b); 20 printf("%d %d\n", a, b); 21 22 system("pause"); 23 return EXIT_SUCCESS; 24 } 25 void tab(int *a, int *b) 26 { 27 int temp = *a; 28 *a = *b; 29 *b = temp; 30 } 31 int main() 32 { 33 34 int a = 10; 35 int b = 20; 36 tab(&a, &b); 37 printf("%d %d\n", a, b); 38 39 tab1(a, b); 40 system("pause"); 41 return 0; 42 }

指標作為函式引數

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 //1、陣列作為函式引數可以退化為指標
 7 //2、在傳遞陣列時需要加上陣列的個數
 8 
 9 void print01(int * arr, int len)
10 {
11     //函式引數中如有有陣列  都會轉化為指標  sizeof(int *)  4  所以求出來的值不能作為陣列的迴圈條件存在
12     for (int i = 0; i < len; i++)
13     {
14         printf("%d\n", arr[i]);
15     }
16 }
17 int main2()
18 {
19 
20     int arr[] = { 1,2,3,4,6,0,7,8,9,10 };
21 
22     print01(arr, sizeof(arr) / sizeof(arr[0]));
23     system("pause");
24     return EXIT_SUCCESS;
25 }
26 void print(char * arr)
27 {
28     //兩種方式可以求出字串長度 \0
29     int len = strlen(arr);
30     int i = 0;
31     while (arr[i] != '\0')
32     {
33         i++;
34     }
35     printf("%d\n", i);
36 }
37 int main(void)
38 {
39     char arr[] = "hello world";//字串
40     //char arr[] = { 'h','e','l','l','o' };//字元陣列
41     print(arr);
42     system("pause");
43     return 0;
44 }

函式的返回值是指標

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 int aa = 10;//全域性變數
 7 
 8 char * test()
 9 {
10     //字元陣列  建立位置在棧區  
11     //char arr[] = "hello world";
12     //字串常量  會在程式執行時   常量區  不能被修改的 在程式結束時 銷燬
13     char * arr = "hello world";
14     //static 
15     aa = 100;
16     //保證指標地址對應的值是有內容的
17     return arr;
18 }
19 int main04()
20 {
21     char * p = test();
22     printf("%p\n", p);
23     printf("%s\n", p);
24 
25     system("pause");
26     return EXIT_SUCCESS;
27 }
28 
29 //strstr   hello  world   llo

實現strstr函式

#include <string.h>

char*strstr(constchar *haystack, constchar *needle);

功能:在字串haystack中查詢字串needle出現的位置

引數:

       haystack:源字串首地址

       needle:匹配字串首地址

返回值:

       成功:返回第一次出現的needle地址

       失敗:NULL

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 //不是很好
 6 /*
 7 1、兩個匹配的字串 必須完全匹配  匹配個數 = 字串長度
 8 2、如果配匹一個字串,需要記錄被匹配字串地址
 9 3、如果匹配一半未成功 回到記錄被匹配字串地址+1
10 4、如果匹配的被匹配字串的結尾  匹配個數 不等於 字串長度
11 */
12 
13 char * mystrstr(char * dest, char *src)
14 {
15     int i = 0;
16     int j = 0;
17 
18     //匹配個數
19     int count = 0;
20     int len = strlen(src);
21     char * p = NULL;
22     while (dest[i] != '\0')
23     {
24         //if (dest[i] == src[i]);
25 
26         while (dest[i] == src[j] && dest[i])//匹配個數 = 字串長度 l l     l o
27         {
28             if (!count)
29                 //如果匹配成功一個字元  需要記錄位置
30                 p = &dest[i];
31             count++;
32             i++; 
33             j++;
34             //匹配成功
35             if (count == len)
36             {
37                 return p;
38             }
39 
40         }
41 
42         //發生改變的值  i  j  count  p
43         if (count < len)
44         {
45             i = i - count;
46             j = 0;
47             //count 歸 0
48             count = 0;
49             //continue;
50         }
51 
52         i++;
53     }
54 
55     //返回值結果
56     //return p;
57     return NULL;
58 }
59 
60 int main()
61 {
62 
63     char *p = mystrstr("helllllo", "lllllo");
64     printf("%s\n", p);
65 
66     system("pause");
67     return EXIT_SUCCESS;
68 }

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 /*
 7 1、兩個匹配的字串 必須完全匹配  匹配個數 = 字串長度
 8 2、如果配匹一個字串,需要記錄被匹配字串地址
 9 3、如果匹配一半未成功 回到記錄被匹配字串地址+1
10 4、如果匹配的被匹配字串的結尾  匹配個數 不等於 字串長度
11 */
12 
13 char * mystrstr(char * dest, char *src)
14 {
15     char * p = NULL;
16     char * temp = src;
17     while (*dest)//
18     {
19         p = dest;
20         while (*dest == *temp && *dest)//匹配個數 = 字串長度 l l     l o
21         {
22             dest++;
23             temp++;
24         }
25         if (!*temp)//\0
26                    //if (*temp=='\0')//\0
27             return p;
28         else
29             temp = src;
30         dest = p;
31         dest++;
32     }
33 
34     //返回值結果
35     //return p;
36     return NULL;
37 }
38 
39 int main()
40 {
41 
42     char *p = mystrstr("helllo", "lll");
43     printf("%s\n", p);
44 
45     system("pause");
46     return EXIT_SUCCESS;
47 }

指標和字串

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 int main1()
 7 {
 8 
 9     char arr[] = "hello world";//ABllo world
10     char * p;
11     p = arr;
12     *p = 'A';//arr[0] p[0]
13 
14     p++;//arr[1] p[1]
15     *p = 'B';
16     printf("%s\n", arr);
17     printf("%d\n", sizeof(arr));//12
18     printf("%d\n", strlen(arr));//11
19     printf("%d\n", sizeof(p));//4
20     printf("%d\n", strlen(p));//1 4 10
21 
22     system("pause");
23     return EXIT_SUCCESS;
24 }
25 
26 int main()
27 {
28     char * arr = "hello world";//常量區
29     char  arr1[] = "hello world";//棧區
30     printf("%s\n", arr);
31     printf("%c\n", arr[0]);
32     char * p = arr;
33     printf("%p\n", p);
34     //字串常量是一個常量的陣列 可以讀取字元或者字串  但是不能修改
35     //p[0] = 'A';
36     //*p = 'A';
37     p = arr1;
38     printf("%p\n", p);
39 
40     //p[0] = 'A';
41     //*p = 'A';
42     //printf("%s", p);
43     system("pause");
44     return EXIT_SUCCESS;
45 }

實現strcat函式

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 void mystrcat(char * arr, char * s1)
 7 {
 8     //while (*arr)
 9     //    arr++;
10     //while (*arr++ = *s1++);
11     while (*arr)
12         arr++;
13     while (*s1)
14     {
15         *arr = *s1;
16         arr++;
17         s1++;
18     }
19     *arr = '\0';
20 }
21 int main()
22 {
23     char arr[ ] = "hello";
24     char * s1 = "world";
25     mystrcat(arr, s1);
26     printf("%s\n", arr);
27 
28 
29 
30     system("pause");
31     return EXIT_SUCCESS;
32 }

字串排序

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 
 7 //字串排序  根據字串首字元 按照a-z的順序排序
 8 //student tree new bee  bee new student tree
 9 
10 void bubble(char ** arr,int len)
11 {
12     for (int i = 0; i < len - 1; i++)
13     {
14         for (int j = 0; j < len - i - 1; j++)
15         {
16             //比對兩個字串的首字母
17             //1、指標判斷
18             //if (**(arr + j) < **(arr + j + 1))
19             //{
20             //    char * temp = *(arr+j);
21             //    *(arr + j) = *(arr + j + 1);
22             //    *(arr + j + 1) = temp;
23             //}
24             //2、陣列判斷
25             //if (arr[j][0] > arr[j+1][0])
26             //{
27             //    char * temp = arr[j];
28             //    arr[j] = arr[j+1];
29             //    arr[j + 1] = temp;
30             //}
31             //3、混合判斷
32             if (*arr[j] > *arr[j + 1])
33             {
34                 char * temp = arr[j];
35                 arr[j] = arr[j+1];
36                 arr[j + 1] = temp;
37             }
38         }
39     }
40 }
41 
42 int main()
43 {
44     char *arr[] = { "cshdf", "ehsdhf", "bjhdjfhd","abee" };
45 
46     /*arr[0][0]
47     student //arr[0]
48     tree//arr[1]
49     new
50     bee
51     */
52     bubble(arr, 4);
53 
54     for (int i = 0; i < 4; i++)
55     {
56         printf("%s\n", arr[i]);
57     }
58     //printf("%c\n", arr[0][0]);
59     //printf("%c\n", arr[1][0]);
60     //printf("%c\n", arr[2][0]);
61     //printf("%c\n", arr[3][0]);
62 
63 
64     system("pause");
65     return EXIT_SUCCESS;
66 }

氣泡排序的優化演算法

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 void bubble(int *arr, int len)
 7 {
 8     int flag = 1;
 9     for (int i = 0; i < len - 1; i++)
10     {
11         for (int j = 0; j < len - i - 1; j++)
12         {
13             if (arr[j] < arr[j + 1])
14             {
15                 flag = 0;
16                 int temp = arr[j];
17                 arr[j] = arr[j + 1];
18                 arr[j + 1] = temp;
19             }
20         }
21         if (flag)
22             return;
23         flag = 1;
24     }
25 }
26 int main()
27 {
28     int arr[] = { 1,3,5,8,9,2,7,4,6,0 };
29     bubble(arr, 10);
30 
31     for (int i = 0; i < 10; i++)
32     {
33         printf("%d\n", arr[i]);
34     }
35     system("pause");
36     return EXIT_SUCCESS;
37 }