Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Processing Variable Arguments in a function

Status
Not open for further replies.

jaqel

Programmer
Nov 18, 2002
4
0
0
AU
Hi Guys,

Im trying to write a Variable-Length Argument List for my curses program. Im getting a segmentation fault because va_arg requires an terminating argument like CHAR *(NULL). I want break out of the while loop when there are no more arguments.

At the moment I have to pass CHAR *(NULL) as my last argument when I call my function.

e.g. init_process(name,name1, CHAR *(NULL));

How do I do it so that I can call the function like this:
init_process(name,name1) ;

currently this code works only if you pass CHAR *(NULL):

void init_process(char *name, ...)
{
int x=0;
char *new;
va_list argptr;

va_start(argptr,name);

while ((new = va_arg(argptr, char *)) != 0) /* this only works if char*(NULL) is passed */
{
strcpy(pop[x].name,new);
x++;
}
va_end (argptr);


Any ideas???

Thanks in advance
Johnny
 
That is correct. There is no return value from va_arg() that can be tested for the end of arguments.

-pete
 
I never thought about it but thats probably why some variable length functions in the standard libraries require a null to be passed as the last argument.

thanks
tomcruz.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top