va_args

va_args

Post by Jeremy Dinse » Fri, 13 Feb 1998 04:00:00



Hi. I'm attempting to make a variable arguement function using the functions and
tools provided in <stdarg.h> .. Upon reading the man page stdarg (under Linux),
I was able to put together a small function based on their foo() example.

Unfortunately, the switch statement does not work. I can pass "strings",'c',1 ..
to the function and nothing will be printed. I had attempted to get around that
by removing the switch (since I plan to only pass strings). However, a version
such as this would print a blank line followed by unallocated memory:

void food(char *fmt, ...){
  va_list ap;
  char *s,buf[BUFSIZ+1];

  va_start(ap, fmt);
  while (*fmt++){
    s = va_arg(ap, char *);
    printf("%s\n",s);

  }
  va_end(ap);

Quote:}

In some cases, that example would even seg fault after printing the
line of strings I sent it. Another note, it would not print the first string
either.
 
 
 

va_args

Post by James » Sat, 14 Feb 1998 04:00:00



Quote:>Unfortunately, the switch statement does not work. I can pass
>"strings",'c',1 .. to the function and nothing will be printed. I had
>attempted to get around that by removing the switch (since I plan to
>only pass strings). However, a version such as this would print a blank
>line followed by unallocated memory:
>void food(char *fmt, ...){
>  va_list ap;
>  char *s,buf[BUFSIZ+1];

>  va_start(ap, fmt);
>  while (*fmt++){
>    s = va_arg(ap, char *);
>    printf("%s\n",s);

>  }
>  va_end(ap);
>}
>In some cases, that example would even seg fault after printing the
>line of strings I sent it. Another note, it would not print the first string
>either.

Let me tell you what I think your function is doing, and you tell me if I
have it right.

Your are telling the function food() that the number of arguments being
passed in is equal to the length of first string passed in.  You are then
printing out those other string arguments.  So, a typical way to use food()
would be:

  food ("abc", "one", "two", "three");
  food ("abcd", "one", "two", "three", "four");
  food ("hi", "hello", "world");

The behavior of your program becomes unpredictable if you pass in things
that are different from "char *" types, since that is what you are
expecting to receive according to your while loop.  Moreover, if the
number of strings you pass in not greater than the length of the first
string, then problems will also result.

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

- Show quoted text -

Quote:>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>


 
 
 

va_args

Post by spirale ingenieri » Sat, 14 Feb 1998 04:00:00


Hi / Salut.

Sorry for my poor English...

The right code to calling food is :

                food ("abc", "one", "two", "three", NULL);

Without the NULL at end program's making a good corefile !
The for-loop in food is stopping when it's reads a NULL in the stack.
If you're lucky, the program found a NULL...

A printf-like version of food :

void food(char *fmt, ...)
{
  va_list ap;
  va_start(ap, fmt);
  vprintf(fmt, ap);
  va_end(ap);

Quote:}


Quote:>Hi. I'm attempting to make a variable arguement function using the
functions and
>tools provided in <stdarg.h> .. Upon reading the man page stdarg (under
Linux),
>I was able to put together a small function based on their foo() example.

>Unfortunately, the switch statement does not work. I can pass
"strings",'c',1 ..
>to the function and nothing will be printed. I had attempted to get around
that
>by removing the switch (since I plan to only pass strings). However, a
version
>such as this would print a blank line followed by unallocated memory:

>void food(char *fmt, ...){
>  va_list ap;
>  char *s,buf[BUFSIZ+1];

>  va_start(ap, fmt);
>  while (*fmt++){
>    s = va_arg(ap, char *);
>    printf("%s\n",s);

>  }
>  va_end(ap);
>}

>In some cases, that example would even seg fault after printing the
>line of strings I sent it. Another note, it would not print the first
string
>either.

 
 
 

1. va_args, va_start problems on solaris 2.4 x86

I have been trying to find why this program just won't compile:
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>

static void _cmnerr(const char *fmt, va_list ap)
{
    int err;
    err=errno;

    vfprintf(stderr, fmt, ap);
    if (err!=0)
         fprintf(stderr,": %s", strerror(err));
    putc('\n', stderr);

void error(const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    _cmnerr(fmt, ap);
    va_end(ap);

int main()
{
    error("fatal: %s", "in main");
    return 0;
cc -c error.c
"error.c", line 22: va_start: argument mismatch
"error.c", line 7: warning: const is a keyword in ANSI C
"error.c", line 7: syntax error before or at: char
"error.c", line 12: undefined symbol: fmt
"error.c", line 12: undefined symbol: ap
"error.c", line 18: syntax error before or at: char
"error.c", line 22: undefined symbol: __builtin_va_alist
"error.c", line 23: undefined symbol: fmt
cc: acomp failed for error.c
Any help would be greatly appreciate please

Thanks

2. How to limit the user space ?