Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 21 Nov 1995 10:45:03 -0700
From:      Nate Williams <nate@rocky.sri.MT.net>
To:        Jerry.Kendall@vmicls.com (Jerry Kendall)
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: HELP with typedef
Message-ID:  <199511211745.KAA25161@rocky.sri.MT.net>
In-Reply-To: <9511211559.AA27128.gonzo@vmicls.com>
References:  <9511211559.AA27128.gonzo@vmicls.com>

next in thread | previous in thread | raw e-mail | index | archive | help
> I  need to create a subroutine that is passed a pointer to
> an array of pointers to strings. ie: address of (char *list[]).

The program you supplied was close, but not quite.  I cleaned it up just
a little bit and it follows.

----------------------------------------------------
#include <stdio.h>

typedef char *StringList[];

StringList List = {"one","two","three","four","END OF LIST",""};

void
PrintList( StringList List)
{
        int x;

        for(x = 0; List[x][0] != 0; x++)
                printf("List entry %d: %s\n", x, List[x]);
}

int
main()
{
        PrintList(List);
        return ( 1 );
}
----------------------------------------------------

It now compiles with 'gcc -Wall', and works like I think you intend it
to do.

Note how I changed the end of the for loop condition.  You are using an
empty string, so we need to look at the first character in the string to
determine if it's '\0'.  If you want to do it slightly different (and
somewhat more standard), you could do it this way.

StringList List = {"one","two","three","four","END OF LIST",NULL};
...
        for(x = 0; List[x] != NULL; x++)
...

Note the NULL as the last entry vs "".  This would allow you to use
empty strings.



Nate



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199511211745.KAA25161>