在C中,当作为参数传递给一个函数修改指针的指针数组的目标 [英] In C, modify the target of a pointer to an array of pointers when passed as argument to a function
问题描述
我要返回给定目录内容的功能。对于这一点,我使用的<一个href=\"http://www.gnu.org/software/libc/manual/html_node/Scanning-Directory-Content.html#Scanning-Directory-Content\"相对=nofollow> SCANDIR
从 dirent.h
。下面的code编译成功(GCC -Wall test.c的),但最后的printf导致分段错误。这意味着,EPS结构(一个指向指针数组的dirent结构)仍然是功能之后空:我怎样才能解决这个问题。
的#include&LT;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&dirent.h GT;
#包括LT&;&string.h中GT;静态INT myselector(常量结构的dirent * dir_entry)
{
字符* PCH =的strstr(dir_entry-&gt;中d_name,);
返回PCH == NULL? 1:0;
}INT list_dir(字符*目录名,结构的dirent ** EPS)
{
INT nbfiles = SCANDIR(目录名,和放大器; EPS,myselector,alphasort);
如果(nbfiles大于0)
{
的printf(内部函数:%S \\ n,EPS [0] - &GT; d_name);
返回1;
}
其他
返回0;
}INT主(INT ARGC,CHAR *的argv [])
{
INT状态= 0;
结构的dirent ** EPS = NULL;
状态= list_dir(/家,EPS);
如果(状态)
{
卖出期权(OK);
的printf(外部函数:%S \\ n,EPS [0] - &GT; d_name);
}
返回EXIT_SUCCESS;
}
由于您的指针发生了变化,而你的看着做错事的main()
: )
您正在传递一个指针的指针的指针 SCANDIR()
。它改变了指针的指针正指向什么(我知道,这伤害读...)。
由于你调用()
与&放SCANDIR的功能。
EPS的价值
已经改变了自己的函数中。
要更好地理解这一点,在当前的功能包住 SCANDIR()
调用具有的printf()
显示报表你什么包含在 EPS值是:
...
的printf(%P \\ N,EPS);
INT nbfiles = SCANDIR(目录名,和放大器; EPS,myselector,alphasort);
的printf(%P \\ N,EPS);
...
要解决这个更改功能:
INT list_dir(字符*目录名,结构的dirent *** EPS)
{
INT nbfiles = SCANDIR(目录名,EPS,myselector,alphasort);
如果(nbfiles!= -1)
{
的printf(内部函数:%S \\ n,(* EPS)[0] - &GT; d_name);
返回1;
}
其他
返回0;
}
和称呼其为...
状态= list_dir(/家,&安培; EPS);
在的main()
。然后,它会很好地工作:
拉刀@蟑螂VirtualBox的:〜$ ./test结果
内部功能:戳破结果
确定结果
外功能:戳破
块引用>I want a function that returns the content of a given directory. For this, I am using
scandir
fromdirent.h
. The code below compiles successfully (gcc -Wall test.c), but the last printf leads to a segmentation fault. It means that the "eps" structure (a pointer to an array of pointers to dirent structures) is still empty after the function: how can I fix this?#include <stdlib.h> #include <stdio.h> #include <dirent.h> #include <string.h> static int myselector(const struct dirent * dir_entry) { char * pch = strstr(dir_entry->d_name, "."); return pch == NULL ? 1 : 0; } int list_dir(char * dirname, struct dirent ** eps) { int nbfiles = scandir(dirname, &eps, myselector, alphasort); if(nbfiles > 0) { printf("inside function: %s\n", eps[0]->d_name); return 1; } else return 0; } int main(int argc, char *argv[]) { int status = 0; struct dirent ** eps = NULL; status = list_dir("/home", eps); if (status) { puts("ok"); printf("outside function: %s\n", eps[0]->d_name); } return EXIT_SUCCESS; }
解决方案Because your pointer has changed, and you're looking at the wrong thing in
main()
:)You're passing a pointer to a pointer to a pointer to
scandir()
. it's changing what the pointer to a pointer is pointing at (I know, that hurts to read ... ).Because you're calling
scandir()
with&eps
in your function, you lose that change outside of the function. The value ofeps
has changed inside your function.To better understand this, in your current function wrap the
scandir()
call withprintf()
statements showing you what the value contained ineps
is:... printf("%p\n", eps); int nbfiles = scandir(dirname, &eps, myselector, alphasort); printf("%p\n", eps); ...
To fix this change your function to:
int list_dir(char * dirname, struct dirent *** eps) { int nbfiles = scandir(dirname, eps, myselector, alphasort); if(nbfiles != -1) { printf("inside function: %s\n", (*eps)[0]->d_name); return 1; } else return 0; }
And call it as ...
status = list_dir("/home", &eps);
in
main()
. It will then work perfectly:broach@roach-VirtualBox:~$ ./test
inside function: broach
ok
outside function: broach
这篇关于在C中,当作为参数传递给一个函数修改指针的指针数组的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!