Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!bu-cs!purdue!decwrl!sun!pitstop!sundc!seismo!uunet!mcvax!enea!kth!sics!uplog!thomas From: thomas@uplog.se (Thomas Hameenaho) Newsgroups: comp.lang.c Subject: Re: Can ANYONE tell me why this code snippet doesn't work?? Message-ID: <315@uplog.se> Date: 18 Oct 88 13:37:13 GMT References: <7778@gryphon.CTS.COM> Reply-To: thomas@uplog.UUCP (Thomas Hameenaho) Organization: TeleLOGIC Uppsala AB Lines: 71 In article <7778@gryphon.CTS.COM> rickf@pnet02.cts.com (Rick Flower) writes: >I've been trying to get the following code to work.. I've tried just about >everything that I could think of (along the lines of modifications). All I'm >trying to do is write a different printf routine to route information through >a window text printing routine... If anyone can help me, let me know.. > >----------------------------------------------------------------------------- >void Test(fmt,args) >char *fmt; >unsigned *args; >{ >char buff[129]; > > sprintf(buff,fmt,args); > printf("%s\n",buff); >} > >main() >{ > Test("%d %d %d %d",10,20,30,40); >} > >------------------------------------------------------------------------------ The problem is that the stack doesn't look the way you would expect on entry to sprintf(). For a normal 68k machine the stack layout would be: +---------------+ fp -> | frame pointer |--+ +---------------+ | | ret to Test | | +---------------+ | | ptr to buff | | +---------------+ | | ptr to fmt str| | +---------------+ | | 10 | | +---------------+ | | | | . buff (132) . | | | | +---------------+ | +--| frame pointer |<-+ | +---------------+ | | ret to main | | +---------------+ | | ptr to fmt str| | +---------------+ | | 10 | | +---------------+ | | 20 | | +---------------+ | | 30 | | +---------------+ | | 40 | | +---------------+ +->| frame pointer | +---------------+ | ret from main | +---------------+ As you can see the arguments to sprintf are just the three first. If you want to do something like what I think you are aiming at you have to do it some other way. Possibly manipulating the stackpointer in assembler! -- Real life: Thomas Hameenaho Email: thomas@uplog.{se,uucp} Snail mail: TeleLOGIC Uppsala AB Phone: +46 18 189406 Box 1218 Fax: +46 18 132039 S - 751 42 Uppsala, Sweden