Passing parameters of functions and pointers in C Language
Recently I used a binary tree data structure experiment to create a tree using a function without return values. I found that this tree cannot be created, so I will use this example to discuss what is hidden in the passing of pointers in C language functions as form parameters.
We all know that C ++ has a reference concept. If two data references the same data, changing any one is equivalent to changing the ontology, and the value corresponding to the other data will also change, but C does not have this concept. So something is generated. It is different from what we thought.
1. Define the function entry in C:
In C language, the function parameters are responsible for receiving external data. How exactly does the data enter the function? In fact, the parameters we operate in the function body are only a copy of the parameters passed in, that is to say, although the two parameters have the same name and corresponding values, their memory addresses are different. That is to say, they are two completely different variables that "look exactly the same.
Therefore, you must know that functions in C language pass values. That is to say, C language can only pass values to functions, rather than completely put the variables you want to pass into the functions.
2. Pass the pointer to the function:
As a special thing, a pointer is powerful in that it can directly modify the data on the memory address. Although the pointer is particularly powerful, it cannot escape the limitations of the function. You pass a pointer to the function, because it is a value transfer, then the form parameter pointer you use in the function body is also a copy, it's just another variable that points to the same value as the pointer you passed in. That is to say, it is no different from normal constants.
3. How can I achieve the reference effect?
In C, because the value is passed, we will pass the value, as long as the address of the thing to be passed into the function is passed into the function, and the function is received with a pointer, so it is equivalent to passing the variable address intact to the function. The pointer of the form parameter points to the address passed in outside. Isn't it easy to do it with the address.
4. The following is a non-return value version created by a binary tree I wrote:
Because Binary Trees are built recursively, just like creating a linked list, one node is built on one node. If you don't get the address of the previous node, How do you connect the linked list, the linked list is dispersed.
Therefore, each node can be worn only by passing the address to find the precursor to the created linked list.
#include
#include
typedef struct tree { char t; struct tree *lchild; struct tree *rchild;}Tree;void initTree(Tree **T) { char ch; ch = getchar(); if (ch == '#') { *T = NULL; } else { *T = (Tree *)malloc(sizeof(Tree)); (*T)->t = ch; initTree(&(*T)->lchild); initTree(&(*T)->rchild); }}void qianT(Tree *T) { if (T) { printf("%c ",T->t); qianT(T->lchild); qianT(T->rchild); }}int main (void) { Tree *T; initTree(&T); qianT(T); return 0;}
Note that every time I look at the function of building a tree, I wear the address of a node, and then find the established tree through the address to establish the tree. So some people will ask? Why not write the following statement?
void initTree(Tree *T) { char ch; ch = getchar(); if (ch == '#') { T = NULL; } else { T = (Tree *)malloc(sizeof(Tree)); T->t = ch; initTree(T->lchild); initTree(T->rchild); }}
In this way, each time you pass in a variable, it is said that the C language is a value transfer, so each time you apply for a Node space, it is applied for a copy, then recursion is the copy of the left and right children, that is to say, your tree is not built at all, because each request for memory is not connected.
Why can I write this statement? Because I use a pointer pointing to a pointer to store the address. I applied for memory for the passed address, this is equivalent to applying for the memory for the node itself, rather than the copy, so the binary tree is built with its nature.
Finally, remember that C language is passed by value, and everything passed to the function is only a value!