Home > Back-end >  Problems (pointer) : string, copy the function returns the string after the first address values, un
Problems (pointer) : string, copy the function returns the string after the first address values, un

Time:12-21

 # include 
Char * strcopy (char * t, const char * s)
{
While (* t++=* s++);
Return t;

}
Int main (void)
{
Char h1 [100], h2 [100], h3 [100].
Char * s1, s2, s3.
S1=h1;
S2=h2;
S3=h3;
The scanf (" % s ", s2);
S3=strcopy (s1, s2);
Printf (" % s \ n ", s3);
return 0;

}

Code as above, no print value, change the line 16 to
 strcopy (s1, s2); S3=s1; 

There is the printing cost? Under the great god please show that, thank you!

CodePudding user response:

1. S3 pointing to the address space of the h3 [100], has not been initialized,

Key local variables, arrays, before use, must be initialized,
Because local variables of the initial value, is very dangerous, (from the stack space distribution, its value for the last time the rest of the others)


CodePudding user response:

Char * strcopy (char * t, const char * s)
{
While (* t++=* s++);
Return t;//t here have pointed to the incoming data to the end of the
}

CodePudding user response:

Yes, you return the t have drift away,

Very fit making his mark,

CodePudding user response:

The building Lord, program modification, if you make it easier to understand, a record pointer is the address of a variable,
 # include 
Char * strcopy (char * t, const char * s)
{
Char * temp=t;
While (* temp++=* s++);
Return t;

}

Int main (int arg c, char * argv [])
{

Char h1 [100], h2 [100], h3 [100].
Char * s1, s2, s3.
S1=h1;
S2=h2;
S3=h3;
The scanf (" % s ", s2);
S3=strcopy (s1, s2);
Printf (" % s \ n ", s3);
return 0;

}

CodePudding user response:

 # include 
Char * strcopy (char * t, const char * s)
{
Char * temp=t;
While (* temp++=* s++);//temp s address values recorded in an increase in change
Return t;//returns the address of the value is first address remains the same string

}

Int main (int arg c, char * argv [])
{

Char h1 [100], h2 [100], h3 [100].
Char * s1, s2, s3.
S1=h1;
S2=h2;
S3=h3;//here s3 - & gt; The address of the h3 [100]
The scanf (" % s ", s2);
S3=strcopy (s1, s2);//here s3 - & gt; S1 address that h1 [100] address,
Printf (" % s \ n ", s3);
return 0;

}
//procedures performed, h1 h2 [100] and [100] the content of the agreement, h3 [100] and no change,
  • Related