Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!usenet!ogicse!ucsd!nosc!humu!pilikia!art From: art@pilikia.pegasus.com (Art Neilson) Newsgroups: comp.lang.c Subject: Re: Question about pointer Message-ID: <1991Mar02.195232.29287@pilikia.pegasus.com> Date: 2 Mar 91 19:52:32 GMT References: <2488@njitgw.njit.edu> Distribution: usa Organization: Pilikia, Honolulu Lines: 47 In article <2488@njitgw.njit.edu> yxz1684@hertz.njit.edu (Ying Zhu ee) writes: >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! You are right, it is unsafe as storage for automatic r within function combine is released upon return to main. >main() >{ > char a[10], b[10], *p, *combine(); > > strcpy( a, "horse" ); > strcpy( b, "fly" ); > > p=combine(a, b); Why not use strcat(3) instead of the combine() function ? (of course you'd have to declare a[20]) p=strcat(a, b); I guess it'd defeat the purpose of the excersize. > 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); >} -- Arthur W. Neilson III | INET: art@pilikia.pegasus.com Bank of Hawaii Tech Support | UUCP: uunet!ucsd!nosc!pilikia!art