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>*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 (&A,&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