Home > Back-end > Child function annotation influence the main function of the output, please answer
Child function annotation influence the main function of the output, please answer
Time:10-04
# include & lt; stdio.h> #include #include #include /* input two strings a and b, Enter an integer f, at fWill be added to a, b and then output New a */ The main () { Char * func (char [], char [], int). Char a [80], [80] b, * c. Int f; The scanf (" % s ", a); The scanf (" % s ", b); The scanf (" % d ", & amp; F); C=func (a, b, f); Printf (" c2==% s ", c);//as a result the output here } Char * func (char [80], a char b [80], int f) { Char c [160]; Char * p; P=c; int i; for(i=0; IC=[I] a [I]; } for(int j=0; B [j].='\ 0'; J++) { C=b [i++] [j]; } for(; A [f]!='\ 0'; F++) { C=a [f] [i++]; } [I] c='\ 0'; Printf (" c1==% s \ n ", c);//here to test the output a Return the c; }
Doing, dev c + +, Program is very simple, but I have been wrong output, If the child followed output function, the result is right, as shown in figure: Calculate, figure, I write, meaning see, Input a: aaaaaa Type b: 111 Input f: 3 Results: c1=aaa111aaa C2=aaa111aaa This shows that my main function * c received a subroutine in c [160] of the data,
But if I take the printf commented out in the function, the result becomes: Input a: aaaaaa Type b: 111 Input f: 3 Results: the c2= C2 it outputs an item, but if I output * c in the main function, the result it is certainly a, * (c + 4) also is 1, it shows that C [160] of the data or to the * c, but why not output? Is my output method is there a problem? If there is a problem, why without annotation of printf function and can output? Child function returns p is also the same effect, O great god answer why
CodePudding user response:
Did you return to the address of the local variable within the function, what results are possible
CodePudding user response:
Char c [160] are local variables with automatic storage period, but the function exits, its address stored content may be erased, You print an inside the function, can display normally, should be undefined, the system does not do this to assure, When not to print in a function, returns the address is likely to have been written into the other content,