Array pointers, pointer arrays, function pointers, pointer functions-----notes

Source: Internet
Author: User

1. Four ways to access an array

Define array a[]; Pointer *p points to array A;

(1) Access a[i using the following table of the array];

(2) Array name + address offset I * (a+i)

(3) Use the pointer p to access the table below P[i]

(4) The offset of the pointer p + address I * (p+i)

One-dimensional array name: equivalent to a single pointer

2.

Array pointer (pointer) array of pointers (arrays)

function pointer (pointer) pointer function (function)--------> Look at the back two words to distinguish between a pointer or an array or a function

_____________________________________________________________________________

Array pointer: (pointer to all address of array)

int a[3]={2,3,4}

int *p = A; Pointer to array

int *P1 = &a[0]//Pointer to the first element of the array

int (*P2) [3] = &a; Array pointer to the full address of the array

3-> points the number of data in the array

&a represents the entire address space from start to end of an array

_____________________________________________________________________________

Pointer array: (an array that holds multiple addresses)

int a=10;

int b=20;

int c=30;

int *pa[3]={&a,&b,&c}; Save is int* (address)

_____________________________________________________________________________

function number pointer (pointer to function)//function name represents the entry address

Three elements: Same as the return value type of the indicated function, with the same parameter type and the same number of parameters.

int Max (int a,int b) {}

Int (*pfun) (int,int);

Main ()

{

Max (12,13);

pfun=&max; ----> Pfun=max;

(*pfun) (12,13); ----> Pfun (12,13);

}

_____________________________________________________________________________

Pointer function (return type is pointer type called pointer function)

Definition Format: Return type identifier * Function name (formal parameter table)

{function Body}

_____________________________________________________________________________

3 How to differentiate:

Array pointers:

define INT (*p) [n]; () A high priority, first stating that P is a pointer to a one-dimensional array of integers,

The length of this one-dimensional array is n.

Array of pointers:

define int *p[n]; [] High priority, first combined with p to become an array, and then by int* that this is an integral type

A pointer array that has n pointer-type array elements.

function pointers:

define INT (*pfun) (int,int); *pfun in parentheses, the pointer is first formed, and the brackets are still parentheses.

Point to a function with a return value of shaping, and this function has two shaping as a formal parameter.

pointer function:

define int *fun (); Equivalent to int* fun (); The return value is a shaped pointer.

_____________________________________________________________________________

4. Complex pointers:

Right-left rule: the identifiers that have never been defined begin to read, then look to the right and then to the left. Turn the direction each time you encounter parentheses.

Once you have finished parsing all the parentheses inside, jump out of the parentheses. Repeat the process until the parsing is complete.

Int (*fun) (int *p);

First find the undefined identifier, is fun, it has a pair of parentheses outside, and the left is an * number, which means

Fun is a pointer, then jumps out of this parenthesis, first look to the right, is also a parenthesis, which means (*func) is a function,

And fun is a pointer to such a function, which is a function pointer, which has a parameter of type int*, and the return value type is int.

Int (*fun[5]) (int *p);

Fun right is a [], the description of fun is a 5 elements of an array, fun's left there is a *, that the fun element is a pointer,

Note that this is not a fun, but a cosmetic fun[5], because [] operator precedence is higher than *, fun first with [],

So the * modifier is func[5]. Jump out of this parenthesis, look to the right, also a pair of parentheses, indicating that the elements of the Func array are function types

Pointer to a function that has a parameter of type int* and a return value of type int.

Array pointers, pointer arrays, function pointers, pointer functions-----notes

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.