Passing Arguments In C

Bob Larson blarson at skat.usc.edu
Mon Sep 19 04:04:22 AEST 1988


In article <2232 at ssc-vax.UUCP> dmg at ssc-vax.UUCP (David Geary) writes:
>  I'm trying to get a grasp on what actually goes on under the covers
>when passing arguments in C.
[...]
>When main() calls DoIt(), the values of a,b, and c are loaded on
>a stack somewhere in memory.

When main() calls DoIt(), the arguements are calculated in random
order then passed via whatever trick the compiler writer thought
approprate.  (Stack?  Why do you assume there is one?)

>Am I thinking correctly?

Your sceme should work, but it certainly isn't the only or even the 
most common way.

>Does the compiler have the freedom
>to, say, grow the stack towards the end of memory instead of the beginning?

The compiler can use anything that works.  Os9/68k passes the first two
arguments in registers, Prime 64v mode C creates temporaries then passes
the address of them, etc.

>Where is all this kind of stuff spelled out?

It isn't spelled out on purpouse. The compiler writer can choose what
is best under the circumstances.  (K&R, K&R II, ansi drafts, and H&S
are possible referneces.)

>I teach an Advanced C class, and am showing my students how to write variadic
>functions.

There are two semi-portable ways of doing it, known as varargs and stdargs.
(varargs comes from bsd, stdargs from ansi.)  If using varargs, remember
to get ALL arguments to the function that way.  (Some varargs implemetations
depend on this.)  stdargs implementations are still rare, and will frequently
require compiler support.

For C implementations that don't come with varargs.h, I have written my
own.  To do this, you read the compiler specific documentation, and
write some example programs and study the assembler output.

If you must show an example of argument passing, you should show as
wide a variety of them as possible so your students don't get stuck
in the all the worlds a vax philosiphy.
Bob Larson	Arpa: Blarson at Ecla.Usc.Edu	blarson at skat.usc.edu
Uucp: {sdcrdcf,cit-vax}!oberon!skat!blarson
Prime mailing list:	info-prime-request%ais1 at ecla.usc.edu
			oberon!ais1!info-prime-request



More information about the Comp.lang.c mailing list