Pointer: Memory Model 3; pointer model 3

Source: Internet
Author: User

Pointer: Memory Model 3; pointer model 3

 

1 # define _ CRT_SECURE_NO_WARNINGS 2 # include <stdio. h> 3 # include <stdlib. h> 4 # include <string. h> 5 6 // generate malloc 7 char ** getMem41 (int num) 8 {9 char ** p2 = NULL; 10 int I = 0; 11 12 p2 = (char **) malloc (sizeof (char *) * num); 13 if (p2 = NULL) 14 {15 return NULL; 16} 17 for (I = 0; I <num; I ++) 18 {19 p2 [I] = (char *) malloc (sizeof (char) * 100 ); // char buf [100]; 20 sprintf (p2 [I], "% d", I + 1, I + 1, I + 1); // in this format ("% d", I + 1, I + 1, I + 1) print to p2 [I]; 21} 22 return p2; 23} 24 25 // sort 26 void sortNum31 (char ** array, int num) 27 {28 char * tmp = NULL; 29 int I = 0, j = 0; 30 for (I = 0; I <num; I ++) 31 {32 for (j = I; j <num; j ++) 33 {34 if (strcmp (array [I], array [j]) <0) 35 {36 tmp = array [I]; // note: the value of the exchanged data, the pointer is switched // the pointer is directed to 37 array [I] = array [j]; 38 array [j] = tmp; 39} 40} 41} 42} 43 44 // print 45 46 void printNum31 (char ** array, int num) 47 {48 int I = 0; 49 for (I = 0; I <num; I ++) 50 {51 printf ("% s", array [I]); 52} 53} 54 55 void getMem41_Free (char ** p, int num) 56 {57 int I = 0; 58 char ** p2 = p; 59 for (I = 0; I <num; I ++) 60 {61 if (p2 [I]! = NULL) 62 {63 free (p2 [I]); 64 p2 [I] = NULL; 65} 66} 67 if (p2! = NULL) 68 {69 free (p2); 70 p2 = NULL; 71} 72} 73 74 int main () 75 {76 int I = 0; 77 char ** p2; 78 int num = 5; 79 char * tmp = NULL; 80 char tmpBuf [100]; 81 82 p2 = getMem41 (num ); 83 84 // print 85 printf before sorting ("Before sorting: \ n"); 86/* for (I = 0; I <num; I ++) 87 {88 printf ("% s \ n", p2 [I]); 89} */90 printNum31 (p2, num ); 91 92 93 // sort // the pointer 94/* for (I = 0; I <num; I ++) 95 {96 for (int j = I + 1; j <num; j ++) 97 {98 if (strcmp (p2 [I], p2 [j]) <0) 99 {100 tmp = p2 [I]; 101 p2 [I] = p2 [j]; 102 p2 [j] = tmp; 103} 104} 105} */106 107 108 // sort // swap memory 109/* for (I = 0; I <num; I ++) 110 {111 for (int j = I + 1; j <num; j ++) 112 {113 if (strcmp (p2 [I], p2 [j]) <0) 114 {115 strcpy (tmpBuf, p2 [I]); 116 strcpy (p2 [I], p2 [j]); 117 strcpy (p2 [j], tmpBuf ); 118} 119} 120} */121 122 // sorting 123 sortNum31 (p2, num); 124 // sorting 125 printf ("\ n sorted: \ n "); 126/* for (I = 0; I <num; I ++) 127 {128 printf ("% s \ n", p2 [I]); 129} */130 printNum31 (p2, num); 131 // release memory 132 getMem41_Free (p2, num); 133 system ("pause"); 134 return 0; 135}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.