By value and pass the pointer in the C language

We may have heard by value and pass the pointer in the C language, in other languages, there are also references to a pass that they in the end what difference does it make? If you still can not accurately distinguish between, it's time to take a look.

By value

We are novice C language teacher when he taught, the following is not a way to exchange value of a and b:

#include<stdio.h>
void swap(int a,int b)
{
    int temp = a;
    a = b;
    b = temp;
    printf("swap a = %d,b = %d\n",a,b);
}
int main(void)
{
    int a = 10;
    int b = 20;
    printf("before swap:a = %d,b = %d\n",a,b);
    swap(a,b);
    printf("after swap:a = %d,b = %d\n",a,b);
    return 0;
}

Results are as follows:

before swap:a = 10,b = 20                                                                                                                                                                   
internal swap a = 20,b = 10                                                                                                                                                                 
after  swap:a = 10,b = 20

Can be seen, the values ​​of a and b is not ultimately be exchanged. Beginning a, b values ​​10, 20, and ultimately the same value.

why? Because when passing function parameters are passed  copy of the original data  , that is, the use of internal swap a and b is just a copy of the most initial a and b only, so both internally and swap function of a and b do anything change will not affect the initial value of a and b.

For this reason, we are often told,  do not put directly to the structure directly as a parameter  , so that efficiency will be low. Since the structure itself occupies a large number of bytes, if directly as a parameter, it will produce a larger "copy", this way, also very low efficiency.

We combined the following chart to understand:

First of all boxes in the upper part of FIG. A and b represent the main function of a and b, i.e. the original data, while the lower portion of the block a and b represent the parameters a and b, i.e., "a copy of the original data . " (The latter is true in FIG., The value represents the original upper portion, a lower portion of the function representing a parameter value).

Call swap function before and after the situation is as follows:

Since a copy is always just a and b in the swap operation, and therefore does not affect the original values ​​of a and b. The final exchange can not achieve the values ​​of a and b.

Biography pointer

So in order to solve the above problems, we know, we need to pass a pointer. Code is as follows:

#include<stdio.h>
void swap(int *a,int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
    printf("swap a = %d,b = %d\n",*a,*b);
}
int main(void)
{
    int a = 10;
    int b = 20;
    printf("before swap:a = %d,b = %d\n",a,b);
    swap(&a,&b);
    printf("after swap:a = %d,b = %d\n",a,b);
    return 0;
}

operation result:

before swap:a = 10,b = 20                                                                                                                                                                   
swap a = 20,b = 10                                                                                                                                                                          
after  swap:a = 20,b = 10

Can be seen in this case, a, b values ​​of the real exchange.

Why have traditional values, but also pass the pointer

See here, I do not know whether you will be wondering why when passing a parameter to a function, a value will be passed, would pass a pointer to it? Why pass a pointer parameter values can change it? In fact,  C language, the parameters are passed are passed by value!  In other words,  you think of a pointer is passed by value  , but its value is a pointer type Bale.

Let us to understand why the foregoing by FIG pass pointer can exchange values ​​a, b of:

As it can be seen from the figure, although the function is passed to a point b, and a copy pointer, copy it and is also a point b, and therefore can not be changed while the pointer, but the parameters a and b can change points content, i.e., to change the original values ​​of a and b.

Look pass pointer

If p is a pointer for a memory application, the following code can achieve the purpose of it?

#include<stdio.h>
#include<stdlib.h>
void getMemery(int *p)
{
    /*申请1024个int大小*/
    p = malloc(sizeof(int)*1024);
    if(NULL == p)
    {
        printf("malloc failed\n");
        p = NULL;
    }
}
int main(void)
{
    int *p = NULL;
    getMemery(p);
    printf("address of p is %p\n",p);
    return 0;
}

Through previous content analysis it is certainly not achieve the desired effect.

operation result:

address of p is (nil)

Why is this? We still use the previous knowledge to analyze, because the parameters passed to the function are getMemory a copy, so p within the function is a copy of the external p, so even inside the function, p will point to memory a new application, still outside it will not change the value of p, that p is pointing to NULL.

How to modify it? We need to pass the address of p, that is a pointer to an int pointer.

#include<stdio.h>
#include<stdlib.h>
void getMemery(int **p)
{
    /*申请1024个int大小*/
    *p = malloc(sizeof(int)*1024);
    if(NULL == *p)
    {
        printf("malloc failed\n");
        *p = NULL;
    }
}
int main(void)
{
    int *p = NULL;
    getMemery(&p);
    printf("address of p is %p\n",p);
    free(p);
    p = NULL;
    return 0;
}

Results are as follows:

address of p is 0x144f010

You can see from the operating results, the value of p is changed.

It can be understood with the following diagram:

to sum up

This article summarized as follows:

  • Shape function parameters are the original data of "copy" of the original data can not be changed in the function
  • Function parameters are passed by value, pass on the transmission pointer value is essentially
  • If you want to change the contents of parameters, the parameters will need to pass the address (pointers and references are similar effect), which points to modify the contents by dereferencing
  • These conclusions are not limited to the C language

Guess you like

Origin blog.csdn.net/Abelia/article/details/93773109