程序片段(01):01.Malloc.c

内容概要:Malloc拓展

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //01.内存伸缩函数:
  4. // void * realloc(void * block, unsigned int size);
  5. // 用途:用于内存节省,动态根据所需内存尺寸进行内存开辟!
  6. // 说明:返回值+参数
  7. // 参数(block):原始内存块儿的内存起始地址!
  8. // 参数(size):经过内存字节伸缩之后所占用的总内存字节尺寸!
  9. // 返回值(void *):经过内存字节伸缩之后返回的有效首地址(可能变化)!
  10. // 特点:拓展之后的内存不会自动清零!
  11. int main01(void)
  12. {
  13. //realloc();-->重新分配:在同一个函数当中进行堆内存的(伸缩|拓展)非常简单,指针变量所存储的指针位置总是正确的!
  14. int * p = (int *)malloc(100);
  15. for (int i = 0; i < 25; ++i)
  16. {
  17. printf("%3d", p[i] = i);
  18. }
  19. p = (int *)realloc(p, 104);//拓展内存:让内存字节总数增加四个字节,以多容纳一个额外的数组元素[内存字节尺寸拓展!]
  20. printf("拓展成功! \n");
  21. for (int i = 0; i < 26; ++i)
  22. {
  23. printf("%3d", p[i] = i);
  24. }
  25. system("pause");
  26. }
  27. void add1(int * p, int num)
  28. {
  29. p = (int *)realloc(p, 104);//内存首地址可能存在更改,可能不能实现跨函数修改数据!
  30. p[25] = num;//修改动作!
  31. }
  32. /*
  33. A-->P-->PP
  34. 1->&A->&P
  35. */
  36. int * add2(int * p, int num)
  37. {//通过返回内存地址的方式,确保跨函数修改内存的正确性!
  38. p = (int *)realloc(p, 104);//动态拓展堆内存,有可能返回一个新的内存首地址,从而无法实现原始内存地址的数据修改!
  39. p[25] = num;
  40. return p;
  41. }
  42. //int------>int * p;
  43. //int * p-->int ** pp;
  44. void add3(int ** pp, int num)
  45. {//通过传递变量的地址,实现跨函数修改内存
  46. *pp = (int *)realloc(*pp, 104);
  47. (*pp)[25] = num;//中括号("[]")的优先级高于星号("*"),特别注意!
  48. }
  49. int main02(void)
  50. {
  51. int * p = (int *)malloc(100);
  52. for (int i = 0; i < 25; ++i)
  53. {
  54. printf("%3d", p[i] = i + 1);
  55. }
  56. printf("\n");
  57. //p = add(p, 100);//通过内存地址返回的方式,实现跨函数修改内存!
  58. add3(&p, 26);//通过传递变量的地址的方式,实现跨函数修改内存!
  59. printf("内存拓展成功! \n");
  60. for (int i = 0; i < 26; ++i)
  61. {
  62. printf("%3d", p[i]);
  63. }
  64. system("pause");
  65. }
  66. //02.跨函数修改内存:
  67. // 返回新地址+传递变量地址

程序片段(02):01.二级指针.c

