In C a function returns a summary of a string of methods

  • 2020-05-19 05:17:23
  • OfStack

Before we discuss the four methods, it is important to have a simple understanding of the function that there is a copy process in both the combination of form and reality and the return of the return statement. The parameter you pass in is a value, and naturally the function will copy that value for itself before it goes to work, and what you pass in is an address, and the function will copy that address for itself. Similarly, when return returns, if it returns a value, the function will copy the value and provide it to the calling function. It will return a pointer (that is, an address), and it will naturally copy an address for the calling function.

Give an example of an error:


#include <stdio.h>
#include <string.h>

char * retstring();
int main()
{
  char * name2;
  name2 = retstring();
  printf("%s\n",name2);
  return 0;
}

char * retstring()
{
  char name[10]; 
  strcpy(name," Han qing ");
  return name;
}

Compile 1 and you'll find a warning that returns the address of a local variable. The output of this program is uncertain because, as we all know, the lifetime of the local variable is inside the block, which is inside the function retstring(). In the function main, the memory space of name has been reclaimed.

So you can't return a string of automatic variables...

Here are four ways to return a string:

1. Pass in a string pointer as a function parameter and return it.

2. Use malloc function to dynamically allocate memory, and pay attention to release it in the main function.

3. Return a static local variable.

4. Use global variables.

Here's the explanation:

Method 1: pass in a string pointer as a function parameter and return the pointer.

A typical strcpy() function would take this approach, with the first argument being a pointer to the destination string and the return value being the same.


char* strcpy(char* des,const char* source)
 
{
 
 char* r=des;
  
 assert((des != NULL) && (source != NULL));
 
 while((*r++ = *source++)!='\0');
 
 return des;
 
}

Method 2: use the malloc function to allocate dynamically, but be sure to release it in the main call function. The memory allocated dynamically by malloc is located in the heap, and the heap is allocated by the programmer.

One example is as follows:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char * retstring();
int main()
{
  char * name2;
  name2 = retstring();
  printf("%s\n",name2);
  // Keep in mind that 1 Need to use free Release, otherwise it will cause a memory leak 
  free(name2);
  return 0;
}

char * retstring()
{
  char * name;
  name = (char *)malloc(10); 
  strcpy(name," Han-qing zhang ");
  return name;
}

Method 3: returns a static local variable.

One example is as follows:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char * retstring();
int main()
{
  char * name2;
  name2 = retstring();
  printf("%s\n",name2);
  return 0;
}

char * retstring()
{
  static char name[10];
  strcpy(name," Han-qing zhang ");
  return name;
}

One problem with this approach is that static local variables (located in a static region and released by the system at the end of the program) are used, so that if the function is called multiple times, the next call overrides the result of the previous call.

Library functions in C, tmpnam(), getenv(), and so on should all be used this way, which is why, when using such functions, you should immediately return a copy of the result.

Method 4: use global variables.

One example is as follows:


char  g_s[100]; 
char*  fun() 
{ 
    strcpy(g_s,  "abc "); 
    return  s; 
} 

That's s it. I hope it helps. Please correct any mistakes. Thank you


Related articles: