Pointer array, array pointer, pointer function, function pointer

Source: Internet
Author: User

1. Array of pointers: As the name implies, arrays of pointers are pointers to the elements inside the array, and the code is as follows:

#include <stdio.h>intMainintargcConst Char*argv[]) {    //Insert code here ...//printf ("Hello, world!\n");    intA=1, b=2;//define two variables first    int*p[]={&a,&b};//the array is then defined, and the array elements are pointersprintf"%p\n"-P:0]); printf ("%p\n"-P:1]); return 0;}
View Code

2. Array pointers: That is, the pointer points to the elements in the array, the code is as follows:

#include <stdio.h>intMainintargcConst Char*argv[]) {    //Insert code here ...//Array Pointers        intnums[]={1,2,3,4,5,6};//define an array//int *p=&nums[0];    int*p=nums;//This statement is equivalent to the preceding sentence, which defaults to the first address of the array, which is the address of the first element of the arrayprintf"%d\n",*p); printf ("%d\n", *p+1);//*p+1 can take the next element of the *p corresponding array elementprintf"--------\ n");  for(intI=0;i<sizeof(nums)/sizeof(int); i++)//use an array pointer to facilitate this array{printf ("%d\n", *p+i); }    return 0;}
View Code

3. Pointer function: That is, the function's return value is the function of the pointer:

#include <stdio.h>//defines a function that returns the larger address of two numbersint*max (int*PA,int*PB)//define function, int * function name (parameter){    return*PA&GT;*PB? PA:PB;//returns the address of a large number}intMainintargcConst Char*argv[]) {    //Insert code here ...    intA=5, b=3; printf ("a=%p,b=%p\n", &a,&b);//print addresses for A and B    int*p=max (&AMP;A,&AMP;B);//incoming parameters are the addresses of A and B, respectivelyprintf"%p\n", p);//output larger number of addresses}
View Code
//Enter lowercase numbers, output uppercase days of the week#include<stdio.h>Char*getday (intN//pointer Functions{    //defines an array of pointers to return addresses    Char*days[]=    {        "Monday",        "Tuesday",        "Wednesday",        "Thursday",        "Friday",        "Saturday",        "Sunday",    }; returndays[n-1];//return address}intMainintargcConst Char*argv[]) {    //Insert code here ...//printf ("Hello, world!\n");        Char*pday=getday (6);//defines the return value of a pointer receive functionprintf"%s\n", Pday);//Print out the contents of this pointer        return 0;}
View Code

4. Function pointers: pointers to functions, which are function pointers

Definition method: Type (* function pointer name) (parameter) ....

Not to be continued

Pointer array, array pointer, pointer function, function pointer

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.