内容概要:锯齿数组

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define N 10
  4. //01.动态内存开辟规律:
  5. // 1.N级指针用于开辟(N-1)级指针的动态数组
  6. // 2.N级指针变量的名称就是动态数组的名称
  7. // 3.动态数组的名称和静态数组名称的区别:
  8. // 动态数组名称:变量指针
  9. // 静态数组名称:常量指针
  10. // 注:数组名称的特点,动态数组可以自我指定是变量指针还是常量指针!
  11. // 0级指针用于代替变量本身!
  12. //02.所有图形打印都在于规律寻求:
  13. // 行规律+规律+组合规律!
  14. //03.锯齿数组:
  15. // 1.关于核心N行进行对称
  16. // 2.总函数就是2*N-1
  17. // 3.前五行:N-i控制+后五行:i - N +2控制
  18. int main01(void)
  19. {
  20. //int * p = (int *)malloc(N * sizeof(int));
  21. int **pp = (int **)malloc((2 * N - 1) * sizeof(int *));//纵向一级指针数组!
  22. //pp[2 * N - 1];//(行数规律+对称规律)
  23. for (int i = 0; i < N; ++i)
  24. {//打印上部分锯齿
  25. pp[i] = (int *)malloc((N - i) * sizeof(int));//纵向指针一级指针数组所对应的整型数组开辟
  26. for (int j = 0; j < N - i; ++j)//i:0-1-2-3-4-->j<N-i:5-4-3-2-1;
  27. {
  28. printf("%3d", pp[i][j] = j + 1);
  29. }
  30. printf("\n");
  31. }
  32. for (int i = N; i < 2 * N - 1; ++i)//i=5:索引从5开始;i<2*N-1:最后一个索引
  33. {
  34. pp[i] = (int *)malloc((i - N + 2) * sizeof(int));//每个一级指针所对应的整数递增!
  35. for (int j = 0; j < i - N + 2; ++j)
  36. {
  37. printf("%3d", pp[i][j] = j + 1);
  38. }
  39. printf("\n");
  40. }
  41. system("pause");
  42. }

程序片段(03):01.删除.c

内容概要:动态数组删除数据

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void findFirstNum1(int * p, int length, int delNum)
  4. {
  5. int flag = 0;//假定多数情况找不到
  6. for (int i = 0; i < length; ++i)
  7. {
  8. if (delNum == *(p + i))
  9. {
  10. flag = 1;//存在否定情况
  11. break;
  12. }
  13. }
  14. return flag;
  15. }
  16. int * findFirstNum2(int * p, int length, int delNum)
  17. {
  18. int * pFlag = NULL;//假定多数情况找不到!
  19. for (int i = 0; i < length; ++i)
  20. {
  21. if (delNum == *(p + i))
  22. {
  23. return pFlag = p + i;//存在否定情况找得到!
  24. }
  25. }
  26. return NULL;
  27. }
  28. int findFirstNum3(int * p, int length, int delNum)
  29. {
  30. int pos = -1;
  31. for (int i = 0; i < length; ++i)
  32. {
  33. if (delNum == *(p + i))
  34. {
  35. pos = i;
  36. break;
  37. }
  38. }
  39. return pos;
  40. }
  41. void deleteNum(int ** pp, int *length, int delNum)
  42. {
  43. int pos = findFirstNum3(*pp, *length, delNum);
  44. if (-1 != pos)
  45. {
  46. for (int i = pos; i < *length - 1; ++i)//i<*length-1:空留最后一个位置用于做为删除缓冲!
  47. {
  48. *((*pp) + i) = *((*pp) + i + 1);//移动覆盖!
  49. }
  50. *pp = (int *)realloc(*pp, (*length-1));//压缩内存
  51. --*length;
  52. }
  53. }
  54. //01.for循环具备循环条件的对称性:
  55. // 从左往中+从右往中:对称判断性!
  56. int main01(void)
  57. {
  58. int * p = (int *)malloc(100);
  59. int length = 25;
  60. for (int i = 0; i < length; ++i)
  61. {//间隔赋值方式!
  62. if (0 == i % 2)
  63. {
  64. *(p + i) = 4;
  65. }
  66. else
  67. {
  68. *(p + i) = 5;
  69. }
  70. }
  71. for (int i = 0; i < length; ++i)
  72. {
  73. printf("%d \n", *(p + i));
  74. }
  75. for (int pos = findFirstNum3(p, length, 4); -1 != pos; pos = findFirstNum3(p, length, 4))
  76. {//不断的进行同一个整数的删除操作
  77. deleteNum(&p, &length, 4);
  78. }
  79. printf("删除之后! \n");
  80. for (int i = 0; i < length; ++i)
  81. {
  82. printf("%d \n", *(p + i));
  83. }
  84. printf("length = %d \n", length);
  85. system("pause");
  86. }

程序片段(04):01.字符串.c

