二维数组 malloc 外挂 实现库函数

数组与指针 :
数组在传递参数里,作用一样:  array 都是一个指针,接收数组的首地址
(int array[],int n  ) 
 ( int * array,   int n   )

指针和数组可以等价转换
array[i]     =========  *(array+i)


二维数组传参 :

(int a[][],  int R , int C)  //错误

横坐标可以不写,列坐标必须要写。
(int a[][6],  int R , int C) 
  1. #include <stdio.h>
  2. #include <string.h>
  3. void init(int a[][6] ,int m, int n)
  4. {
  5. printf("%d\n",(int)sizeof(a)); //结果是4
  6. }
  7. int main()
  8. {
  9. int a[5][6];
  10. init(a,5,6);
  11. }

不同的指针+1的区别
int a [6] [4]

&a[0][0]  + 1             //  +了 4个字节
a[0]         + 1            //   +了 4个字节           a[0] 就从二维数组的概念转变成一维数组了
a              +1            //   +了 4*4个字节         a   应该从二维数组的角度
&a           + 1            //   +了 4*6*4个字节



   0    0    0
0    0    0    0 
0    0    0    0
0    0    0    0
0    0    0    0
0    0    0    0

   0    0    0           0    0    0    0           0    0    0    0             0    0    0    0           0    0    0    0           0    0    0    0  

随机输出扑克牌(不重复):
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #define M 4
  5. #define N 13
  6. int main()
  7. {
  8. char a[M][N] = {0};
  9. srand(time(NULL));
  10. int n,m,num;
  11. const char X[] = {'1','2','3','4','5','6','7','8','9','t','j','q','k'};
  12. const char Y[] = {'a','b','c','d'};
  13. printf("请输入你现在还有几张牌:");
  14. scanf("%d",&num);
  15. while(num>0)
  16. {
  17. m = rand()%4;
  18. n = rand()%13;
  19. if(!a[m][n])
  20. {
  21. a[m][n] = 1;
  22. num--;
  23. printf("%c %c\n",Y[m],X[n]);
  24. }
  25. }
  26. return 0;
  27. }
malloc的使用 :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. int n;
  6. printf("你的名字最多多长:");
  7. scanf("%d", &n);
  8. getchar(); //过滤回车
  9. char * a;
  10. a = (char *)malloc(n * sizeof(char));
  11. printf("请输入你的名字:");
  12. gets(a);
  13. printf("%s\n", a);
  14. free(a);
  15. }

realloc的使用 :

realloc(  指针   ,容量  );  //容量是改变后的大小,不是增加的大小
p = realloc(  p , 100)        //返回的地址, 是 现在100个的首地址  ,不是新增加的首地址


dll函数外挂 : 
_declspec(dllexport)   void   waigua()
{

}

_declspec(dllexport)是导出一个函数


交换字符串:
  1. #include <stdio.h>
  2. #include <string.h>
  3. char * str_reverse(char * str)
  4. {
  5. char temp;
  6. int len = strlen(str),i;
  7. for (i = 0; i < len/2; i++)
  8. {
  9. temp = str[i];
  10. str[i] = str[len-i-1];
  11. str[len-i-1] = temp;
  12. }
  13. return str;
  14. }
  15. int main()
  16. {
  17. char s[100] = "hello";
  18. char * p = str_reverse(s);
  19. printf("%s\n",p);
  20. }



字符串某个字符出现的次数:
  1. #include <stdio.h>
  2. #include <string.h>
  3. int str_times(char * str,char ch)
  4. {
  5. int i = 0;
  6. while(*str != '\0')
  7. {
  8. if (*str == ch)
  9. i++;
  10. str++;
  11. }
  12. return i;
  13. }
  14. int main()
  15. {
  16. char s[100] = "hello";
  17. printf("%d\n",str_times( s , 'l'));
  18. }


strlen的实现 :

第一种实现方案:
  1. sizt_t strlen(const char *s)
  2. {
  3. size_t n = 0;
  4. while(*s++)
  5. n++;
  6. return n;
  7. }

第二种实现方案:
  1. sizt_t strlen(const char *s)
  2. {
  3. const char * p = s;
  4. while(*s)
  5. s++
  6. return s - p;
  7. }

自己写一个strlen 类似 mb_strlen 汉字只算一个字符
  1. #include <stdio.h>
  2. #include <string.h>
  3. int mb_strlen(const char * p)
  4. {
  5. int len = 0;
  6. while( *p )
  7. {
  8. len++;
  9. if(*p++ < 0)
  10. p++;
  11. }
  12. return len;
  13. }
  14. int main()
  15. {
  16. char *s = "a我b是c你d大e";
  17. int len = mb_strlen( s );
  18. printf("%d\n",len);
  19. return 0;
  20. }


strstr的实现:
  1. #include <stdio.h>
  2. #include <string.h>
  3. char * str_find(char * str, char * f)
  4. {
  5. while (*str)
  6. {
  7. char * a = str;
  8. char * b = f;
  9. char * res = NULL;
  10. if (*a == *b)
  11. {
  12. res = a;
  13. while (*++a == *++b ) ;
  14. if (*b == '\0')
  15. return res;
  16. }
  17. str++;
  18. }
  19. return NULL;
  20. }
  21. int main()
  22. {
  23. char *s = "helloworld";
  24. printf("%s\n",str_find(s ,"lo"));
  25. return 0;
  26. }


strcpy的实现:
  1. #include <stdio.h>
  2. #include <string.h>
  3. void str_cpy( char * dest , const char * src)
  4. {
  5. while(*dest++ = *src++);
  6. }
  7. int main()
  8. {
  9. char buf[100] = "aaaaaaaaaaaaaaaaaaaaaaaa";;
  10. str_cpy(buf , "liuwei");
  11. printf("%s\n" , buf);
  12. return 0;
  13. }


strcat的实现:
  1. #include <stdio.h>
  2. #include <string.h>
  3. char * str_cat(char *p,char *s)
  4. {
  5. char * bak = p;
  6. while(*p)
  7. {
  8. p++;
  9. }
  10. while(*p++ = *s++)
  11. {
  12. }
  13. return bak;
  14. }
  15. int main()
  16. {
  17. char s[10] = "hello";
  18. char v[10] = "world";
  19. printf("%s\n",str_cat(s,v));
  20. return 0;
  21. }


strtok的实现(不完善):
  1. #include <stdio.h>
  2. #include <string.h>
  3. char *str_tok(char * str, const char * de)
  4. {
  5. static char * n;
  6. char * r; //返回的首地址
  7. const char * de_bak = de; //分隔符地址备份
  8. if (str != NULL)
  9. n = str;
  10. r = n;
  11. while (*n)
  12. {
  13. while (*de)
  14. {
  15. if (*n == *de)
  16. {
  17. *n++ = '\0';
  18. return r;
  19. }
  20. de++;
  21. }
  22. de = de_bak;
  23. n++;
  24. }
  25. if (r == n)
  26. {
  27. return NULL;
  28. }
  29. return r;
  30. }
  31. int main()
  32. {
  33. char s[] = "abcd,cdef,efgh,ghjk";
  34. char *de = ",c";
  35. printf("%s\n", str_tok(s, de));
  36. char *p;
  37. while (p = str_tok(NULL, de))
  38. printf("%s\n", p);
  39. return 0;
  40. }





posted @ 2014-09-02 16:00  我爱背单词  阅读(259)  评论(0编辑  收藏  举报