Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between value passing and address passing of CAccord +?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The main purpose of this article is to show you the "what is the difference between the value transfer and address transfer of Cpicket +". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "what is the difference between value transfer and address transfer?"

There is an obvious difference between the value-by-value and address-by-address delivery of Cetc. Here is a difference between them:

Pass by value: in the calling function, copy the value of the original function to the function that was called in the past, and the modification of this value in the called function will not affect the value of the original function.

Pass by address: when calling the function, copy the address where the value of the original function is located. The changes made by the called function to this address will affect the original value.

Overview:

First of all, we need to know the difference between "address of a" and "content of address a". The data is stored in memory, each variable has a memory address, and the contents of the variable are stored in the space corresponding to the memory address.

For example, definition

Int a = 10

So the address of an in memory is 0x1100, and the data stored in this address is 10

Suppose you create a pointer p and assign the address of a to p, that is, assign the first address 0x1100 of a to pointer p. At this time, the value of p is the first address of the variable an in memory.

Int a = 10 intact address / / assign the first address of a to P

A simple understanding is like if you go to the library to borrow a book, each book will have a number (address) to record its location, and this book is the corresponding content of this address.

If you use a pointer, you get the corresponding number (address) of the book, and the stored content is the address.

If you are a variable assignment and value transfer, it is equivalent to copying the book (the content corresponding to the address) and then using a new number (address) to store the book you copied.

In terms of naming

So we will find:

Value transfer, or variable assignment, modify the value of the variable, modify the contents of the new number (address) (copied book), and will not affect the data in the original number (address).

That is, a formal parameter is a copy of the content of the argument, not a copy of the address, so changing the value of the parameter does not affect the value of the parameter.

Using the address to pass and use the pointer to modify the value of the variable is to change the book in the original number (address) to a new book, which is equivalent to the operation of the argument itself.

Declare Declaration: describes objects created elsewhere and does not allocate memory. (can appear in multiple places)

Define Definition: generate a new object and allocate memory. (can only occur once)

Value transfer

Open up a new memory space to store the values of the original variables, and modify the variables to modify the values in the new memory space. Therefore, the original parameters are not modified by the function.

Advantages of value passing: parameters passed by values can be numbers, variables, and expressions. The value of the original parameter is not modified.

Disadvantage of value passing: the value of the original parameter cannot be modified.

# include void swap (int x, int y) {int temp; temp = x; x = y; y = temp;// printf ("x =% d, y =% d\ n", x, y);} int main (void) {int a = 4, b = 6; printf ("before exchange:\ n a =% d, b =% d\ n", a, b); swap (a, b) Printf ("after exchange:\ n a =% d, b =% d\ n", a, b); return 0;}

Output:

Address transfer

Address delivery is pointer transmission, and the formal parameter is actually a pointer to the address of the argument. When operating on the parameter, it is equivalent to the operation on the parameter itself. The value of the pointer pointing to the content can be changed, but the address of the pointer itself cannot be changed.

# include void swap (int * x, int * y) {int temp=*x; * x transactions; * ychangtemp; / printf ("x =% d, y =% d\ n", * x, * y);} int main (void) {int astat4; int bread6; printf ("before exchange:\ n a =% d, b =% d\ n", a, b) Swap (& a _ return _ b); / / the address passed is printf ("after exchange:\ n a =% d, b =% d\ n", a, b); address 0;}

After you understand what value passing and address passing are, let's take a look at an interview question:

# include # include # include void GetMemory (char * p) {p = (char *) malloc;} int main (void) {char * str = NULL; GetMemory (str); strcpy (str, "hello world"); printf (str); return 0;}

What will be the result of the operation? Can you output hello world?

A: the program crashed and there was no output

Because GetMemory does not pass dynamic memory, the str in the Test function is always NULL. Strcpy (str, "hello world"); will crash the program

The p in the function is actually a copy of the argument str. All the operations in the function are performed on Q, and str is still NULL, so the value of the output * str crashes.

That is, the pointer passed to the parameter is still a copy of the argument pointer.

This point needs to be noted.

If you solve the problem, you can change the parameter to a double pointer, and the program can proceed as expected:

The address of the double pointer to the single pointer, that is, what is passed is actually the parameter itself of * str.

# include # include # include void GetMemory (char * * p) {* p = (char *) malloc;} int main (void) {char * str = NULL; GetMemory (& str); strcpy (str, "hello world"); printf (str); return 0 } these are all the contents of this article entitled "what is the difference between the value transfer and the address transmission of CAccord +? thank you for your reading!" I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report