Pointer as function parameter, pointer function parameter

Source: Internet
Author: User

Pointer as function parameter, pointer function parameter

Demonstration Result 1:

Demonstration Result 2:

Sample Code:

# Include <iostream> void swap (int *, int *); void swap1 (int *, int *); void function1 (void); void displayIntArray (int *, int ); void bubbleSort (int *, int); void function2 (void); int main (int argc, char ** argv) {// function1 (); function2 (); return 0;}/** demonstrate sorting */void function2 (void) {int array [10] = {9, 1 }; printf ("original array:"); displayIntArray (array, 10); bubbleSort (array, 10); printf ("sorted array:"); disp LayIntArray (array, 10);}/** bubble sort */void bubbleSort (int * array, int arrayLength) {for (int I = 0; I <arrayLength; I ++) {for (int j = 1; j <arrayLength-I; j ++) {if (array [j] <array [J-1]) {swap1 (& array [j], & array [J-1]) ;}}}/** display array */void displayIntArray (int * a, int arrayLength) {for (int I = 0; I <arrayLength; I ++) {printf ("% d", a [I]); /* change a row of five numbers if (I + 1) % 5 = 0) {printf ("\ n ");} */} printf ("\ n");}/** demo transposition */ Void function1 (void) {int a = 0 xffffffff; int B = 0 xfffffe; printf ("a = % d, B = % d \ n", a, B ); swap1 (& a, & B); printf ("a = % d, B = % d \ n", a, B); int c = 0x7fffffff; int d = 0x00000001; printf ("c = % d, d = % d \ n", c, d); swap1 (& c, & d ); printf ("c = % d, d = % d \ n", c, d);}/** switching algorithm 1 */void swap (int *, int * B) {int temp = * a; * a = * B; * B = temp;}/** switching algorithm 2 */void swap1 (int *, int * B) {// the sum of the two numbers must be greater than 0 xffffffff. * A = * a + * B; // 10 9 * B = * a-* B; // 10 1 * a = * a-* B ;}

 

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.