C Language Pointer Instance practice

Source: Internet
Author: User

Example 1: Enter a and b two integers, output the values of a and B in order of magnitude.

1#include <stdio.h>2 intMain ()3 {4     int*p1,*p2,*p,a,b;5printf"Please enter a total of two integers:");6scanf"%d,%d", &a,&b);//when entering a number, be sure to use the comma in the English state.7     p1=&a;8     p2=&b;9     if(a<b)Ten     { Onep=P1; Ap1=P2; -P2=p; -     }  theprintf"a=%d,b=%d\n", A, b); -printf"max=%d,min=%d\n", *p1,*p2); -     return 0; -}

The example 2:swap () function implements the value of exchanging two variables.

1 intMain ()2 {3     void swap (int *p1,int *p2);4     intb;5     int *pointer_1,*pointer_2;6printf"Please enter a and B:");7scanf"%d,%d",&a,&b);8     pointer_1=&a;9     pointer_2=&b;Ten swap (pointer_1,pointer_2); //swap (A, B) is wrong  Oneprintf"a=%d,b=%d\n", A, b); A     return 0; - } - voidSwapint*P1,int*p2) the { -     inttemp; -temp=*P1; -*p1=*P2; +*p2=temp; -}

To be continued, do not look at the C, review Java, tomorrow after the test to see the pointer part.

C Language Pointer Instance practice

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.