Take bubble sort as an example--malloc/free redirect stdin stdout

Source: Internet
Author: User

ESORT.C code below, you can focus on the use of Mallloc/free,freopen redirection, sorted for each round to put the smallest number on the front:
1#include <stdio.h>2#include <malloc.h>3 4 #defineN 85 6 voidDatin (int*p);7 voidPrintOutint*p,intlen);8 voidEsort (intp[]);9 Ten intMainvoid) One { A     intlen=0; -     int*a,*Ptrinit; -A = (int*)malloc(sizeof(int)*N); thePtrinit =A; -     if(A = =NULL) -     { -printf"a malloc err!\n"); +         return-1; -     } + Datin (a); A     //a = Ptrinit; atprintf"\ n input data: \ n"); - printout (a,n); - Esort (a); -printf"\ esort data: \ n"); - printout (a,n); -      in      Free(a); -printf"\n--exit--\n", Len); to     return 1; + } -  the voidEsort (intp[]) * { $     intI=0, j=0;Panax Notoginseng     inttemp =0; -      for(i=0; i<n; i++) the          for(j=i; j<n; J + +) +         { A             if(P[i] >P[j]) the             { +temp =P[i]; -P[i] =P[j]; $P[J] =temp; $             } -         } - } the  - voidDatin (int*p)Wuyi { the     intLen =0 ; -Freopen ("Data.txt","R", stdin);//Data Input Wu      while(len<N) -     { Aboutscanf"%d", p); $len++; -p++; -     } - fclose (stdin); A } +  the voidPrintOutint*p,intlen) - { $     intI=0; the      while(i<len) the     { theprintf"%d\t",*p); thep++; -i++; in     } the}
View CodeNote the following function, parameter int *p is an integer pointer type:
1 voidDatin (int*p)2 {3     intLen =0 ;4Freopen ("Data.txt","R", stdin);//Data Input5      while(len<N)6     {7scanf"%d", p);8len++;9p++;Ten     } One fclose (stdin); A}
1) in the function of input and output data, when the pointer does a formal parameter, the address of the pointer is not modified in the function, only the content pointed to by the pointer is modified, that is, *p. 2) in conjunction with stacks (stack) understanding, function Pass parameters and temporary variables are stored in the stack, when the function exits, the contents of the stack will be released. 3) Therefore, the address of the pointer is not changed, even if it is done in the function of the p++ operation. Change the value of (*p) by scanf

Makefile, can be used to compile GCC and ARM-LINUX-GCC versions
CC: = gccobjs:= esort.oesort: $ (OBJS)    -o esort$ (OBJS): esort.c    -C esort.c-o $ ( OBJS)    ARMCC:= arm-linux-gccarmobjs:= arm_esort.oarm_esort: $ (ARMOBJS)    -o arm_ esort$ (ARMOBJS): esort.c    -C esort.c-o $ (ARMOBJS) Clean    :    rm. /* . o    rm./esort    RM./arm_esort
Compiling the GCC version

[[email protected] pjesort]$ make
Gcc-c Esort.c-o ESORT.O
GCC Esort.o-o esort

Compiling the ARM-LINUX-GCC version

[email protected] pjesort]$ make Arm_esort
Arm-linux-gcc-c Esort.c-o ARM_ESORT.O
ARM-LINUX-GCC Arm_esort.o-o Arm_esort

Status of implementation:
[email protected] pjesort]$ cat Data.txt255  -  +  the  - 171 Ten  +[email protected] pjesort]$./esort input data:255     -     +     the     -    171    Ten     +esort Data:Ten     -     +     -    171     the    255     +--exit--

Code Link:

http://Pan.baidu.com/s/1o83wqgi

Take bubble sort as an example--malloc/free redirect stdin stdout

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.