This segfaults.

main( argc, argv )
int argc;
char  *argv[];
{

  func( "hello","this","is","a test" );
}

func( argv )
char *argv[];
{

  printf( "argv[0] = %s\n", argv[0] );

}








--- In [email protected], andrew clarke <[EMAIL PROTECTED]> wrote:
>
> On Mon 2008-12-08 18:24:43 UTC-0000, mikejd42 ([EMAIL PROTECTED]) wrote:
> 
> > I have a need to pass a variable number of arguments to a function
in "C".
> > 
> > Is there a way to do something like this
> > 
> > function( argc, argv )
> > int  argc;
> > char *argv[];
> > {
> > }
> 
> Do you know you are using a very old coding (K&R) style here?  You can
> write this as:
> 
> void function(int argc, char *argv[])
> {
> }
> 
> > I know main does this and need to write a function that also can
do it.
> 
> Yes.  In the case of main(), the final element of argv is equal to
NULL, eg.
> 
> argv[0] == "program-name"
> argv[1] == "Hello"
> argv[2] == "world"
> argv[3] == NULL
> 
> argc is actually superfluous.  You can calculate its value by
> comparing each element of argv with NULL.  When it equals NULL, stop.
> If you are familiar with null terminated strings, this may be familiar
> territory.
> 
> (Incidentally it's possible argv wasn't always NULL terminated in the
> early days of C, before it was standardised, which could explain why
> argc exists.)
> 
> Anyway, the point of all this is that you can do what you want,
> provided you can devise a way to terminate your variable length array
> in a way that you don't confuse the terminator with actual valid data.
> In other words, don't have an array of ints where zero is a valid
> number, then use zero as the terminator!
>


Reply via email to