If you don't know how big the arrays are going to be ahead of time:
int main(int argc, char **argv)
{
char **users = NULL; // users and names will be dynamically allocated arrays
char **names = NULL; // of pointer to char
size_t entries = 0;
/**
* Somewhere between here and openPassword figure out how big the arrays
* need to be
*/
openPasswd(&users, &names, entries);
return 0;
}
/**
* Since we need to modify the values of the pointers for users and names,
* we must pass pointers to those pointers.
*/
void openPasswd(char ***users, char ***names, size_t entries)
{
size_t i;
/**
* allocate the arrays
*
* type of *users == char **
* type of **users == char *
*/
*users = malloc(sizeof **users * entries);
*names = malloc(sizeof **names * entries);
/**
* Allocate each entry and get the username/password data from somewhere
* get_user_length, get_name_length, get_user, get_name are all
* placeholders.
*/
for (i = 0; i < entries; i++)
{
/**
* The subscript operator should not applied to the values of users and
* names, but to the values of what users and names *point to*. Since
* [] binds before *, we must use parens to force the correct grouping.
*
* type of (*users)[i] == char *
* type of *(*users)[i] == char
*/
(*users)[i] = malloc(sizeof *(*users)[i] * get_user_length(i));
if ((*users)[i] != NULL)
{
strcpy((*users)[i], get_user(i));
}
(*names)[i] = malloc(sizeof *(*names)[i] * get_name_length(i));
if ((*names)[i] != NULL)
{
strcpy((*names)[i], get_name(i));
}
}
}