The difference between pointer arrays and arrays of pointers & pointer functions and function pointers __ function

Source: Internet
Author: User

The nature of the pointer array and the pointer function is unchanged, or arrays and functions. and the array pointer and function pointer, which is the indication that this is the pointer, points to the types of arrays and functions respectively.

One

Array of pointers: the array that is used to store pointers, that is, the array elements are pointers

Char Const *keyword[]={"We", "we", "Wexx"};//represents an array of pointers to character types that point to different string addresses
int len=sizeof (keyword)/sizeof (keyword[0]);//compute the size of the pointer array
printf ("%d", sizeof (keyword[0]));//4
printf ("The largest number is:%d\n", Len);//3

Array pointer: A pointer to a array, which points to the array pointer

Also note the differences in their usage, as illustrated below.

int* a[4] Pointer array

Indicates that the elements in array A are int pointers (that is, four addresses that point to integer data are stored)

Element means: *a[i] * (A[i]) is the same, because [] priority is higher than *

int (*a) [4] Array pointer

"A" is an address variable that points to (or address type) a row containing an array address of four elements

Element representation: (*A) [i]

Example: In the following function, apricot points to three rows of five rows of data table, apricot to access the next page of three lines, through the apricot+1, rather than apricot[1],apricot[1] equivalent to * (apricot+1), It points to the five-element data group.

int main ()
{
int apricot[2][3][5]={{{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}},
{{16,17,18,19,20},{21,22,23,24,25},{26,27,28,29,30}}};//Two vertical three rows five columns, the following pointers point to a different number of dimensions
int (*r) [3][5]=apricot;//refers to the first address of a two-vertical array int type
int (*t) [5]=apricot[0];//refers to a three row array int type of first address
int *s=apricot[0][0];//represents the first address of a five-column array int type
int u=apricot[0][0][0];//represents an array element
printf ("%x\n", ***r);
printf ("%x\n", **t);
printf ("%x\n", *s);
printf ("%x\n", * * * * (r+1));
printf ("%x\n", * * (t+1));
printf ("%x\n", *s+1);
return 0;
}

The values printed separately are:

1

1

1

10

6

2
Two

Pointer function: Just his return value type is a pointer variable

Function pointer: is a pointer variable, except that it is a pointer to a function

such as function int * A (intnum) is a pointer that returns an integral type

the Int (*PF) (intnum) is a function pointer, and the type that points to is function int * A (int num)

So with PF = A, the first address of function A is assigned to the PF 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.