Pointer array, array pointer, function pointer, pointer function summary

Source: Internet
Author: User

Pointer array && array pointers

Char *a[5];      Defines an array of pointers, the elements of which are char * pointer types. Initialization can also hold characters or strings inside. The type of a is char *[5]//if char *a[5]={"red", "white", "Blue", "dark", "green"};//a here is the type char *[], essentially a level two pointer.

That means that the memory represented by a is the address of the first element in the array. The memory that a+1 represents is the address of the second element in the array, and so on ...//a[0] is the address of the "red" string, printf ("%s", A[0]), and//a[0]+1, which represents a position offset on the address of the "red" string. Access to the character ' C '. printf ("%c", * (a[0]+1))//If the address of a is 0x0000000. Then the address of the a+1 is 0x00000008 (64-bit platform) or 0x00000004;int (*PTR) [5]; Defines an array pointer to an array of PTR, whose actual type is char (*) [5], (the number can be ignored) because in some strict compilers there is a different time to warn and then define a two-dimensional array int a[1][5]={1,2,3,4,5};//a of type int [ 1][5] (numbers can be ignored), the dimensions of the columns are the same. P=a is able to assign values.

This means that the array pointer ptr can be seen as a level two pointer, which is used in the same way as Char **a.

from the above we can see that char (*) [], Char [], char * * are actually considered equivalent
#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 (*) []. Type M is char []. These two types can be assigned to each other.

But they are not entirely equal.

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

Execution results such as the following


Common pitfalls

Double n =2.0, *pt=&n;printf ("Double%d%d\n", PT, pt+1);//difference is 8    char c= ' m ', *q=&c;printf ("Char%d%d\n", Q, q+ 1);//difference is 1char **p=&q;printf ("%d%d", p, p+1);//difference is 4printf ("%d%d%d", sizeof (int*), sizeof (double*), sizeof (char*), sizeof (int**));
// can be found from the results. Any type of pointer in memory is four bytes in size holding

                                 ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                                 
/u>

function pointers && pointer functions

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

The following 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 are able to invoke 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 may run in a different way than you think: the function name fun is first converted to a pointer to a single position in memory. The function call operator then calls the function to run the code that starts at that address.


Statement 2 runs an indirect access operation to PF, converting PF to the function name.

But such a conversion is actually unnecessary, and the compiler will convert it back before running the function call operator.

Statement 3 runs the same way as the first two.


A common use of function pointers is to pass function pointers as parameters to functions. The following is a sample example.


Execution Result:

Functions using the above techniques are called callback functions (callback function). The user passes a function pointer as a parameter to other functions. The latter will "callback" the user's function. The comparison function compar_int (void const *,void const *) and Compar_char (void const *,void const *) are different from string writing for the comparative integer type. through to compare (const void *,const void *,Int (*compare) (void const *, void const *)) Different function pointers are passed, and the comparison of different types of data is achieved by the same interface.


Pointer function , pointer function refers to a function with pointers. That essence is a function. The function has a return type (assuming no return value, or no value), just the pointer function return type is a pointer to a type.

Pointer array, array pointer, function pointer, pointer function summary

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.