Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA Path: utzoo!linus!philabs!cmcl2!seismo!brl-tgr!tgr!roger@ll-sst From: roger@ll-sst (Roger Hale) Newsgroups: net.lang.c Subject: Re: What is wrong with this program? Message-ID: <635@brl-tgr.ARPA> Date: Mon, 12-Aug-85 12:58:19 EDT Article-I.D.: brl-tgr.635 Posted: Mon Aug 12 12:58:19 1985 Date-Received: Wed, 14-Aug-85 01:42:45 EDT Sender: news@brl-tgr.ARPA Lines: 37 > In article <117@graffiti.UUCP> bruce@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? > > ... [ > main() { > struct { char ut_line[8]; ... } *data; ... /* in effect */ ] > } > > > >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. > ... > AWR [I tried sending to graffiti!bruce@Berkeley.ARPA, but no luck.] In fact data->ut_line is a null \emphasis{pointer}, since printf is recognizing a null string pointer for %s and printing '(null)' instead. This check is in doprnt.s in our V7 PDP11 system, though not in 4.2bsd's. Data->ut_line is null because data is null and ut_line is at offset zero; data is null by accident because this is your first time at this stack level and the stack happens to start out zero filled. If any of this were different it would have printed 'tty03' instead, leaving no clue that *data had been trashed. All this is Murphy-dependent; be glad the symptoms were so clear! Your servant, Roger Hale(sorry, no usenet address)