SIMPLE malloc & pointer question

Tim McDaniel mcdaniel at adi.com
Wed Aug 8 01:18:56 AEST 1990


jdm5548 at diamond.tamu.edu (James Darrell McCauley) asked about setting
a function argument.  The answer has been given elsewhere (argument
values are local; changes are not passed back to the parent).  I just
have small style quibbles.

   main()
   { ...
   inita(a,b);
   ... }

   inita (a,b)
   int a[],b[];
   {
     a=(int *)malloc( (unsigned) 4*sizeof(int));
     ...
   }

I have style rules to avoid confusion in the reader and to avoid bugs:
* I declare before use, even if the compiler seems to let me get away
  with it.
  inita was not a problem only because it returns type int.
* I don't try to lie to the compiler.
  If a function doesn't return a value, I define it "void".
  If a function is taking a pointer as argument, I say so, because you
  can't pass arrays as arguments in C.

Inita falls under both these points.

So I would have written

   void inita (a,b)
   int *a, *b;
   { ... }

   main ()
   { ... }

--
"I'm not a nerd -- I'm 'socially challenged'."

Tim McDaniel
Internet: mcdaniel at adi.com             UUCP: {uunet,sharkey}!amara!mcdaniel



More information about the Comp.lang.c mailing list