How to return an array from a custom function in C language (detailed explanation with pictures and text)

烟雨青岚
Release: 2020-06-17 12:44:33
forward
7160 people have browsed it


How to return an array from a custom function in C language (detailed explanation with pictures and text)

How to return an array from a custom function in C language (detailed graphic and text explanation)

Recently I saw some questions from classmates, and one mentioned: How to return an array in a function?

Can I write a char * type return value directly in the custom function and return it directly? , the code is as follows:

How to return an array from a custom function in C language (detailed explanation with pictures and text)

## Directly returns the str array name (note that there is no need to add &, but Many students make this mistake)

But in fact, the running result is not normal. We try to output in the calling function, and we can see that the result is not the original content (of course your computer output Maybe it’s not like this yet)

is as follows:

How to return an array from a custom function in C language (detailed explanation with pictures and text)

##The reason is that you can start with the properties of str. str itself is a

local variablein a custom function. It is an array with one hundred bytes. Its life cycle will of course depend on where it is located. Together with the functions, as the saying goes, "One trick for the emperor and one courtier", with the end of the fun function call, various local variables in it will also be recovered by the system. Of course, the one hundred bytes of the str array will also be recovered, so The string "Hello www.dotcpp.com" was wiped out! Naturally, what you output in the main function will definitely not be the original content!

How about it, I can understand it!

However, it is not over yet, some students still continue to ask, why can I change it to the following way of writing?

As shown below:

How to return an array from a custom function in C language (detailed explanation with pictures and text)##Answer: In this case, although str is also The local variable is not an array, but a pointer, only four bytes. It stores the string "Hello www.dotcpp.com" in the constant area, but please note that this string is in the constant area, not The part that belongs to the fun function is readable by the whole program, so it still exists after return. What is returned is the value in str, which is the first address of the string "Hello www.dotcpp.com". It is a number, which is actually equivalent to Transfer the address of this string from str to p through the return value.

You can also use an analogy: before, only the fun function knew this string, but now it is about to die. Before dying, he confessed: "I am about to die, hurry up and put 'Hello www .dotcpp.com's treasure address (the first address of the string) is transferred to the main function!"

Then return and quickly return to the main function! Then it disappears...

Then, after the main function is obtained, you will know...

Can you understand it this way?

The later C language reverse analysis part will also involve the principles here, and you can learn and understand it in depth.

At the same time, in the next article we will explain to you how to implement array passing of custom functions!

Through the explanation in the previous article, I believe everyone has understood the problems and reasons of returning arrays directly. Today we will explain in detail the common methods of returning arrays in functions.

For this kind of problem, the application scenario is often to solve the problem of mutual communication between functions. For example, the result data processed in a function needs to be handed over to another function. Then Generally speaking, there are three types of summaries:

1. Directly use global variables: This method is the most convenient, but this method breaks the communication and encapsulation between functions. Thought, so its use is not recommended and is beyond the scope of today’s discussion.

2. Solution through dynamic memory allocation in the heap area: In C language, we usually use malloc to dynamically allocate memory in the heap area, using the heap area to "open now when used, manually after use" Take back” feature to achieve flexible management. It is a commonly used method in actual development, and it is also our main content today.

3. The caller passes in the array pointer: This method does not require the function return address, but directly passes in the array address when calling, and entrusts the callee to perform the operation. Since This local variable belongs to the caller itself, so even if the callee completes memory release, the array will not be affected.

Let’s experiment one by one. For the second method, since the memory is dynamically allocated in the heap area, the heap area does not want the local variables to be stored in the stack area in the previous lecture. The system will be based on its life cycle. Automatic recovery, but manual development and manual release, so that the problem can be completely avoided. See the example and effect below:

It should be noted that: remember to free it after use to prevent memory Give way!

How to return an array from a custom function in C language (detailed explanation with pictures and text)

The third method: Although the array is not returned in the function, it can also solve the array communication problem. The main idea is to define the array in the caller , and then pass in the address. Since the memory belongs to the caller, not the callee, it does not matter if the callee releases the memory after the call is completed. The example program and its effects are as follows:

How to return an array from a custom function in C language (detailed explanation with pictures and text)

The summary about the use of array transfer is generally as above. If you have any questions or doubts, please feel free to contact us. You can contact us!

Thank you everyone for reading, I hope you will benefit a lot.

Recommended tutorial: "C Language"

The above is the detailed content of How to return an array from a custom function in C language (detailed explanation with pictures and text). For more information, please follow other related articles on the PHP Chinese website!

source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!