What is wrong with this program?

Andrew W. Rogers andrew at grkermi.UUCP
Sat Aug 10 22:57:55 AEST 1985


In article <117 at graffiti.UUCP> bruce at graffiti.UUCP (Bruce Jilek) writes:
>Why does printf insist that data->ut_line is a null string while
>putchar can display all of the characters of this array?
>
>		char	ut_line[8];		/* tty name */
>	...
>			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);
>	...
>
>Sample output:
>	
>	tty03                 /*  This shows that data->ut_line isn't null */
>bruce	(null)	492299400     /*  So what's the problem in this line?      */

Are you *sure* it really isn't null?  If the first character were \0, your
'putchar' loop would give no indication of that fact; the \0 would be
invisible and the remaining characters would print normally.  Of course,
'printf' would consider the string null.

Try replacing the 'putchar...' with 'printf("\\%03o ", data->ut_line[i]);'
and see what the characters in the string *really* are.  Either that, or
redirect the output to a file and get a binary/octal/hex dump of it.

AWR



More information about the Comp.lang.c mailing list