C Language Basics _ Pointers

Source: Internet
Author: User

First, the pointer variable

1. Variables for storing addresses

2. * Indirect address character

3.%p The format character of the print address

4. & Take address character to get the storage unit address of the variable

5, int *p = &a p points to a storage unit [p is a variable where the pointer stores the address]

6. *p storage unit (content) for accessing variables via address

Second, the pointer

    int a[5]={3,4,1,2,5};       int *p = A; // define a pointer to the first address    printf ("p[2] =%d\n", p[1]);    printf ("%d\n", * (p+1));  

Three, a string pointer

    CharStr[] ="Hello"; Char* PS =str; intCount =0;//int n = 0;     for(inti =0; Str[i]! =' /'; i++) {//* (ps+n)! = ' + ';printf"%c", *ps);//n++;ps++; Count++; } printf ("%d", count);

Iv. exchange of two variables

voidSwapint*a,int*b) {    inttemp = *A; *a = *b; *b =temp; printf ("%d%d", *a,*b);} intMainintargcConst Char*argv[]) {    intA =1; intb =2; Swap (&a,&b);return 0;}

Five, array sorting

voidAddint*arr,intLen) {     for(inti =0; I < len-1; i++) {         for(intj =0; J < Len-1-I.; J + +) {            if(* (ARR+J) > * (arr+j+1)) {                inttemp = * (arr+j); * (ARR+J) = * (arr+j+1); * (arr+j+1) =temp; }        }    }     for(inti =0; i < Len; i++) {printf ("%d", * (arr+i)); }}intMainintargcConst Char*argv[]) {        intArr[] = { -, the, at, About,Panax Notoginseng}; Add (arr,5); return 0; }

1, array sort 2

voidAddint* p,intLen) {     for(inti =0; i<len-1; i++) {         for(intj =0; J < len-1-I.; J + +) {            if(* (P+J) >* (p+j+1)) {                inttemp; Temp= * (p+j); * (P+J) = * (p+j+1); * (p+j+1) =temp; }        }    }     for(inti =0; i < Len; i++) {printf ("%d", * (p+i)); }}intMainintargcConst Char*argv[]) {        intArr[] = { -, the, at, About,Panax Notoginseng}; int* p =arr;//defines the pointer to the first address of Add (p,5); return 0; }

C Language Basics _ Pointers

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.