[关闭]
@pnck 2015-11-15T23:38:40.000000Z 字数 3710 阅读 2112

Deep Into C

presentation


前置技能:

补充:


高级技巧

草稿

  1. typedef struct _NODE
  2. {
  3. struct _NODE* next;
  4. }NODE;
  5. struct DATA1_WITH_LIST
  6. {
  7. NODE list_node;
  8. //any_data
  9. int data;
  10. char str[600];
  11. }
  12. struct DATA2_WITH_LIST
  13. {
  14. NODE list_node;
  15. //any_data
  16. int data;
  17. struct StuInfo stu;
  18. }
  19. N0DE n1,n2,n3,n4;
  20. n1.next = n2;
  21. n2.next = n3;
  22. n3.next = n4;
  23. n4.next = n1;
  24. n2.next = n4;//剔除n3
  25. NODE * n7 = malloc(sizeof(NODE));
  26. n7->next = n4;
  27. n2.next = n7;
  28. free(n7);
  29. n2.next = n4;//n7被踢
  30. InitList()
  31. {
  32. NODE *n1 = malloc(sizeof(NODE));
  33. }
  34. AddNode(NODE * 某一node)
  35. {
  36. NODE *new_node = malloc(sizeof(NODE));
  37. new_node->next = 某一node->next;
  38. 某一node->next = new_node;
  39. }
  40. DelNode(NODE * 某一node)
  41. {
  42. }
  43. AddNode(struct NODE* nd)
  44. {
  45. NODE *newNode (struct NODE*) = malloc(sizeof(struct NODE));
  46. newNode->pNext = nd->pNext;
  47. newNode->pPrev = nd;
  48. nd->pNext = newNode;
  49. newNode->pNext->pPrev = newNode;
  50. }
  51. void func(int a)
  52. {
  53. puts("this is a function");
  54. }
  55. void func3(int a)
  56. {...}
  57. int func2(char a, char b)
  58. {...}
  59. int main()
  60. {
  61. int func2(char, char); //声明
  62. func();
  63. TYPE * var_name;//定义TYPE类型的指针
  64. void (*pFunc)(int a);
  65. pFunc = func;//将函数地址赋值给函数指针
  66. pFunc(10);//调用func
  67. pFunc = func3;
  68. pFunc(10);//调用func3
  69. int (*pFunc2)(char, char);
  70. pFunc2 = func2;
  71. pFunc2('a', 'b');
  72. }
  73. #define pi 3.14
  74. #define TRUE 0
  75. typedef int DOULBE;//NOOOOOOOO
  76. sizeof
  77. double CalcArea(double r)
  78. {
  79. return r * r * pi;
  80. }
  81. struct StuInfo
  82. {
  83. char name[60];
  84. double mark;
  85. };
  86. typedef struct StuInfo
  87. {
  88. char name[60];
  89. double mark;
  90. } STUINFO, *STUINFO_PTR;
  91. struct StuInfo -----> > STUINFO
  92. struct StuInfo* -----> STUINFO_PTR
  93. //leagacy define
  94. struct StuInfo stu1;//变量
  95. struct Stuinfo* stu2;//指针
  96. //new way
  97. STUINFO stu1;
  98. stu1.name
  99. stu1.mark
  100. STUINFO_PTR stu2;
  101. stu2->name//.换成了->
  102. stu2->mark
  103. strcpy(stu.name, "maodada");
  104. stu.mark = 101;
  105. char array[10];
  106. sizeof(array);
  107. void GetArray(char a[]/*char *a */)
  108. {
  109. sizeof(a);//Never
  110. }
  111. void swap(int a, int b) {..}
  112. ...
  113. int aa = 1, bb = 2;
  114. swap(aa, bb);
  115. //...any output
  116. void* malloc(/*size*/)
  117. void free(/*ptr*/)
  118. char *a = malloc(10);//生成了10个元素的char数组
  119. //papapa
  120. //...
  121. //完了
  122. free(a);
  123. memory - allocation
  124. scanf("%d %d %d", &x, &y, &z);
  125. double mat[x][y];//you want this
  126. double *dynamic_mat = malloc(x*y*z*sizeof(double));
  127. //double normal[x*y*z];//=L=
  128. //normal[0][0]
  129. //dynamic_mat[0][1]//can this
  130. //dynamic_mat[0][1][2]//wrong
  131. //double *mat = malloc(10*sizeof(double));
  132. //数组名 是 一个指向第一个元素的指针
  133. //一个指向第一个元素的指针 是 数组名
  134. mat:
  135. [00][01][02][03]
  136. [10][11][12][13]
  137. [20][21][22][23]
  138. ...
  139. mat ---> double*
  140. mat[1] ---> double*[1] ---> double
  141. mat[0][1]---> double [1] ---> ?
  142. //没有办法变成二维的数组!!
  143. //mat 如果是多一级的指针呢?
  144. mat ---> double**
  145. mat[1] ---> double**[1] ---> double*
  146. mat[0][1]---> double* [1] ---> double
  147. //三维?
  148. mat ---> double***
  149. mat[1] ---> double***[1] ---> double**
  150. mat[0][1]---> double** [1] ---> double*
  151. mat[0][0][0]-> double* [0] ---> double
  152. mat ---> double** -- > double* (*x2)
  153. mat[0] ---> double*(*x1)
  154. mat[0][0]---> double*
  155. mat[0][0][0]-> double*[0] ---> double
  156. ///指针最高只需要2级,够了。
  157. double * MatPlus(double *m1, double *m2)
  158. {
  159. //...
  160. }
  161. mat[0]
  162. mat[1]
  163. list[0]
  164. list[1]
  165. int **mat = malloc(x*sizeof(int*));
  166. for (int i = 0; i < x; i++)
  167. {
  168. mat[i] = malloc(y * sizeof(int*));
  169. for (int j = 0; j < y; j++)
  170. {
  171. mat[i][j] = malloc(z * sizeof(int))
  172. }
  173. }
  174. for(;i<sizeof_pic;i++)
  175. {
  176. brighten(points[i]);
  177. }
  178. for(;i<sizeof_pic;i++)
  179. {
  180. darken(points[i]);
  181. }
  182. for(;i<array_size;i++)
  183. {
  184. plusOne(array[i]);
  185. }
  186. void (*func)(int);
  187. func = brighten;
  188. //func = darken;
  189. //func = PlusOne;
  190. for(;i<sizeof_pic;i++)
  191. {
  192. func(points[i]);
  193. }
  194. void brighten(int *pt_value)
  195. {
  196. *pt_value = *pt_value*2;
  197. }
  198. void ProcEveryPoint(void (*func)(int *pt_value),int *Points,int n)
  199. {
  200. for(int i=0; i<n; i++)
  201. {
  202. func(&Points[i]);
  203. }
  204. }
  205. void my_proc(int *pt_value)
  206. {
  207. //i can do anything
  208. }
  209. ProcEveryPoint(my_proc,Points,n);//这里永远是固定的
  210. ProcEveryPoint(darken,Points,n);
  211. ProcEveryPoint(brighten,Points,n);
  212. ProcEveryPoint(plusOne,Points,n);
  213. void swap(int *a,int *b)
  214. {
  215. int t = *a;
  216. *a = *b;
  217. *b = t;
  218. }
  219. int x,y;
  220. swap(&x,&y);
  221. int *px = &x;
  222. int *py = &y;
  223. swap(px,py);
  224. //指向关系没有改变
  225. px --> x
  226. py --> y
  227. /////若要
  228. px --> y
  229. py --> x
  230. swap(&px,&py);
  231. px --> y
  232. py --> x
  233. void swap(int **a, int **b)
  234. {
  235. int *t = *a;
  236. *a = *b;
  237. *b = t;
  238. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注