Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site ucbvax.ARPA Path: utzoo!linus!decvax!decwrl!ucbvax!anton From: anton@ucbvax.ARPA (Jeff Anton) Newsgroups: net.lang.c Subject: Re: What is wrong with this program? Message-ID: <9940@ucbvax.ARPA> Date: Fri, 16-Aug-85 14:20:33 EDT Article-I.D.: ucbvax.9940 Posted: Fri Aug 16 14:20:33 1985 Date-Received: Mon, 19-Aug-85 21:47:09 EDT References: <117@graffiti.UUCP> <353@ttrdc.UUCP> Reply-To: anton@ucbvax.UUCP (Jeff Anton) Distribution: net Organization: University of California at Berkeley Lines: 36 In article <353@ttrdc.UUCP> levy@ttrdc.UUCP (Daniel R. Levy) writes: >bruce@graffiti.UUCP (Bruce Jilek) <117@graffiti.UUCP>: >> >>Why does printf insist that data->ut_line is a null string while >>putchar can display all of the characters of this array? >>... >> struct utmp { /* This structure is straight out of utmp.h */ >> char ut_line[8]; /* tty name */ >> char ut_name[8]; /* user id */ >> long ut_time; /* time on */ >> }; >>... >> for (i = 0; i <= 7; i++) { >> putchar(data->ut_line[i]); >> } >> printf("\n"); >> printf("%s %s %ld\n", data->ut_name, >> data->ut_line, data->ut_time); /* how about */ printf("%.8s\t%.8s\t%ld\n", .... >>... > >printf expects strings which are null-terminated at the end. There is no >guarantee that what you get in the arrays in struct utmp will be this way. >Ergo, strange results. Putchar is the way to go unless you want to copy >the data out of the struct utmp into something which is null terminated for >the sake of printf. Not really, you are forgetting that the format "%.8s" will cause printf to print up to a null or 8 chars max. Unfortunately this style of printf can not be used with sizeof for compile time format changes without run time code support. -- C knows no bounds. Jeff Anton U.C.Berkeley ucbvax!anton anton@berkeley.ARPA