Explanation of parameters for passing pointers in C Language (code example) and pointer example

Source: Internet
Author: User

Explanation of parameters for passing pointers in C Language (code example) and pointer example

In the textbook, the pointer will certainly pass the pointer as a function parameter, and it will generally be explained through a routine "using the pointer as a parameter to change the variables in the main function. Because the parameter is generally transmitted as a copy of the remote variable, the parameter passed to the function is not the original variable, therefore, directly modifying the variables in the main function becomes meaningless. Passing pointer variables makes up for this shortcoming, so that we can directly modify the variable values in the main function in the function; well, the beginner has learned well.

So I got the code of a younger brother;

#include
 
  #define N 100void mcopy(char *s,char *t,int m) {t = s + (m - 1);}int main(){char str1[N],str2[N];int m;gets(str1);scanf("%d",&m);mcopy(str1,str2,m);puts(str2);return 0;} 
 

This is a string truncation code. It is meant to capture the string str1 from the m-bit and save it to str2 and output str2;

Simple code. Let's take a look at the main function:

Read str1, read m value, and then call

mcopy(str1,str2,m);

Take a look at this function:

Pass three parameters, two pointers, one integer;

Execute internally

t = s + (m - 1);

That is to say, add the address of the pointer s to the (S-1) bit and assign the value to the pointer t, so that the m-bit of the pointer s is intercepted and the pointer t is completed;

It seems that there is no problem ....

But it is an error code. Why?

In fact, here we should understand 1.The pointer is actually a memory address. This memory address is the pointer to the address. The content of this address and its later addresses is what we call the pointer.. 2.When passing parameters, a function must pass parameters.

That is to say, in fact, the pointer passed as a parameter is also a form parameter. It is also a copy of the original pointer variable. The content in the above Code does not modify the pointer variable in the main function, the modified parameter pointer does not make any sense.

Some people may ask why this is different from the example of "using pointers as parameters to change variables in the main function ??

In this case, the pointer passed in the example of "using a pointer as a parameter to change the variables in the main function" is also a form parameter. We only modify the pointer instead of the form parameter, we modify the content in this address through this parameter address. Of course, we can modify the variables in the main function.

Example:

Set a to a variable with address a, B to a pointer variable in the main function, and B to a. C to a pointer variable that calls the function;

When calling a function: B runs to Party C and says to Party C, "the address I am pointing to is a." (at this time, party C is the form parameter of Party ). Then, if we modify C, it will have no effect on Pointer variable B. However, if the function modifies the content in the address of C, it will directly modify variable;

This is the difference ....

For the above Code, to achieve the goal;

The statement is as follows:

#include
 
  #define N 100char *mcopy(char *s,char *t,int m) {t = s + (m - 1);return t;}int main(){char str1[N],*str2;int m;gets(str1);scanf("%d",&m);str2 = mcopy(str1,str2,m);puts(str2);return 0;} 
 

Because we cannot directly modify this parameter, it is enough to return a pointer.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.