内容概要:指针数组动态分配

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. //01.跨级指针实现数据层&显示层
  6. // 查找权限的控制-->地址映射对!
  7. int main01(void)
  8. {
  9. //char *pArr[10] = { "1123", "1231" };
  10. int count;
  11. scanf("%d", &count);
  12. char **pp = (char **)malloc(count * sizeof(char *));
  13. for (int i = 0; i < count; ++i)
  14. {
  15. char str[1024] = { 0 };
  16. scanf("%s", str);//针对于字符串的所有操作实质都是通过字符指针实现间接修改内存实体!
  17. *(pp + i) = (char *)malloc((strlen(str) + 1)*sizeof(char));//字符串结尾整体标识符'\0'
  18. strcpy(*(pp + i), str);//字符串拷贝!
  19. }
  20. for (int i = 0; i < count; ++i)
  21. {
  22. printf("%s \n", *(pp + i));
  23. }
  24. system("pause");
  25. }
  26. int num;
  27. //跨函数操控指针数组!
  28. void init(char *** ppp)
  29. {
  30. scanf("%d", &num);
  31. *ppp = (int **)malloc(num * sizeof(int *));
  32. for (int i = 0; i < num; ++i)
  33. {
  34. char str[1024] = { 0 };
  35. scanf("%s", str);
  36. *((*ppp) + i) = (int *)malloc((strlen(str) + 1) * sizeof(char));
  37. strcpy(*((*ppp) + i), str);
  38. }
  39. for (int i = 0; i < num; ++i)
  40. {
  41. printf("%s \n", *((*ppp) + i));
  42. }
  43. }
  44. //指针数组二分查找
  45. int binsearch(char **pp)
  46. {
  47. printf("二分查找法! \n");
  48. char searchStr[128] = { 0 };
  49. scanf("%s", searchStr);
  50. int minIndex = 0;
  51. int midIndex = 0;
  52. int maxIndex = num - 1;
  53. while (minIndex <= maxIndex)
  54. {
  55. midIndex = (minIndex + maxIndex) / 2;
  56. if (0 == strcmp(searchStr, *(pp + midIndex)))
  57. {
  58. printf("searchStr = %s找到,位于多少%p号地址! \n", searchStr, *(pp + midIndex));
  59. return midIndex;
  60. }
  61. else if (-1 == strcmp(searchStr, *(pp + midIndex)))
  62. {
  63. maxIndex = midIndex - 1;
  64. }
  65. else
  66. {
  67. minIndex = midIndex + 1;
  68. }
  69. }
  70. return -1;
  71. }
  72. //指针数组排序
  73. void sort(char **pp)
  74. {
  75. for (int i = 0; i < num - 1; ++i)
  76. {
  77. for (int j = 0; j < num - 1 - i; ++j)
  78. {
  79. if (0 < strcmp(*(pp + j), *(pp + j + 1)))
  80. {//比较数据区+交换指针区
  81. char * pTmp = *(pp + i);
  82. *(pp + i) = *(pp + i + 1);
  83. *(pp + i + 1) = *(pp + i);
  84. }
  85. }
  86. }
  87. puts("排序之后! \n");
  88. for (int i = 0; i < num; ++i)
  89. {
  90. printf("%s \n", *(pp + i));
  91. }
  92. }
  93. //通过二级指针数组间接从指针数组当中进行数据查询!
  94. int binSearchAddr(char *** ppp)
  95. {
  96. printf("二分查找法! \n");
  97. char searchStr[128] = { 0 };
  98. scanf("%s", searchStr);
  99. int minIndex = 0;
  100. int midIndex = 0;
  101. int maxIndex = num - 1;
  102. while (minIndex <= maxIndex)
  103. {
  104. midIndex = minIndex + (maxIndex - minIndex) / 2;
  105. if (0 == strcmp(searchStr, *(*(ppp + midIndex))))
  106. {
  107. printf("searchStr = %s找到,位于%p号地址! \n", *(*(ppp + midIndex)));
  108. return midIndex;
  109. }
  110. else if (-1 == strcmp(searchStr, *(*(ppp + midIndex))))
  111. {
  112. maxIndex = midIndex - 1;
  113. }
  114. else
  115. {
  116. minIndex = midIndex + 1;
  117. }
  118. }
  119. return -1;
  120. }
  121. //不操作原有指针数组的情况之下操作二级指针数组实现排序!
  122. // 诀窍:比较内存实体,交换指针变量(数据保护措施!)
  123. //注:只要有需求不改变原有数据的情况下实现排序,就需要高级指针:
  124. // 通过对地址映射进行排序,从而实现数据表现形式的排序特点!
  125. void sortByAddr(char ** pp)
  126. {
  127. char *** ppp = (char ***)malloc(num * sizeof(char **));
  128. for (int i = 0; i < num; ++i)
  129. {
  130. *(ppp + i) = pp + i;
  131. }
  132. for (int i = 0; i < num - 1; ++i)
  133. {
  134. for (int j = 0; j < num - 1 - num; ++j)
  135. {
  136. if (0 < strcmp(*(*(ppp + j)), *(*(ppp + j + 1))))
  137. {
  138. char ** ppTmp = *(ppp + j);
  139. *(ppp + j) = *(ppp + j + 1);
  140. *(ppp + j + 1) = ppTmp;
  141. }
  142. }
  143. }
  144. printf("二级指针数组显示层! \n");
  145. for (int i = 0; i < num; ++i)
  146. {//对二级指针数组进行排序之后-->方可对二级指针数进行查询操作!
  147. printf("%s \n", *(*(ppp + i)));
  148. }
  149. printf("一级指针数组数据层! \n");
  150. for (int i = 0; i < num; ++i)
  151. {
  152. printf("%s \n", *(pp + i));
  153. }
  154. binSearchAddr(ppp);
  155. }
  156. int main02(void)
  157. {
  158. //char * p[10] = { "1123", "1231" };
  159. char **pp;
  160. init(&pp);
  161. //sort(pp);//指针数组排序
  162. //binsearch(pp);//指针数组二分查找
  163. sortByAddr(pp);//构建二级指针数组,实现一级指针数据的权限查找
  164. system("pause");
  165. }

