C function return value

Source: Internet
Author: User

Returns local variables, local pointers, and local arrays.
In these three cases,

 

The returned local variable is actually a copy, so there is no problem.
Returns a local pointer. The content pointed to by the pointer will expire with the function, so it is inappropriate.

 

Consider returning local variables and local variable pointers

Return Statement
When a function is called, a temporary variable is generated at the place where the function is called. The return statement assigns the returned value to this temporary variable, that is, a copy of the returned value from the temporary variable.

In fact, it is very simple. If the pointer points to a stack zone (local variables are put here) and the function returns after the function ends, the stack is released and you can get a copy of the pointer through return, but this is the space pointed to by the pointer. What do you want the pointer to do?

Compare the local variable and return it, that is, let the Temporary Variable mentioned above get a copy of it.

 

____

 

 

The local variables have to be divided into automatic and static variables. The results of the two variables are different. The score is described.

An array cannot be returned as a function because the array name is an unchangeable left value, suchCodeIs invalid:

Int func (void) [5];

Returning an array is usually replaced by a pointer to the array, and the pointer cannot point to an automatic array because the array is discarded after the function ends, however, a pointer to a static local array can be returned, because the static storage period is from the object definitionProgramEnd. For example:

Int * func (void)
{
Static int A [10];
........
Return;
}

Local variables are also divided into local automatic variables and local static variables. Because C returns a value, it is possible to return a local variable, whether it is automatic or static, this is because the value of the local variable is returned at this time, but the pointer to the local automatic variable should not be returned because the local automatic variable is discarded after the function call ends, this pointer points to an object that no longer exists and is meaningless. However, a pointer to a local static variable can be returned because the lifetime of the static variable ranges from the definition to the end of the program. For example:

Int func ()
{
Int;
....
Return A; // allow
}

Int * func ()
{
Int;
....
Return & A; // meaningless. Do not do this.
}

The local pointer is the same as the local variable described above. You can return the value of a local pointer or the address of a local static pointer, but you should not return the address of a local automatic pointer.

 

-----------------------------------------

Supplement:

1. I encountered a function pointer passing when I wrote a program for a long time. Later, my cousin helped me solve the problem, it is also the basis of C language:

First read the code section (1 ):

Void chartowchar (char * STR, wchar_t * Out) <br/>{< br/> size_t Len = sizeof (wchar_t) * strlen (STR ); <br/> out = (wchar_t *) malloc (LEN); <br/> mbstowcs (Out, STR, Len ); <br/>}</P> <p> setlocale (lc_all, "CHS"); <br/> char * STR = "Beijing "; <br/> wchar_t * out; <br/> chartowchar (STR, out); </P> <p> // The out output here is null.

 

Why is the out value null? Let's look at another piece of code (2 ):

Void chartowchar (char * STR, wchar_t * Out, size_t Len) <br/>{< br/> mbstowcs (Out, STR, Len ); <br/>}</P> <p> setlocale (lc_all, "CHS"); <br/> char * STR = "Beijing "; <br/> size_t Len = sizeof (wchar_t) * strlen (STR); <br/> wchar_t * out = (wchar_t *) malloc (LEN ); <br/> chartowchar (STR, out, Len); <br/> // This out indicates null.

 

Here we will explain the reasons:

In code 1, wchar_t * out is to define a pointer, but no memory is allocated to it. Passing to chartowchar is just a pointer copy, but there is no specific address, although the memory (pointing to) is allocated in chartowchar, this out pointer is not an out pointer, because it is copied during transfer;

 

In code 2, out memory is allocated outside, with a specific address. Although the pointer copy is passed to chartowchar, The out address and the out address in chartowchar are one, so it can be passed out;

 

2. Read a code segment (3 ):

Void test (int * j) <br/>{< br/> * j = 2; <br/>}</P> <p> int AJ = 1; <br/> A (& AJ); <br/> printf (AJ); <br/> // change AJ to 2

The specific address of AJ is assigned to the test function J, so the value of J in the test function is the address of AJ.

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.