Sometimes we need the function returns a string but can not define a string function, right? So, here are a few ways to make function returns a string.
Method One: Global Variables
Global variables are the easiest way, as in the example:
#include <stdio.h>
char chName[20];
void DoName()//使用全局变量的话就不必要设定函数类型了
{
//对chName的操作
...
return;
}
int main()
{
DoName();//执行对chName的操作
puts(chName);//输出chName的值
}
Although such an approach function does not return anything, but the effect is the same.
Note: not recommended to use too many global variables
Method 2: dynamic allocation of space pointers and malloc
PS: For the use of malloc introduction https://blog.csdn.net/ZouHuiDong/article/details/90415482
variable if you do not use a global, we can also define a pointer type of function:
char *DoName()
Then returns a pointer (char * chName assuming defined in the function):
return chName;
But not so simple, because the variable function defined at the end of the function to empty the function can not be used in variable outside the function. Function defined variables stored in the system in the stack, when the function is not running, the stack is such that:
Stack |
---|
Top of the stack -> Other information about the program ... |
I began to call the function:
Stack |
---|
Top of the stack -> char * DoName |
Other information about the program ... |
Execution to char * chName:
Stack |
---|
Top of the stack -> char * chName |
… |
Char * Donme |
Other information about the program ... |
When the function ends:
Stack |
---|
Top of the stack -> Other information about the program ... |
At this point, the function has ended, the function definition of variables from the pop-up, no. Returns a pointer and then if:
return chName;
You know, the pointer is pointing to an area of space, rather than have their own value, if the function is over, return a pointer to a function pointer still points to the area has been cleared of the piece, then it is equivalent to return empty. Such as:
[Function] stack operation |
---|
Stack -> pointer to -> char * chName |
… |
Char * Donme |
Other information about the program ... |
[Stack function ends, the function defined variable has been cleared] |
---|
Pointer to -> |
Top of the stack -> Other information about the program ... |
So this time, return the pointer to the area, an empty space, it would not play a role.
So, how to solve it?
Please have a protagonist malloc played! !
* ChName can use malloc to apply for a special space
chName = (char*)malloc(20);//给chName申请20的空间,相当于char chName[20]
That one might ask, that this is not the same as you?
This is not the same, generally defined variable in the function after the function elsewhere to not take, the value is cleared. However, the application of space using malloc, not as a function, on the stack, but on a pile, we need to free empty. Such as:
- Function is not running
Stack | stack |
---|---|
Top of the stack -> Other information about the program ... |
- Function starts running
Stack | stack |
---|---|
Top of the stack -> char * DoName | |
Other information about the program ... |
- Function operation
Stack | stack |
---|---|
Stack -> chName = (char *) malloc (20) | |
char *chName | |
… | |
Char * Donme | |
Other information about the program ... |
- Function created a variable chName and requested space, application space on the heap.
Stack | stack |
---|---|
Top of the stack -> ... | |
chName = (char*)malloc(20) | |
char *chName | |
… | |
Char * Donme | |
Other information about the program ... | Pointer to -> ... (number value) |
- The end of the function, variable function creates are cleared, but the need to free heap to empty, but do not use free (), the heap is not empty.
Stack | stack |
---|---|
Top of the stack -> Other information about the program ... | Pointer to -> ... (number value) |
In this case, the function returns a pointer * chName, it points to an area of the heap rather than the stack has been emptied. The main function of receiving this pointer, succeeded in obtaining a value from the stack:
char *DoName()
{
...
char *chName;
chName = (char*)malloc(20);
...
return chName;
}
int main()
{
char getName[20] = DoName();
puts(getName);
return 0;
}
that's it.
Wrong approach
Beware: Some people may think I return a string does not like it, why do like method so much trouble?
Error code:
char *DoName()
{
char chName[4] = {'D','a','v','\0'};
return chName;
}
int main()
{
char getName[4] = DoName();
puts(getName);
return 0;
}
Thus, value can be obtained seemingly ...... "Dav" ah ( "\ 0" indicates the end of the string of the symbol in the string).
In fact, this is only just no other procedures to deal with, do not empty the stack nothing! Now you can get, but not necessarily be able to get elsewhere. If you let the program handle something, then it is not worth it, so do not use this erroneous practice.