程序片段(05):01.五级指针.c

内容概要:五级指针

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //堆内存动态数组模拟栈内存静态普通变量数组
  4. int main01(void)
  5. {
  6. int * p = (int *)malloc(10 * sizeof(int));
  7. printf("%p \n", p);//打印的是变量所存储的二进制数据
  8. for (int i = 0; i < 10; ++i)
  9. {
  10. printf("%2d \n", *(p + i) = i);
  11. }
  12. free(p);
  13. system("pause");
  14. }
  15. //堆内存动态数组模拟栈内存静态指针变量数组
  16. int main02(void)
  17. {
  18. int ** pp = (int **)malloc(10 * sizeof(int *));
  19. printf("%p \n", pp);
  20. for (int i = 0; i < 10; ++i)
  21. {
  22. *(pp + i) = (int *)malloc(10 * sizeof(int));
  23. printf("%p\n", *(pp + i), " ");
  24. for (int j = 0; j < 10; ++j)
  25. {
  26. printf("%4d", *(*(pp + i) + j) = i * 10 + j);
  27. }
  28. printf("\n");
  29. }
  30. system("pause");
  31. }
  32. //堆内存动态数组模拟栈内存静态二级指针数组
  33. int main03(void)
  34. {
  35. int *** ppp = (int ***)malloc(10 * sizeof(int **));
  36. printf("%p \n", ppp);
  37. for (int z = 0; z < 10; ++z)
  38. {
  39. *(ppp + z) = (int **)malloc(10 * sizeof(int *));
  40. printf("%p \n", *(ppp + z));
  41. for (int x = 0; x < 10; ++x)
  42. {
  43. *(*(ppp + z) + x) = (int *)malloc(10 * sizeof(int));
  44. printf("%p \n", *(*(ppp + z)));
  45. for (int y = 0; y < 10; ++y)
  46. {
  47. printf("%4d \n", *(*(*(ppp + z) + x) + y) = z * 100 + x * 10 + y);
  48. }
  49. printf("\n");
  50. }
  51. printf("\n\n");
  52. }
  53. system("pause");
  54. }
  55. //01.内存架构模型:
  56. // 通过多级指针数组实现!

