C language function parameter is how to pass?

Foreword

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

void swap(int a,int b)

{

int temp = a;

a = b;

b = temp;

printf(''swap a = %d,b = %d'',a,b);

}

int main(void)

{

int a = 10;

int b = 20;

printf(''before swap:a = %d,b = %d'',a,b);

swap(a,b);

printf(''after swap:a = %d,b = %d'',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.

If there want to learn programming beginners, you can come to our C / C ++ technology learning buckle qun oh: 58,934,83-89 free of charge to the entire system inside the C / C ++ tutorials!

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:

Value transfer

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:

Call before and after the swap

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

void swap(int *a,int *b)

{

int temp = *a;

*a = *b;

*b = temp;

printf(''swap a = %d,b = %d'',*a,*b);

}

int main(void)

{

int a = 10;

int b = 20;

printf(''before swap:a = %d,b = %d'',a,b);

swap(&a,&b);

printf(''after swap:a = %d,b = %d'',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:

Biography pointer

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.

Xiaobian to recommend a good learning environment super place, C / C ++ exchange penguin skirt: 870 + 963 + [251] for college students, white, want to change careers, want to find a job through this to join. Skirt, there are a lot of learning materials, a large God answers communication problems, every night live free courses

Look pass pointer

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

#include

#include

void getMemery(int *p)

{

/ * Application 1024 int size * /

p = malloc(sizeof(int)*1024);

if(NULL == p)

{

printf(''malloc failed'');

p = NULL;

}

}

int main(void)

{

int *p = NULL;

getMemery(p);

printf(''address of p is %p'',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

#include

void getMemery(int **p)

{

/ * Application 1024 int size * /

*p = malloc(sizeof(int)*1024);

if(NULL == *p)

{

printf(''malloc failed'');

*p = NULL;

}

}

int main(void)

{

int *p = NULL;

getMemery(&p);

printf(''address of p is %p'',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/93485710