haochenghhc haochenghhc - 4 months ago 55
C Question

Two-dimensional string pointer

In the process of debugging this procedure, I found two-dimensional behind the target can not add NULL.

void list_dir(char **dirp_array)
char *dir_name = "./";
DIR *dp;
struct dirent *dirp;

if ((dp = opendir((const char *)dir_name)) == NULL){
perr_exit("opendir errno");

while((dirp = readdir(dp)) != NULL){
(* dirp_array) = strdup((char *)dirp->d_name);
printf("%s\n",(* dirp_array));
dirp_array ++;
//dirp_array += strlen(*dirp_array);
*dirp_array = NULL;

after modify´╝îi want to get the two-dimensionalh which i have defined.
so, i define a function here.

void chack_array(char **array)
while(array != NULL){
printf("%s\n",(* array));
array ++;

char *dirp_array[256];

but i core dump


This line:

dirp_array = NULL;

Should be

*dirp_array = NULL;

Your first version does not change contents of the array provided by caller. It just says that the dirp_array pointer is now NULL, ie does not point to any array. You want to store NULL to an element in the caller's array, and you do that by the 2nd version above, dereferencing the pointer.