Pointer articles | teach you to play in a multi-stage clapping hands

Two pointer regarded as a relatively high level of usage, a lot of people because of their complex, difficult to understand and avoid it as much as possible. But in large-scale project development, often can not avoid using two pointers, and learn two pointers can help us consolidate the meaning of existence pointer, two pointers learned, mastered the c language on your sense really. The meaning of existence as a function of the parameter pointer is used, the secondary pointer no exception. As a function of parameters, we put the main function compared to a fully functional system, the sub-function module has a specific function than to make, then for most of the tasks we will have the following two requirements:
1. Functions In order to perform a specific function, from the main function to obtain information, which requires the main function subroutine to enter the appropriate information.
2. The main function in order to better perform system functions, the need to obtain information from the sub-function, which requires the corresponding output information to the subroutine main function.
The above input and output functions, the greatest significance is used as the pointer function parameters guy. Thus, it will expand the following detailed speaking, when two pointers are function parameters, are used for input and output characteristics.

1. As an input pointer

Pointer as an input, the primary function through a pointer to the subroutine of data transfer, the primary function of the data sub-function to operate, it is necessary to allocate memory space in the main function. And when the memory model has the following three pointers are input.

void main()
{
    int i = 0;

    //第一种:指针数组
    char *   p1[] = {"123", "456", "789"};


    //第二种:二维数组
    char p2[3][4]  = {"123", "456", "789"};

    //第三种:malloc二维内存
    char **p3 = (char **)malloc(3 * sizeof(char *)); //int array[3];

    for (i=0; i<3; i++)
    {
        p3[i] = (char *)malloc(10*sizeof(char)); //char buf[10]

        sprintf(p3[i], "%d%d%d", i, i, i);
    }
}

FIG IV below as the memory of the three memory model, the four areas of memory that we should be able to FIG intuitive feel three kinds of two pointer memory model of the similarities and differences.


5529997-3cee378414cde42c.png
5529997-69979958b7d47104.png
5529997-e43c00bc5e8bed18.png
2. pointer as output:

To output information Functions main function, the information generated in the sub-functions in the body, so we need to allocate memory in the subroutine, and the function can be used for the main memory allocated in child functions, memory space needs to be allocated on the heap so that when the subroutine exits, the memory will not be released automatically. Of course, do not forget the free heap space in the main function of memory.

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

 
//指针做输出:被调用函数分配内存 
//求文件中的两段话的长度
int getMem(char **myp1, int *mylen1, char **myp2, int *mylen2)
{
    char *tmp1 = NULL;
    char *tmp2 = NULL;
    tmp1 = (char *)malloc(100);
    if (tmp1 == NULL)
    {
        return -1;
    }
    strcpy(tmp1, "abcdefg");
    *mylen1 = strlen(tmp1);

    *myp1 = tmp1; //间接修改实参p1的值

    tmp2 = (char *)malloc(100);
    if (tmp2 == NULL)
    {
        return -2;
    }
    strcpy(tmp2, "11122233333");
    *mylen2 = strlen(tmp2);

    *myp2 = tmp2; //间接修改实参p1的值
    return 0;
}

int getMem_Free(char **myp1)
{
    if (myp1 == NULL)
    {
        return -1;
    }
    tmp = *myp1;
    free(tmp);  //释放完指针变量 所致的内存空间
    *myp1 = NULL;  //把实参修改成nULL
    return 0;
}


void main()
{
    char  *p1 = NULL;
    int len1 = 0;

    char *p2 = NULL;
    int len2 = 0;

    int ret = 0;

    ret  = getMem(&p1, &len1, &p2, &len2 );

    printf("p1: %s \n", p1);
    printf("p2: %s \n", p2);

    getMem_Free(&p1);
    getMem_Free(&p2);  

    return ;
}

Note: The above code will not make too many analyzes, personal humble opinion, the memory model four districts carved into your cerebral cortex, you will eventually play pointer to applause. emmm, I'll play next. According to the code segment in the figure below four districts and its memory model, we found that only the code free tmp, the final myp1 the pointer will be wild pointer.


5529997-73ea902141d8f88f.png

Then the following code segment IV and its memory model, we found that with a higher level as a pointer parameter field pointers can prevent.

5529997-c45c0040c26252a3.png

Reproduced in: https: //www.jianshu.com/p/fe2d00e6f596

Guess you like

Origin blog.csdn.net/weixin_34192732/article/details/91199901