Question about pointer

Ying Zhu ee yxz1684 at hertz.njit.edu
Sat Mar 2 02:46:59 AEST 1991


The following is a small program from "Mastering C pointers" by Robert J.
Traister. I am confusing with the pointer here. Since the r[100] is an
automatic variable, after calling the "combine" the r[100] will be deallocated.
Then the p will point to an unsafe place. Actually, runing this program
on VAXII/GPX(ULTRIX) and Sparc( BSD UNIX ) here have different answers. Any
one familiar with C  can help me? Thank you!


main()
{
    char a[10], b[10], *p, *combine();

    strcpy( a, "horse" );
    strcpy( b, "fly" );

    p=combine(a, b);

    printf("%s\n", p );
}

char *combine(s,t)
char *s, *t;
{
     int x,y;
     char r[100];

     strcpy(r,s);
     y=strlen(r);
     for ( x=y; *t != '\0'; ++x )
         r[x] = *t++;
     r[x]='\0';

     return(r);
}



More information about the Comp.lang.c mailing list