C Basic Knowledge----pointer array && array pointer && function pointer && pointer function

Source: Internet
Author: User

Pointer array && array pointers

char (*PTR) [5];    Defines a pointer to an array of PTR, pointing to an array containing 5 char types char *a[5];      Defines a pointer array A, containing 5 char* type pointers
#include <stdio.h>int main (void) {char *a[5]={"red", "white", "Blue", "dark", "green"};    printf ("%s%c%s\n", A[0], * (a[0]+1), a[1]+1);    printf ("%x%x\n", a[0], a[1]);    char m[2][5]={"xxx", "yyyy"};    char (*PTR) [5]=m;    printf ("%x%s%s%x%s\n", ptr[0], PTR, ptr[1], ptr[0]+2, ptr[0]+1);    printf ("%d", sizeof (a));    printf ("%d", sizeof (m)); return 0;}
@1 defines an array of pointers, and the elements in the group are char * types

@2 defines an array pointer, the PTR type is char (*) [The],m type is char [], and the two types can be assigned. But they are not exactly equivalent.

The type of @3 A is Char *[5],sizeof (a) =sizeof (char *); The type of M is a two-dimensional array of char [], and sizeof (m) evaluates the actual bytes it occupies in memory, and some compilers align the bytes for efficiency reasons.

The operation results are as follows


br>                     & nbsp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                   

function pointers && pointer functions

The function pointer, like any other pointer, must be initialized to point to a function before performing an indirect access to the function pointer.

Here is a way to initialize a function pointer:

int fun (int);

Int (* PF) (int) = &fun;

Create the function pointer pf and point it to the function fun. It is important to have a fun prototype before the initialization of a function pointer, otherwise the compiler cannot check that the type of fun is the same as the type that PF points to.


After initializing the declaration, we can call the function in the following three ways:

int ans;

1) ans = fun (25);

2) ans = (*PF) (25);

3) ans = PF (25);

Statement 1 Simple use of the name call function fun. But it's going to be a different process than you think: the function name fun is first converted to a functional pointer that points to a piece of space in memory. The function call operator then invokes the function, executing the code that begins at that address.

Statement 2 performs an indirect access operation to PF, converting PF to the function name. But this conversion is actually unnecessary, and the compiler will convert it back before executing the function call operator.

Statement 3 and the first two perform the same effect.


The common use of function pointers is to pass function pointers as arguments to functions, as shown below.


Operation Result:

Functions using the above techniques are called callback functions (callback function). The user passes a function pointer as a parameter to other functions, which will "callback" the user's function. Compare integer to String, respectively, for comparison function compar_int (void const *,void const *) and Compar_char (void const *,void const *), through to compare (const void * , const void *,Int (*compare) (void const *, void const *)) pass different function pointers, implementing comparisons of different types of data through the same interface.


pointer function, pointer function refers to a function with pointers, that is, essentially a function. The function has a return type (no value if no return value), except that the pointer function return type is a pointer to a type.


Related Article

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.