Path: utzoo!utgpu!attcan!uunet!lll-winken!lll-tis!ames!amdahl!pacbell!att!ihuxz!burris From: burris@ihuxz.ATT.COM (Burris) Newsgroups: comp.lang.c Subject: Re: variable number of strings passed to function - how? Message-ID: <3533@ihuxz.ATT.COM> Date: 21 Oct 88 14:16:01 GMT References: <434@tutor.UUCP> Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 79 > I want to write a function that accepts a variable number of string > arguments. What is a possible way to do this? > > -pete > In the C language arguments are placed on the stack in reverse order such that the first argument is the lowest index off of the stack pointer. This allows you to pass the first argument as a count of the number of following arguments. Example: string_func( 3, "how", "what", "why" ); or string_func( 4, "how", "what", "why", "where" ); then int string_func( argn, argv ) int argn; char *argv; { int i; char **argp; argp = &argv; /* argp is a pointer to the first * string pointer */ for( i = 0; i < argn; i++ ) { printf( "%s\n", *argp++ ); } } Another (and probably better) way: string_func( "what", "why", "where", NULL ); or string_func( "what", "why", "where", "how", NULL ); then int string_func( argv ) char *argv; { char **argp; argp = &argv; /* now loop while the string pointer is not * set to NULL */ while( *argp != NULL ) { printf( "%s\n", *argp++ ); } } No, I didn't compile either example but they should give you a rough idea of how to do this. The examples assume that the stack grows in a negative direction. In both cases you set argp to the ADDRESS of the first string pointer on the stack. The CONTENTS of argp is the POINTER to the first string. Incrementing argp causes it to point to the second string pointer. Dave Burris ..att!ihuxz!burris