Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to get the arg1, arg2 and arg3 actual values in this C code?

Status
Not open for further replies.

Help

Advanced Member level 2
Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
Hi,

Anyone can help me to explain the code?

Code:
int varfunc (char *buf, int id, ...) 
{
   va_list tag;
   va_start (tag, id);

   
   if(id == 0) 
   {
      char i=0, result=0; 

      int arg1;
      char *arg2;
      char arg3;

      arg1 = va_arg (tag, int);   // 27(1Bh) at int address = 0x1B00
      arg2 = va_arg (tag, char *);
      arg3 = va_arg (tag, char);

      arg1 = arg1 + 0x02; // arg1 != 0x0008???

      while(arg2[i] != '\0') // arg2[] = {0} ????
      {
         result = arg2[i];
         i++;
      }
      arg3 = arg3 * 0x02; // arg3 = 0 ???

   }
   else
   {
      char i=0, result=0; // static Variable

      char *arg1;
      char *arg2;
      char arg3;


      arg1 = va_arg (tag, char *);
      arg2 = va_arg (tag, char *);
      arg3 = va_arg (tag, char);
      *buf = '1';

      arg1 = "01";
      arg2 = "9876";
      arg3 = 100;   

      while(arg1[i] != '\0')
      {
         result = arg1[i];
         i++;
      }
      i=0;
      while(arg2[i] != '\0')
      {
         result = arg2[i];
         i++;
      }
      arg3 = arg3 * 0x02; // arg3 != 0x06 ????
   }
}

void caller (void) 
{
   char tmp_buffer [10];

   varfunc (tmp_buffer, 0, 4, "Test Code", 2);
   varfunc (tmp_buffer, 1, "Test", "Code", 3);
}

Why i can't get arg1, arg2, and arg3 actual value?
How can i get the actual value?

Thank You.
 

stdarg while va_arg code forum

Try enabling all of your compiler's warning messages and recompile. The messages will help your debugging.
The biggest troubles I see are missing header file, mismatched type of arg3, and missing va_end.

This works for me in MinGW gcc 3.4.2:
Code:
#include <stdio.h>
#include <stdarg.h>

int varfunc(char *buf, int id, ...)
{
   va_list tag;
   va_start(tag, id);

   if (id == 0)
   {
      int arg1;
      char *arg2;
      char arg3;

      arg1 = va_arg (tag, int);
      arg2 = va_arg (tag, char *);
      arg3 = va_arg (tag, int);

      printf("id == 0: %d %s %d\n", arg1, arg2, arg3);
   }
   else
   {
      char *arg1;
      char *arg2;
      char arg3;

      arg1 = va_arg (tag, char *);
      arg2 = va_arg (tag, char *);
      arg3 = va_arg (tag, int);

      printf("id != 0: %s %s %d\n", arg1, arg2, arg3);
   }
   va_end(tag);
   return 0;
}

int main (void)
{
   char tmp_buffer [10];

   varfunc (tmp_buffer, 0, 4, "Test Code", 2);
   varfunc (tmp_buffer, 1, "Test", "Code", 3);
   return 0;
}
Output:
Code:
id == 0: 4 Test Code 2
id != 0: Test Code 3
More info in the comp.lang.c FAQ (this FAQ is full of wonderful advice):
http://c-faq.com/varargs/varargs1.html
 

c variadic function implementation without stdarg

Hi,

Can i simulate the result by this way... for arg1 = arg1 + 2; i just wanna to see the result but if i do that i can't get the result arg1 = 6. I think the following arg2 and arg3 also the same problem. Why?

and why the arg3 type is 'int'?

I am using Keil compiler but i think is the same...because it it general code, am i right?

Thank You.
 

stdarg c

'char' is promoted to 'int' when passed through '...', so your varfunc() function must expect an 'int'. One mismatched argument could corrupt all of them.

I don't understand your sentence about simulate.

If Keil is ANSI C compliant (I don't know if it is), then it should behave according to standard C language.
 

c language printf stdarg

Hi,

echo47 said:
'char' is promoted to 'int' when passed through '...', so your varfunc() function must expect an 'int'. One mismatched argument could corrupt all of them.
You mean the last declaration must declare as int in va_arg (tag, int); if the arg3 type is char?
Code:
      char arg3; 
      ...
      ....
      arg3 = va_arg (tag, int);

let say if the arg3 type is int, so the va_arg (tag, long); , am i right?

I don't understand your sentence about simulate.

Can i use this 3 variable (arg1, arg2, and arg3) to do something else (ex. arg3 += 2; )? or It's just for printf purpose? Can you make some examples?

Thank You.
 

stdarg in c

Yes, if your calling function passes a 'char' in arg3, it gets promoted to 'int', so you must specify 'int' inside your va_arg().

No, if your calling function passes and 'int' in arg3, it remains an 'int'. It does not get promoted to 'long'.

Yes, you can use the three arguments just like ordinary function arguments. I used printf simply because I wanted the program to output visible results.

Please refer to a good C book that describes how to use variable number of arguments. You may also find some answer in the comp.lang.c FAQ. In particular, this one:
http://c-faq.com/varargs/float.html
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top