Pointer array, array pointer, function pointer, pointer function summary C + +

Source: Internet
Author: User

pointer array, array pointer, function pointer, pointer function summary
2014-11-11
pointer array && array pointer
?
1234567891011121314 char *a[5];      //定义一个指针数组, 数组的元素都是char *指针类型。初始化也可以在里面存放字符或字符串。a的类型是char *[5]//若char *a[5]={"red","white","blue","dark","green"};//a在这里的类型是char *[],实质上是一个二级指针。也就是说a所代表的那块内存里面存放着的是数组中第一个元素的地址。//a+1所代表的那块内存里面存放着的是数组中第二个元素的地址,依此类推……//a[0]就是“red”字符串的地址,printf("%s",a[0]);//a[0]+1,表示在"red"字符串的地址上偏移一个位置,访问字符‘c‘.printf("%c",*(a[0]+1))//若a的地址是0x0000000,则a+1的地址是0x00000008(64位平台)或0x00000004;int (*ptr)[5];    //定义一个指向数组的数组指针ptr,它的实际类型为char (*)[5],(数字可以忽略)因为在有些严格的编译器中不同时会提出警告再定义一个二维数组int a[1][5]={1,2,3,4,5};//a的类型为int [1][5](数字可以忽略),列的维度相同,p=a可以赋值。也就是说此时数组指针ptr可以看做一个二级指针,它的用法跟char **a相同。
From the above we can see that char (*) [], Char [], char * * can actually be considered equivalent
?
1
?
123456789101112131415 #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; }</stdio.h>
@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

Common pitfalls

?
12345678910 double n =2.0, *pt=&n;printf("double %d %d\n", pt, pt+1);//差为8    char c=‘m‘, *q=&c;printf("char %d %d\n", q, q+1);//差为1char **p=&q;printf("%d %d",p ,p+1);//差为4printf("%d %d %d",sizeof(int*), sizeof(double*), sizeof(char*), sizeof(int**));
From the results, it can be found that no matter what type of pointer in memory is accounted for four bytes of the size of the storage


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.

?
123456789101112131415161718192021222324252627282930 #include <stdio.h>#include <string.h>int compare_int(void const *a, void const *b){    if( *(int *)a == *(int *)b)        return 0;    else        return 1;}int compare_char(void const *a, void const *b){    if(strcmp ((char *)a, (char *)b) == 0)        return 0;    else        return 1;}void Compare(void const *a, void const *b,int (*compare)(void const *, void const *)){    if(compare(a, b) == 0)        printf("they are equal\n");    else        printf("not equal\n");}int main(void){    int m=4,n=5;    Compare(&m, &n, compare_int);    char a[4]="abc",b[4]="abc";    Compare(a, b, compare_char);} </string.h></stdio.h>

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 * , the const void *,int (*compare) (void const *, void const *)) passes 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.

Pointer array, array pointer, function pointer, pointer function summary C + +

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.