20160215.CCPP体系详解(0025天)的更多相关文章

  1. 20160129.CCPP体系详解(0008天)

    程序片段(01):函数.c+call.c+测试.cpp 内容概要:函数 ///函数.c #include <stdio.h> #include <stdlib.h> //01. ...

  2. 20160226.CCPP体系详解(0036天)

    程序片段(01):01.多线程.c+02.多线程操作.c 内容概要:多线程 ///01.多线程.c #include <stdio.h> #include <stdlib.h> ...

  3. 20160208.CCPP体系详解(0018天)

    程序片段(01):main.c 内容概要:PointWithOutInit #include <stdio.h> #include <stdlib.h> //01.野指针详解: ...

  4. 20160206.CCPP体系详解(0016天)

    代码片段(01):.指针.c+02.间接赋值.c 内容概要:内存 ///01.指针 #include <stdio.h> #include <stdlib.h> //01.取地 ...

  5. 20160205.CCPP体系详解(0015天)

    程序片段(01):01.杨辉三角.c 内容概要:杨辉三角 #include <stdio.h> #include <stdlib.h> #define N 10 //01.杨辉 ...

  6. 20160204.CCPP体系详解(0014天)

    程序片段(01):define.h+data.h&data.c+control.h&control.c+view.h&view.c+AI.h&AI.c+main.c 内 ...

  7. 20160203.CCPP体系详解(0013天)

    程序片段(01):数组.c+02.数组初始化语法.c 内容概要:数组 ///01.数组.c #include <stdio.h> #include <stdlib.h> //0 ...

  8. 20160128.CCPP体系详解(0007天)

    以下内容有所摘取,进行了某些整理和补充 论浮点数的存储原理:float浮点数与double浮点数的二进制存储原理–>阶码 浮点数转二进制 1.整数int类型和浮点数float类型都是占用4个字节 ...

  9. 20160127.CCPP体系详解(0006天)

    程序片段(01):msg.c 内容概要:线程概念 #include <stdio.h> #include <stdlib.h> #include <Windows.h&g ...

随机推荐

  1. PHP 常用header头定义

    在php的开发中,我们常常需要使用到header函数头来进行做标记 header() 函数向客户端发送原始的 HTTP 报头. 常用header设置列表如下: header('HTTP/1.1 200 ...

  2. bootstrap——bootstrap-table(1)

    前言: 特地纪念一下,自己参加这份工作刚好满一年.依旧乐在其中 言归正传:之前小接触过bootstrap,只是之后用得少了就生疏了,恰好现在的工作使用到了它,就再拿出来认真研究一下. 过程: 一直以来 ...

  3. Java内存回收机制.md

    1.java的内存 java的内存结构分为 堆 (是gc的主要区域) 线程共享,主要是用于分配实例对象和数组 栈 线程私有,它的生命周期和线程相同,又分成 虚拟机栈和本地方法栈,只有它会报 Stack ...

  4. ng-select 下拉的两种方式

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  5. [LeetCode] My Calendar III 我的日历之三

    Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...

  6. UVA - 11468:Substring

    随机生成一个字符可以看成在AC自动机里面向前走一个节点,那么ans就是0向前走L步并且不经过单词节点, 由概率知识可得,f[p][L]=∑f[nxt[p][i]][L-1]*g[i] 其中p表示位于p ...

  7. Simpson积分(BZOJ2178)

    lrj的代码常数太大T了QAQ,改了一下. #include <cstdio> #include <cmath> #include <algorithm> usin ...

  8. 【转载】 HTTP 中 GET 与 POST 的区别

    HTTP 中 GET 与 POST 的区别   GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通 ...

  9. thymeltesys-基于Spring Boot Oauth2的扫码登录框架

    thymeltesys thymelte是一个基于Spring Boot Oauth2的扫码登录框架,使用PostgreSQL存储数据,之后会慢慢支持其他关系型数据库.即使你不使用整个框架,只使用其中 ...

  10. 谷歌开发者:看可口可乐公司是怎么玩转TensorFlow的?

    在这篇客座文章中,可口可乐公司的 Patrick Brandt 将向我们介绍他们如何使用 AI 和 TensorFlow 实现无缝式购买凭证. 可口可乐的核心忠诚度计划于 2006 年以 MyCoke ...