Linux C Program (TEN)

Source: Internet
Author: User

Pointer arrays and level two pointers

1#include <stdio.h>2 3 intMain () {4         inta[5] = {1,3,5,7,9};5         int*p[5], I;6         int**PP =p;7     //point the P pointer array to each a8          for(i =0; I <5; i++){9P[i] =&A[i];Ten         } One     //"" Priority is higher than *, * (P[i]) p[i] = &a[i],so==> *p[i] = *&a[i] = A[i] A          for(i =0; I <5; i++){ -printf"%d",*p[i]); -         } theprintf"\ n"); -     //p and PP are all level two pointers, i=0, *pp=p[0], p[0]=&a[i],*pp=a[0]. -     //after the pp++ is executed, pp points to p[1],*pp=p[1],p[1]=&a[1]. -          for(i =0; I <5; i++,pp++){ +printf"%d",**pp); -         } +      A         return 0 ; at } - Output: - 1 3 5 7 9 - 1 3 5 7 9


The relationship of pointers to arrays

1Nclude<stdio.h>2 3 intMain () {4         inta[2][5] = {1,3,5,7,9,2,4,6,8,Ten};5         //indicates that P is a pointer to a one-dimensional array containing five elements6         int(*p) [5],i;7         //p points to the first row of a two-dimensional array8p =A;9     //after P=a, p=&a, *p = a[1].Ten     //p is an array pointer One          for(i =0; I <5; i++){ Aprintf"%d",(*p) [i]); -         //(*P) [i] is the first to take the contents of P as the starting address of the array, and then go to the first element.  -         //*p[i] is the first element that takes p as the starting address, which is a pointer and then takes the contents of the pointer.  the         } -printf"\ n"); -     //p move to the second row -p++; +          for(i =0; I <5; i++){ -printf"%d",(*2) [i]); +         } Aprintf"\ n"); at  -         return 0; - } -  - int(*p) [5] ;//A pointer - int*p[5]  ;//An array of five elements with a length of 5, with each element in the array pointing to an integer variable. 


Functions and pointers
1. Pointers as Function parameters
The function is to transfer the address of a variable into a function.

1#include <stdio.h>2 voidChangeintIint*p) {3i++;4         if(P! =NULL) {5(*p) + +;//find variable b,b+1 by address6         }7 }8 intMain () {9         intA =5, B =Ten ;TenChange (a,&b); Oneprintf"a=%d,b=%d", A, b); A         return 0; -}




2. return pointer to function
1 int *f ( int i, int j);
() has a higher priority than *
Indicates that f is a function with a pointer to the function name, indicating that the function has a return value type of pointer.

1Include<stdio.h>2 //defines a pointer array named name, with each array element pointing to a string3 Char*name[7] = {"Monday","Tuesday","Wednessday","Thursday","Friday","Saturday","Sunday"};4 //defines a pointer that points to a string5 Char*message ="wrong input";6 //functions that return pointers7 Char*week (intDay ) {8         if(Day <0|| Day >7){9                 returnmessage;Ten}Else{ One                 returnname[day-1] ; A         } - } -  the  - intMain () { -         intDay ; -         Char*p; +  -printf"input a number of a week:\n"); +scanf"%d",&Day ); A      atp =Week (day); -printf"%s\n", p); -  -         return 0; -}



3. Pointer to function 87 page----------

Linux C Program (TEN)

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.