Xref: utzoo comp.lang.c:21317 comp.lang.c++:4505 Path: utzoo!utgpu!watmath!att!dptg!rutgers!usc!cs.utexas.edu!milano!cadillac!vaughan@mcc.com From: vaughan@mcc.com (Paul Vaughan) Newsgroups: comp.lang.c,comp.lang.c++ Subject: Re: ttyname() - question Message-ID: <2607@cadillac.CAD.MCC.COM> Date: 30 Aug 89 14:56:08 GMT References: <99@lkbpyr.UUCP> <1088@virtech.UUCP> Sender: news@cadillac.CAD.MCC.COM Reply-To: vaughan@mcc.com (Paul Vaughan) Followup-To: comp.lang.c Organization: MCC VLSI CAD Program Lines: 50 In-reply-to: cpcahil@virtech.UUCP (Conor P. Cahill) From: cpcahil@virtech.UUCP (Conor P. Cahill) In article <99@lkbpyr.UUCP>, jonas@lkbpyr.UUCP (Jonas Heyman) writes: > Hello, > > I was wondering why this work: > [ example 1 with ttyname() in main() ] > And why this don't: [ example 2 with ttyname() in test() called from main() ] > > All the above should type out your current 'tty',example: '/dev/tty023'. > This is really on of those RTFM kind of things. ttyname() takes a file descriptor as an argument. Both times you were getting whatever value happened to be on the stack at the time the ttyname was called, you were lucky the first time (within main) probably because the ttyname was called so early that the stack was relatively clean, but even this is a bad assumption because other processing has already been accomplished in the startup routines before main is called. Good answer. I was wondering why the compiler hadn't cited the appropriate passages of TFM to him. I guess that's something you give up if you use instead of . I tried it using (the gnu lib-g++) and got io.cc:8: warning: type mismatch with previous external decl /usr/local/gnu/lib/g++-include/std.h:204: warning: previous external decl of `char *ttyname (int)' The line number is different because I also bothered to declare test first to get rid of the implicit declaration warning. When I found the argument passing error, I decided the original poster was completely lost and that it wasn't worth looking at. However, I think it's useful to note that using instead of stdio.h would have caught this one. Not to mention that using cout instead of printf would obviated the problem with the %d format specifier and that the program is very clearly expressed in one line cout << ttyname(2); Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639 Box 200195, Austin, TX 78720 | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan