strcat/access question

Richard O'Keefe ok at cs.mu.oz.au
Tue Oct 3 20:29:29 AEST 1989


In article <5409 at umd5.umd.edu>, jjk at astro.UMD.EDU (Jim Klavetter) writes:
> if((home=getenv("HOME"))==(char *) 0) perror("GETENV\n");
> printf("%s:\n", home);
At this point, home points to "/a/jjk\0" followed by who knows what
> printf("%s:\n", strcpy(string,strcat(home, "/astro/data/obs.list")));
				^^^^^
At this point, home points to "/a/jjk/astro/data/obs.list\0" who knows what
strcat() copies its second argument into the same array that its first
argument points to, starting at the first NUL it finds there.
> printf("%d:\n", access(strcat(home, "/astro/data/obs.list"), 4));
At this point, home points to "/a/jjk/astro/data/obs.list/astro/data/obs.list"

> The man page says that strcat returns a null-terminated string so that
Yes it does; and the pointer it returns is the pointer you gave it as
its first argument.

Sometimes it is simpler to forget strcat() and so on and just use
sprintf:
	sprintf(work_string, "%s/astro/data/obs.list", home);
This is unlikely to be a performance bottleneck.



More information about the Comp.lang.c mailing list