Fast learning C pointer

C language pointer

(1) Definition of various pointers

1. An integer: int a;

2. A pointer to an integer: int *a;

3. A pointer to a pointer, which points to an integer: int **a;

4. An array with 10 integers: int a[10];

5. An array with 10 pointers, and each pointer points to an integer: int *a[10];

6. A pointer to an array with 10 integer numbers: int (*a)[10];

7. A pointer to a pointer. The pointed pointer points to an array with 10 integer numbers: int (**a)[10];

8. A pointer to an array, which has 10 integer pointers: int * (* a) [10];

9. A pointer to a function that has an integer parameter and returns an integer number: int (*a)(int);

10. An array with 10 pointers. Each pointer points to a function. The function has an integer parameter and returns an integer number: int (*a[10])(int);

11. Pointer to a function. The type of the function pointed to is a function with two integer parameters and returns a function pointer. The returned function pointer points to a function with one integer parameter and returns an integer number: int (* (* a) (int, int)) (int);

(2) Introduction to various pointers

1.int a;

A is a variable that points to a spatial address in memory. The address space is obtained through & A, and the value in the address space can be obtained through a.

2.int *a;

Defines a pointer a to an integer number. A is a pointer whose return value is an integer number. There is a variable int x; a=&x; A points to the address space of X. The data in the address space can be retrieved through * a, and the value is consistent with X.

Let's take a closer look at the code

#include<stdio.h>

int main()
{
        int a = 10;
        int *p = &a;
        printf("Variable name access a: %d\n",a);
        printf("a Your address is:%p\n",&a);
        printf("Address access a:%d\n",*(&a));
        printf("Pointer access a Your address is%p\n",p);
        printf("Pointer variable access a:%d\n",*p);
        return 0;
}

The operation results are as follows:

3.int **a;

A secondary pointer A is defined. The return value is an integer pointer with variable int y; int *b=&y; int **a=&b;

b points to the address of y, a points to the address of b, b stores the address of y, and a stores the address of b. the address of b can be obtained through * A. because it is a secondary pointer, * * a takes the value of y.

Code example

#include<stdio.h>

int main()
{
        int a = 10;
        int *p = &a;
        int **pp = &p;
        printf("Access by variable name a:%d\n",a);
        printf("Access by address a:%d\n",*(&a));
        printf("Through the first level pointer p visit a:%d\n",*p);
        printf("Secondary pointer pp visit a:%d\n",*(*pp));
        printf("a Your address is:%p\n",&a);
        printf("Primary pointer: a Address of%p\n",p);
        printf("p Your address is:%p\n",&p);
        printf("Secondary pointer pp Access printing p Address of:%p\n",pp);
        return 0;
}
~  

Operation results

4.int a[10];

Defines an array of integer numbers with a size of 10 and 10 elements. You can access the data of the array by adding a subscript to the array name. The subscript is 0-9, and others are out of bounds.

Code example

#include<stdio.h>

int main()
{
        int a[10] ={1,2,3,4,5,6,7,8,9,10};
        int size = sizeof(a)/sizeof(a[0]);
        for(int i=0;i<size;i++){
                printf("%d ",a[i]);
        }
        putchar('\n');

        return 0;
}

Operation results

5.int *a[3];

A pointer array is defined. Each element in the array is a pointer to a spatial address in memory.

For example, there are three variables int, x, y, Z; int *a[3]={&x,&y,&z};

Point the three pointers to the address space of x, y and z respectively.

Code example:

#include<stdio.h>

int main()
{
        int a = 10;
        int b = 20;
        int c = 40;
        int *arr[3] = {&a,&b,&c};
        for(int i=0;i<3;i++){
                printf("%d ",*arr[i]);
        }
        putchar('\n');
        return 0;
}

Operation results

6.int (*a)[3];

Array pointer, defining a pointer to an array.

Array pointers are really equivalent to two-dimensional array names.

Code example

#include<stdio.h>

int main()
{
        int arr[3][4] = {{11,22,33,44},{12,13,14,15},{13,45,67,88}};
        int (*a)[4];
        a = arr;
        for(int i=0;i<3;i++){
                for(int j=0;j<4;j++){
                        printf("%d ",*(*(a+i)+j));
                }
                putchar('\n');

        }
        putchar('\n');
        return 0;
}

Operation results

void (*p)();

A parameterless function pointer with a return value of void

Example

#include<stdio.h>

void Print()
{
        puts("Program start, welcome!");
}
int main()
{
        void (*p)();
        p = Print;
        (*p)();

        return 0;
}

Operation results:

int (*p2)(int );

The return value is of type int, and the formal parameter is a function pointer of type int

code

#include<stdio.h>

void Print()
{
        puts("Program start, welcome!");
}
int GetData(int data)
{
        printf("%d\n",data);
}

int main()
{
        void (*p)();
        int (*p2)(int);
        p2 = GetData;
        p = Print;
        (*p)();
        (*p2)(10);
        return 0;
}

Operation results

7.int(**a)[2];

Equivalent to a secondary pointer, it stores the address of a pointer, which points to an array with 2 elements.

Code example

#include<stdio.h>

int main()
{
        int arr[2][2]={{1,2},{3,4}};
        int (*p2)[2] = arr;
        //int **a = &p2;
        int (**a)[2] = &p2;
        for(int i=0;i<2;i++){
                for(int j=0;j<2;j++){
                        printf("%d ",arr[i][j]);
                }
                printf("\n");
        }
        printf("\n");
        //printf("%d\n",arr[0][0]);
        for(int i=0;i<2;i++){
                for(int j=0;j<2;j++){
                        printf("%d ",*(*(p2+i)+j));
                }
                printf("\n");
        }
        printf("\n");

        for(int i=0;i<2;i++){
                for(int j=0;j<2;j++){
                        printf("%d ",*(*(*a+i)+j));
                }
                printf("\n");
        }
        putchar('\n');
        return 0;
}

Operation results

8.int*( *a)[2];

It is equivalent to a secondary pointer. It stores the address of an array. There are 2 pointers of integer numbers in the array.

9.int *a(int);

This is a function pointer to the address space of a function. The function has a shaping parameter.

10.int (*a[3])(int );

This is an array. The array contains three pointers, and each pointer points to a function. That is, an array of function pointers.

code

#include<stdio.h>
int GetMax(int a,int b)
{
        return a>b?a:b;
}

int GetMin(int a,int b)
{
        return a<b?a:b;
}

int GetSum(int a,int b)
{
        return a+b;
}
int main()
{
        int a = 10;
        int b = 20;
        int ret;
        int (*p[3])(int,int) = {GetMax,GetMin,GetSum};
        for(int i=0;i<3;i++){
                ret = (*p[i])(a,b);
                printf("ret = %d\n",ret);
        }
        return 0;
}

Operation results

11.int (* ( *a)(int,int))(int);

This is a function pointer. The function it points to is a function with two formal parameters, and the return value is a function pointer. The returned function pointer takes one formal parameter, and the return value is an integer, that is, the returned function pointer points to a function with one formal parameter and the return value is int.

Tags: C C++ Linux

Posted on Sun, 10 Oct 2021 07:49:11 -0400 by freephoneid