Talking about pointers and arrays in C language

1. The difference between int *p =NULL; and *p =NULL

1.int * p =NULL

int *p=NULL;

write picture description here
write picture description here
Define a pointer variable p, and the memory pointed to by it stores data of type int; when defining the variable p, set the value of p to 0x00000000 instead of setting the value of *p to 0x00000000

2.*p =NULL

int i =10;
int * p =&i;
*p =NULL;
  • int i = 10;
    int i =10;
  • int * p =&i;

write picture description here

  • *p =NULL;
    write picture description here
    the memory pointed to by p has changed from the original 10 to 0; and the value of p itself (memory address) has not changed

2. The difference between a and &a

    int a[5] = { 1,2,3,4,5 };
    int *ptr = (int *)(&a + 1);
    printf("%d,%d", *(a + 1), *(ptr - 1));

write picture description here
* &a: a exists alone and is the first address of the array a
* &a+1: a is the first address of the array, the first address of the array plus (number of array elements)
    sizeof (int) is the first address of the next array,
so two The number differs by 20 bits

  • a: The address of the first element of the array a[0]
  • a+1: the first address of the next element of the array
    write picture description here
  • The same value has different meanings,
    so the difference between the two numbers is 4 bits.
  • (a+1): It is not placed in parentheses alone, and it is downgraded to the address of the first element, which
         is the first address of the next element of the array 
         
  • (ptr-1): ptr points to a[5], and ptr is of type int*, so he subtracts 1 to point to a[4] 

3. Pointer arrays and array pointers

1.int *p1[10];

int *p1[10];

Pointer array: an array that stores pointers (the elements of the array are all pointers, and the bytes occupied
     by the array are determined by the array itself)
     

2.int (*p2)[10];

int (*p2)[10];

Detailed explanation

int (*) [10]`(指针类型)
p2`    (指针变量)

Array pointer: a pointer to
     an is unknown)
Note: ()>[]>*
operation priority quick check link
https://blog. csdn.net/csdn_kou/article/details/80139099

4. Function pointers

char (*fun1)(char p1,char *p2)

The concept of function pointers

char *(*fun1)(char * p1,char *p2)

Detailed explanation

char *(*)(char * p1,char *p2)(指针类型)
fun1(指针变量)

The role of function pointers:

call method

char * fun(char *p1, char *p2)
{

}
int main()
{
    char *(*p)(char * p1, char *p2);
    p = &fun;
    (*p)("aa", "nn");
    system("pause");
    return 0;
}

The above example we can not see his advantage.
Array of function pointers
Example 2: (with comments)

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <stdlib.h>

/*四种算法*/
int Add(int x, int y)
{
    return x + y;
}
int Sub(int x, int y)
{
    return x - y;
}
int Mul(int x, int y)
{
    return x * y;
}
int Div(int x, int y)
{
    return x / y;
}

void calc(int(*p)(int, int))
{
    //calc(int Add(int ,int )
    int ret = 0;
    int x = 0;
    int y = 0;
    printf("请输入两个操作数:>");
    scanf("%d%d", &x, &y);
    ret = p(x, y);
    // Add(x,y);
    printf("ret = %d\n", ret);
}

int main()
{
    int (*p[5])(int, int) = {0, Add, Sub, Mul, Div};
    //指针变量p[5],内部5个元素,加减乘除

    int input = 0;
    do
    {
        scanf("%d", &input);

        if(input>=1 && input<=4)
        calc(p[input]);
        //calc(Add);//Add 指针类型是 int (*)(int ,int )
        else
        printf("退出\n");
    } while (input);
}

Tips for doing questions

  • draw
  • Arrays are stored contiguously in memory, not in matrix form
  • In a 32-bit system, no matter what type of pointer, it will always occupy only four bytes
  • Memory distribution of two-dimensional array a[i][j] = * (*(a+i)+j)
  • The function itself has no type, the return value of the function has a type.
    Supplement later

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325594326&siteId=291194637