2010年(12)
分类: C/C++
2010-07-27 16:04:55
例如:
#include
#include
typedef struct cust_str {
int id ;
char last_name [20] ;
char first_name[15];
} CUSTREC;
int main (void)
{
char * src_string = "This is the source string" ;
char dest_string[50];
CUSTREC src_cust;
CUSTREC dest_cust;
printf("Hello! I'm going to copy src_string into dest_string!\n");
/* Copy src_ string into dest-string. Notice that the destination
string is the first argument. Notice also that the strcpy()
function returns a pointer to the destination string. */
printf("Done! dest_string is: %s\n" ,
strcpy(dest_string, src_string)) ;
printf("Encore! Let's copy one CUSTREC to another. \n") ;
prinft("I'll copy src_cust into dest_cust. \n");
/* First, intialize the src_cust data members. */
src_cust. id = 1 ;
strcpy(src_cust. last_name, "Strahan");
strcpy(src_cust. first_name, "
/* Now, Use the memcpy() function to copy the src-cust structure to
the dest_cust structure. Notice that, just as with strcpy(), the
destination comes first. */
memcpy(&dest_cust, &src_cust, sizeof(CUSTREC));
printf("Done! I just copied customer number # %d (%s %s). " ,
dest_cust. id, dest_cust. first_name, dest_cust. last_name) ;
system("pause");
return (0);
}
char * rtrim(char*str)
{
int n = strlen(str) -1;/* Start at the character BEFORE
the null character (\0). */
while(n>0)
{
if(*(str+n) != '')/* If we find a nonspace character: */
{
*(str+n+1) = '\0';/* Put the null character at one
character past our current
position. */
break;
}
else
{
n--;
}
}
return str;*Return a pointer to the string*/
}