The whole problem
structnamegroup{Char*Name1; Char*Name2;}; Namegroup A () {namegroup result; //From the other DLL get values to Namegroup. //result. Name1 = source. Value1 result. Name2 = souce. Value2 returnresult;}//question↓voidB () {Namegroup Group=A (); printf ("Name1:%s", group. NAME1); printf ("Name2:%s", group. NAME2); //Name1: [Email protected]#[email protected]#//Name2: [Email protected]#[email protected]#}
The main point is to read the string data from other places, passed into the struct, and then outgoing.
In C # Of course there is no problem, recently in C + +, so that the final hit is garbled.
Later found that the string is the stack, a function execution end is destroyed, this time the structure of the pointer into a wild pointer, so it is garbled.
Solve:
Namegroup A () { namegroup result; // From the other DLL get values to Namegroup . // result. Name1 = source. Value1 result. Name2 = souce. Value2 //result. Name1 = strcpy (result. Name1, source. Value1); // result. Name2 = strcpy (result. Name2, source. Value2); return result;}
Finally, add 2 lines to function A and copy the string to solve it.
C + + One-pointer problem and resolution (char pointer garbled)