C language returns an array of learning Notes

Source: Internet
Author: User
Tags strlen

The problem comes from a schoolmaster in class 3 asking me if his program cannot run. What is the problem?

Let's look at the program he wrote, although it looks uncomfortable.

The code is as follows: Copy code
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char tra (char str []);
Char B [80];
Gets (B );
Puts (tra (B ));
Return 0;
}
Char tra (char str [])
{
Char stri [80];
Int I, j, k = 0;
I = strlen (str );
For (j = 0; j <= 2 * I-1; j = j + 2)
 {
Stri [j] = str [k];
Stri [j + 1] = '';
K ++;
 }
Stri [j-1] = '\ 0 ';
Return (stri );
}

Let's skip the format ...... Let's take a look at what the program will achieve.

Use the puts statement to output characters that are separated by spaces.

So he seems to have done this in the function, but the program fails. Why?

At the beginning, I thought about it for a while, and then I thought about it after multiple parties verified it.

Let's take a look at the parameters in char tra (char str []). What does a parameter mean? Is it an array? No, it actually refers to the pointer to the first element of the array. In other words, it is a pointer, and then let's look at the return value return (stri). Is it an array? No, it's just an address. If we see this address in printf ("% p", tra (B);, it's not a string, naturally, the puts () program fails.

So can we use pointers for operations? In short, it reports an error. Because the return type is incorrect, you cannot use * to solve the problem. At the same time, this involves the scope problem, because stri actually disappears after the return is complete, if we use the traditional return value assignment method, we will not be able to find the value corresponding to the address (this should be wrong ......) For corrections, see the end of this article.

So I changed my familiar solution:

The code is as follows: Copy code
# Include <stdio. h>
# Include <string. h>
Void tra (char str [], char stri []);
Int main (void)
{
Char B [80], c [80] = {0 };
Gets (B );
Tra (B, c );
Puts (c );
Return 0;
}
Void tra (char str [], char stri [])
{
Int I, j, k = 0;
I = strlen (str); // 5
For (j = 0; j <= 2 * I-1; j = j + 2) // j <= 9
 {
Stri [j] = str [k]; // h e l o \ 0
Stri [j + 1] = '';
K ++;
 }
Stri [j-1] = '\ 0 ';
}

In this way, the array can be normally input and output, because its scope is not only within this function.

Of course, xueba also asked some questions similar to this and came to a series of conclusions after the final practice.

Of course, the following are all incorrect designs. Please do not refer to them.

For example:
Define the function as an output pointer so that the output type is the upper number.

The code is as follows: Copy code
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char * tra (char str []);
Char B [80], * p, c [80], d;
Gets (B );
Strcpy (c, tra (B ));
P = tra (B );
Puts (c );
Printf ("% c", tra (B) [2]);
Return 0;
}
Char * tra (char str [])
{
Char stri [80];
Int I, j, k = 0;
I = strlen (str );
For (j = 0; j <= 2 * I-1; j = j + 2)
 {
Stri [j] = str [k];
Stri [j + 1] = '';
K ++;
 }
Stri [j-1] = '\ 0 ';
Return (stri );
}

In this way, the output can also be normal. (Note: C cannot use = to assign values to Arrays .)
Or.

The code is as follows: Copy code
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char * tra (char str []);
Char B [80];
Gets (B );
// Puts (tra (B ));
Puts (tra (B ));
Return 0;
}
Char stri [80];
Char * tra (char str [])
{
Int I, j, k = 0;
I = strlen (str );
// For (I = 0; str [I]! = '/0'; I ++ );
For (j = 0; j <= 2 * I-1; j = j + 2)
{
Stri [j] = str [k];
Stri [j + 1] = '';
K ++;
}
Stri [j-1] = '\ 0 ';
Return (stri );
}

Here, stri is used to write external variables.

The reason for the above failure lies in being uncontrollable. It is a big problem to check whether the data in the memory exists after the automatic variable disappears.

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.