LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C Function Arguments (https://www.linuxquestions.org/questions/programming-9/c-function-arguments-173156/)

jpbarto 04-21-2004 11:28 PM

C Function Arguments
 
I would like to create a function similar to functions like printf and fprintf in C.

In stdio.h, a function such as fprintf is defined as
extern int fprintf (FILE *__restrict __stream, __const char *__restrict __format, ...);

I'm assuming the '...' portion references the fact that multiple variables can be passed to fprintf (if you have a const char* of "%s %d %f %s" you would then pass four additional arguments.

Is this how I define a function if I want to create such functionality? And if so how do I access these additional arguments once inside the function? (is this another argc, argv scenario?)

Thanks for any help,
jpbarto

eantoranz 04-21-2004 11:43 PM

http://www.si.hhs.nl/~bertn/gnu_libc/libc_toc.html

Variadic functions

jpbarto 04-22-2004 12:36 AM

Excellent! Thank you so much. And I'll be bookmarking that page.

Thanks,
jpbarto

itsme86 04-22-2004 12:39 AM

Here's a function I wrote and added to my library of useful things :)

Code:

char *tprintf(char *fmt, ...)
{
  static char buff[4096];
  va_list ap;

  va_start(ap, fmt);
  vsnprintf(buff, sizeof(buff), fmt, ap);
  va_end(ap);

  return buff;
}


jpbarto 04-23-2004 12:01 AM

According to the example (and the manual) over at the GNU C library page, I got the impression that va_start's first argument was va_list and the second was an int indicating the number of the last argument. Did I get the wrong impression?

From your example I'd imagine that format might contain a string of "%s %d\n"?, so this can be passed and will initialize a va_list properly?

itsme86 04-23-2004 12:18 AM

Quote:

The parameter last is the name of the last parameter
before the variable argument list, i.e., the last parame-
ter of which the calling function knows the type.
last is the second argument to va_start() here. fmt from my example is a string like "%d %s %p" as you suspected. Anything following the format string in my tprintf() function are variables to be used for substitution. Therefore the fmt is the last paramater with a known type.

Here's an example usage of my tprintf() function just in case it helps visualize it. It works just like sprintf(), except it returns the formatted string instead of storing it in a buffer you pass to it:

send_msg(player, tprintf("%d apples", num_apples));

send_msg() is of course a hypothetical function that I just came up with for the example :)

To explain why it needs to know the last argument that isn't part of the substitution variable list, consider reversing the first 2 arguments of sprintf() so the protype looks like: sprintf(char *fmt, char *buffer, ...);

In this case, the second argument to va_start() would be buffer, not fmt.

jpbarto 04-23-2004 01:59 PM

I see, thanks for the clarification.


All times are GMT -5. The time now is 07:14 AM.