Novice question on vectors of pointers
John A. Weeks III
john at newave.UUCP
Tue Apr 30 09:22:39 AEST 1991
In article <1991Apr29.051313.2064 at rushpc> jjr at rushpc (John J. Rushford Jr) writes:
>I hope this question is not too OS specific. Point me elsewhere if it is.
Not at all.
> How does one use a vector of pointers to member names.
Fun with arrows and dots!
> It's **gr_mem I don't understand. How do you work with it?
> struct group {
> char *gr_name; /* the name of the group */
> char *gr_passwd; /* the encrypted group password */
> int gr_gid; /* the numerical group ID */
> char **gr_mem; /* vector of pointer to member names */
> };
OK. gr_mem is a pointer to a pointer to a character. The second pointer
is probably a string, so you have:
In pntr structure:
gr_name ----> null terminated string
gr_passwd ----> null terminated string
gr_gid <- an integer location
gr_mem ----> some_location ----> null terminated string
The reason you use this "some_location" hop is that some_location can
an array (a.k.a. vector). For example:
gr_mem ----> some_location[ 0 ] ----> null terminated string
some_location[ 1 ] ----> null terminated string
some_location[ 2 ] ----> null terminated string
some_location[ 3 ] ----> null terminated string
...
So, if you want to print out the third string, use the following:
printf( "%s\n", pntr->gr_passwd[ 2 ] );
To allocate one of these puppies, allocate the vector first, then
step through each element of the vector and allocate the strings.
When you free this, free the strings first, then free the vector.
If you free the vector first, you will loose the addresses of the
strings.
-john-
--
=============================================================================
John A. Weeks III (612) 942-6969 john at newave.mn.org
NeWave Communications ...uunet!tcnet!wd0gol!newave!john
More information about the Comp.lang.c
mailing list