C language function return value resolution

  • 2020-05-26 09:44:17
  • OfStack

C language function return value resolution

Procedure 1:


int main()

{

  int *p;

  int i;

  int*fun(void);

  p=fun();

  for(i=0;i<3;i++)

  {

    printf("%d\n",*p);

    p++;  

  }

  return 0;

};

int* fun(void)

{

  static int str[]={1,2,3,4,5};

   int*q=str;

  return q;

}



// Cannot return correctly 

Although str is in the dynamic variable region, the dynamic variable is local and is not retained at the end of the function.

Program 2:


int main()

{

  char *p;

  char*fun(void);

  p=fun();

  printf("%s\n",p);

  return 0;

};

char * fun(void)

{

  char *str="hello";

  return str;

}


// You can return correctly 

However, the string "hello" is not a variable, but a constant that the compiler normally places in the constant area when it processes such a constant. And the constant region is always there.

In the last example, the return value of the function fun is a pointer to this constant region.

The function returns a pointer so that the host program can use the pointer to access meaningful data. The key is to make sure that when the pointer value is used, the data it points to is still meaningful.

Also, if the pointer is a pointer to a function, the pointer is to the program code area. This is one application.
In addition, if understand its principle, the programmer still can invent 1 some other flexible use method, of course, that belongs to "strange" method, 1 generally does not advocate.

Application:


int main()

{

  int a,b;

  int max;

  int fun (int a,int b);

  scanf("%d%d",&a,&b);

  max=fun (a,b);

  printf("\n%d\n",max);

  return 0;

};
//http://www.bianceng.cn
int fun(int a,int b)

{

  int max;

  if(a>b)

    max=a;

  else

    max=b;

  return max;

}

// You can return correctly 

Application:

In this example, instead of returning the address of the variable max, you return its value.

The thing after return, as an expression, returns the value of this expression.

For example, if the entry a is 3 and b is 5, then max stores 5. The function of the return statement is to take the 5 inside max and put it into the return value register.

The main program gets the 5 from the return value register (the max variable no longer exists).

In your second example, again, the pointer variable str no longer exists after the function ends. But in the return statement, the value in the pointer variable str (equal to the address of the string "hello") is sent to the return value register.

The dynamic variable str no longer exists, but the string "hello" still exists in the constant area. The host program finds the string based on the address it returns.

Program 4:


int main()

{

  char *p;

  char *fun(void);

  p=fun();

  printf("%x\n",p);

  printf("%s\n",p);

  return 0;

}

char* fun(void)

{

//  char str[]={'a','b','c','d','e','f','\0'};

  char str[]="hello";

  printf("%x\n",str);

  return str;

}

// Cannot return correctly 

char str [] = "hello"; The dynamic variables section contains an array of six characters called str. At the same time, copy the string "hello" (originally stored in constant space) into the array space as the initialization value of the array.

At this point, if return str is executed; Where str is the name of the array. The C language states that if an expression is the name of an array, the value of that expression is equal to the address of that array. So it returns the address of the array. Note: it is not the address of the string constant "hello"! And at the end of the function, even though the constant space is not broken, the array space is broken, and you're not going to return the address of the constant space but the address of the array that's broken.

And char * str = "hello"; str is a variable that can hold one pointer value in the dynamic variables section. At the same time, the address of the string "hello" originally stored in the constant space is assigned to this pointer variable as the initial value.

At this point, if return str is executed; Where str is the pointer variable name. The C language states that if a variable name is in an expression, the value of that expression is equal to the value of that variable (the value of the pointer variable is the address). So it returns the value of the variable str, and the value of the variable str is equal to the address of the string constant "hello". When the function ends, the variable str is broken, but the string in the constant space is not broken. The host program finds the string based on the address it returns.

【 summary 】

In a normal program, the pointer returned by a function should normally be:

(1) point to the static (static) variable;
(2) point to the space allocated by special application (e.g. malloc);
(3) point to the constant region (e.g., point to the string "hello");
(4) point to the global variable;
(5) point to the program code area (such as a pointer to a function).

In addition to these 5, other strange techniques are not recommended.

The life cycle of a variable without the keyword static is only within this function, and the variable will be destroyed automatically after the function ends. When return for Pointers need to pay special attention to, because after the function pointer points to the address of the remains, but the address can be modified by other procedures, the content inside is not sure, may be at the back of the operation will continue to use this address, may not use, so there will be a pair of wrong, if need to return a pointer and don't make mistakes can only call if memory application function

Return to structure:


#include <stdio.h>

typedef struct {

      int a;

      int b;

      int c;

    }str;

str change(str s)

{

  s.a += 1;

  s.b += 1;

  s.c += 1;

  return s;

}

int main(void)

{

  str s1, s2;

  s1.a = 1;

  s1.b = 1;

  s1.c = 1;

  s2 = change(s1);

  printf("s1.a = %d\ts1.b = %d\ts1.c = %d\n",s1.a, s1.b, s1.c);

  printf("s2.a = %d\ts2.b = %d\ts2.c = %d\n",s2.a, s2.b, s2.c);

  return 0;

}


// Can return 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: