www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/08/13:44:39

From: kagel AT quasar DOT bloomberg DOT com
Date: Wed, 8 May 1996 13:26:36 -0400
Message-Id: <9605081726.AA03835@quasar.bloomberg.com >
To: wank AT uebemc DOT siemens DOT de
Cc: djgpp AT delorie DOT com
In-Reply-To: <9605080624.AA16965@malta> (wank@uebemc.siemens.de)
Subject: Re: vararg
Reply-To: kagel AT dg1 DOT bloomberg DOT com

   Date: Wed, 8 May 96 08:24:02 +0200
   From: wank AT uebemc DOT siemens DOT de (Rainer Wank)

   i don't know much about variable argument lists, so
   i read a few in the Kernighan/Ritchie book.
   After that i wrote a little program:

   #include <stdarg.h>
   #include <stdio.h>

   unsigned char fg;

   void abc(int anzahl, unsigned char abc, ...)
   {
    va_list ap;
    va_start(ap, abc);
    fg = va_arg(ap, unsigned char);


The problem is that when you call a variable argument function the arguments
beyond the list of predefined arguments are all promoted (char and short to
int, float to double) so you cannot get an unsigned char here.  What you are
getting are the high order 8 bits (all initialized to zero) of the (low order
word, because the X86 processors are little endian, of the) 32-bit integer
whose lowest order byte contains the unsigned char that was passed. (WHEW that
was a mouthful. :p) Make that line:

fg = va_arg(ap, unsigned int);

And everything will be hunky/dunky.  You may have noticed that abc (the second
argument) is properly assigned and this caused the confusion.  This is because
as a defined parameter it is passed without promotion in ANSI "C" and while
promoted in K&R the function header properly unpacks the integer.


    printf("\n\rErster variabler Parameter: %d", fg);
    va_end(ap);
   }

   void main(void)
   {

    abc((int) 10, (unsigned char) 10, (unsigned char) 51);
   }


   I haved compiled it on my 80486 DX 2 with djgpp gcc version 2
   (operating system OS/2 2.11). 
   When i run the program it always prints 0 to stdout.

   I also have compiled the program with BORLAND C++ 3.1 and it
   works fine. Furthermore the acc compiler on the sun delivered
   a result i expected.

   I have searched for help in the given web page
   http://www.delorie.com/djgpp/mail-archives/djgpp
   but i have not found an answer.

   Do you know an answer ?

   Thanx a lot !!!


   Wank Rainer
   e-mail: wank AT ftth DOT uebemc DOT siemens DOT de

-- 
Art S. Kagel, kagel AT quasar DOT bloomberg DOT com

A proverb is no proverb to you 'till life has illustrated it.  -- John Keats

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019