Can ANYONE tell me why this code snippet doesn't work??

Thomas Hameenaho thomas at uplog.se
Tue Oct 18 23:37:13 AEST 1988


In article <7778 at gryphon.CTS.COM> rickf at 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 at uplog.{se,uucp}
Snail mail:	TeleLOGIC Uppsala AB		Phone:	+46 18 189406
		Box 1218			Fax:	+46 18 132039
		S - 751 42 Uppsala, Sweden



More information about the Comp.lang.c mailing list