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,

CodePudding user response:

refer to the second floor m0_46108109 response:
char c [160] is a local variable, 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,

Can I output * c in the main function, the content is really a, * c + 4 and 1, even if is because local variables and didn't get the data, * c should not have value, the value is there in the * c cases, printf % s was not able to output data, is really don't understand

CodePudding user response:

reference 1/f, the truth is right or wrong response:
did you return to the address of the local variable within the function, what the result is possible

Well, is really didn't expect that because of the local variable problem, I also want to define an array in the main function c again the address to a subroutine, but one doesn't want to change, second, really want to know why the output is wrong, but if, because of the local variable is eliminated, why in the comments no children printf () function, the output is right

CodePudding user response:

reference 4 floor qq_36058799 response:
Quote: reference 1/f, the truth is more important than right or wrong response:
did you return to the address of the local variable within the function, what the result is possible

Well, is really didn't expect that because of the local variable problem, I also want to define an array in the main function c again the address to a subroutine, but one doesn't want to change, second, really want to know why the output is wrong, but if, because of the local variable is eliminated, why don't the comments under the condition of sub printf () function, the output is right

What the result is possible, or according to the specification to write code

CodePudding user response:

Oh, I see, is that the program is running, you need to code links into the stack,
Library functions such as printf to link into the program stack, the second call, you can reuse the code,

If you didn't join the printf in function, so in the main function call printf for the first time, printf code that covers your stack,
So I can't find the original data,

You can test, elsewhere first printf, call time can get the results you want,
This effect is still undefined, of course, the system does not do this,

CodePudding user response:

refer to 6th floor m0_46108109 response:
oh, I know, is the fact that the program is running, the need to link the code into the stack,
Library functions such as printf to link into the program stack, the second call, you can reuse the code,

If you didn't join the printf in function, so in the main function call printf for the first time, printf code that covers your stack,
So I can't find the original data,

You can test, elsewhere first printf, call time can get the results you want,
This effect is still undefined, of course, the system does not do this guarantee,

And useless, I in the main function and the function is added the printf a space, is the same, or even firing a function of the original printf let him not output c [160] also can not get the result, oh, c to output the output array pointer p did not affect, the return is c or didn't influence, p

But if the child function is commented out, behind the main function printf plus a printf * c, we won't be able to receive a data * c, if there is only one main function printf to output * c, there is data, it may be that the so-called "cover" relationship,
  • Related