Pointer array array pointer function pointer function pointer array pointer to function pointer array

Source: Internet
Author: User

1. Array of pointers: arrays of pointers, arrays of pointers

1 int *arr1[// store 10 array of shaping pointers 2char *arr2[4]//  Array of 4-character pointers 3char **arr3[5]//Is also an array of pointers

2. Array pointers: Array pointers are pointers

Shaping pointer: int *pint; A pointer that can point to the shaping data
Float pointer: float *PF; A pointer that can point to floating-point data
Note: Int (*p) [The address of the 10]//array is stored in the array pointer, (P is combined with *, stating that P is a pointer variable, and the pointer is pointing to a

An array of 10-size shaped shapes. So P is a pointer to an array, which is an array pointer. )
Example: int arr[10];
Int (*p) [10]=&arr;
int *arr[10];
int * (*P) [10]=&arr;
Note: The above assignment is legal, [] the priority is higher than the * number, so must be added () to ensure that P and * combined.

3. Use of pointer arrays:

int arr[10]={0};

arr;//represents the address of the first element of the array

&arr;//represents the address of an array

printf ("%p\n", arr);//address of the first element

printf ("%p\n", arr+1); The address of the second element in the array

printf ("%p\n", &arr+1); The address after the last element of the array

(we can see that the results are quite different, and the reader can verify that the address of the array is the same as the address of the first element, but the meaning is different)

4. The address of the array is stored by an array pointer. So when you pass a parameter, if you pass the array name of a two-dimensional array to the parameter, we know that the array of the two-dimensional array

The name is the address of the first row of the array, which means that the array name is the address of a one-dimensional array, so the function should be received with an array pointer when it receives the value passed by the argument.

5. Pointers and arrays of definitions and declarations
The definition does not exist when it is to let him exist, and the statement is not known to let him know.

1 // test.c 2 // definition of an array 3 Char arr[]="abcdef"; 4 // the definition of a pointer 5 Char *p="abcdef";
1 //main.c2 extern Chararr[];3 extern Char*p;4 intMain ()5 {6printf"%s\n", arr);7printf"%s\n", p);8 return 0;9}

Readers can experiment on their own, the small part here to give the results of the operation:

A. Define an array, declare it as a pointer, and run the result such as:

For it to output correctly, you can output as follows:

B. If defined as a pointer, declare an array:

To output correctly, you can output in two ways:

Note: When defining an array, declared as a pointer, the program crashes because the contents of four bytes in the array memory space (in this case, the ABCD Azkar code value 61626364) are passed to the pointer, and the pointer treats it as an address, but the address is an illegal address, the pointer accesses an error, and the program crashes. Defined as a pointer, when declaring an array, outputs a random value because the address of the pointer is output as a character, so it is a random value.


6. Array parameters, pointer parameters

When writing code, it is unavoidable to pass an array or a pointer to a function, how is the parameter of the function designed?

A. One-dimensional array parameters:

1#include <stdio.h>2 voidTestintArr[])//one-dimensional array is passed to an array, OK3 {}4 voidTestintarr[Ten])//OK5 {}6 voidTestint*arr)//OK7 {}8 voidTest2 (int*arr[ -])//pointer array to pointer array9 {}Ten voidTest2 (int**arr)//OK One {} A intMain () - { -    intarr[Ten]={0}; the    int*arr2[ -]={0}; - test (arr); - test2 (ARR2); -    return 0; +}

B. Two-dimensional array parameters:

1#include <stdio.h>2 voidTestintarr[3][5])//OK3 {}4 voidTestintArr[][])//error, no column5 {}6 voidTestintarr[][5])//OK7 {}8 //Summary: Two-dimensional array parameters, the design of function parameters can only omit the first [] number9 //because for a two-dimensional array, you can not know how many rows, but you must know how many elements of a row. Ten voidTestint*arr)//error, because Arr is a two-dimensional array, the array name is the first row, which is a one-dimensional array,You cannot pass an array to a primary pointer, it should be passed to the array pointer A {} - voidTestint*arr[5])//Here the parameter is an array of pointers, not an array pointer, error - {} the voidTestint(*arr) [5])//OK - {} - voidTestint**arr)//error, cannot pass one-dimensional array to level two pointer - {} + intMain () - { +   intarr[3][5]={0}; A test (arr); at   return 0; -}

C. First-level pointer-pass parameter:

1#include <stdio.h>2 voidPrintint*p,intsz)3 {4   intI=0;5    for(i=0; i<sz;i++)6   {7printf"%d\n", * (p+i));8    }9 }Ten intMain () One { A   intarr[Ten]={1,2,3,4,5,6,7,8,9}; -   int*p=arr; -   intsz=sizeof(arr)/sizeof(arr[0]); the   //Primary Pointer p, passed to function - print (P,SZ); -   return 0; -}

D. Two-level pointer-pass parameter:

#include <stdio.h>void Test (int *ptr) {  printf ("num=%d\n ", * *ptr);} int Main () {  int n=0;   int *p=&n;   int **pp=&p;  Test (PP);  Test (&P);   return 0 ;}

E. Function Pointers:

1#include <stdio.h>2 voidTest ()3 {4printf"hehehe\n");5 }6 intMain ()7 {8printf"%p\n", test);9printf"%p\n",&test);Ten   return 0; One}

The reader can verify that the output is the address of the same piece of space, stating that the function name is the address of the function. Similarly, the address of the function can be stored in a pointer, that is, the function pointer, with the function pointer, there is a function pointer to the array, that is, the function pointer array, in the same way, with the function pointer array, there is a function pointer array pointer. The small part is not here specifically elaborated. For the purpose of function pointers, there are applications in many places, such as transfer tables, calculators, and readers interested in reading related books.

Pointer array array pointer function pointer function pointer array pointer to function pointer array

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.