About the child function return string problem collection

Source: Internet
Author: User

CASE1:

#include <stdio.h> #include <string.h> #include <stdlib.h>char* getmem (void) {      char  p[] = " Hello world;//Such a child definition can be output, but the output is garbled.      p[5] = 0x0;      return p;} int main () {    char *s= "fzz";    S=getmem ();    printf ("%s\n", s);    return 0;}


As for the program operation results are indicated in the program. So why is it garbled? According to my own understanding is:

The string array p is a local variable that exists inside the stack. When the child function returns, the stack space is also destroyed. Then the returned string first address p corresponds to the memory saved is not Hello World anymore. But the unknown. So the output is garbled.


Case 2:

#include <stdio.h> #include <string.h> #include <stdlib.h>char* getmem (void) {      static char  p [] = "Hello world";//can output, output as Hello;      P[5] = 0x0;      return p;} int main () {    char *s= "fzz";    S=getmem ();    printf ("%s\n", s);    return 0;}
Here we define the string array p as a static variable, stored in a static storage area. is not destroyed with the return of the child function. As a result, it will work correctly.


Case 3:

#include <stdio.h> #include <string.h> #include <stdlib.h>char* getmem (void) {      char  *p = " Hello world ";//Error in running the program cannot output      p[5] = 0x0;      return p;} int main () {    char *s= "fzz";    S=getmem ();    printf ("%s\n", s);    return 0;}

This cannot be output and case1 a bit alike, but not exactly the same. Because the values of the elements in the character array can be changed (they can be assigned again), the contents of the string constants pointed to by the character pointer variable cannot be changed. This is because there are only character variables and string constants in the C language, there are no string variables, and constants cannot be changed. So the p[5]=0x0 itself is wrong. Although the compilation passed, the operation will be error-free. This is not related to storage. Therefore, the results are the same without static.

Such as:

Char a[]= "Hello";

Char *b= "Hello";

A[2]= ' r ';//allows assigning a value to a character variable

b[2]= ' d ';//error, string constants cannot be changed.


Case 4:

#include <stdio.h> #include <string.h> #include <stdlib.h>char* getmem (void) {      char  *p = " Hello world ";//The program can run output as" Hello World "      return p;} int main () {    char *s= "fzz";    S=getmem ();    printf ("%s\n", s);    return 0;}

This is because if you define a character array, it is just a character array variable, and as the function's call ends, the memory that the variable occupies is freed, so the value returned is arbitrary, but if you use a pointer, it always points to the allocated memory until it is released, so the return value is"Hello World".



About the child function return string problem collection

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.