What is wrong with this program?

Daniel R. Levy levy at ttrdc.UUCP
Sun Aug 11 06:31:19 AEST 1985


bruce at graffiti.UUCP (Bruce Jilek) <117 at 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);
>...

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.
-- 
 -------------------------------    Disclaimer:  The views contained herein are
|       dan levy | yvel nad      |  my own and are not at all those of my em-
|         an engihacker @        |  ployer, my pets, my plants, my boss, or the
| at&t computer systems division |  s.a. of any computer upon which I may hack.
|        skokie, illinois        |
|          "go for it"           |  Path: ..!ihnp4!ttrdc!levy
 --------------------------------     or: ..!ihnp4!iheds!ttbcad!levy



More information about the Comp.lang.c mailing list