使用 scandir() 的分段错误和不兼容的指针类型警告
Segmentation fault and incompatible pointer type warning with scandir()
我之前写了一篇关于 scandir()
的 post,但我想我会写一篇新的 post,因为我认为每个 [=60= 的要求是只有一个问题].
我正在编写一个使用 scandir()
的程序,当我 运行 该程序时收到 incompatible pointer type
警告。
根据 scandir() 的 man-page,该函数接收指向 dirent 结构的“三重指针”:
int scandir(const char *dirp, struct dirent ***namelist,
int (*filter)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **));
但是,该页面还提供了以下示例:
#define _DEFAULT_SOURCE
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
struct dirent **namelist;
int n;
n = scandir(".", &namelist, NULL, alphasort);
if (n == -1) {
perror("scandir");
exit(EXIT_FAILURE);
}
while (n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
exit(EXIT_SUCCESS);
}
在我的程序中,我决定按照示例并在我的程序中使用下面的代码(仅提炼为与 scandir()
相关的语句):
int numOfDirectories = 0;
struct dirent **dirEntries;
numOfDirectories = scandir(".", &dirEntries, NULL, alphasort);
当我 运行 程序时,我收到以下警告:
当我基本上遵循手册页中的示例时,为什么会给我这个错误?
另外,根据我另一个post的消息,当dirEntries
是一个双指针时,传入&dirEntries
应该是一个三指针。
我确实尝试通过将 dirEntries
声明为三重指针来更改程序:
int numOfDirectories = 0;
struct dirent ***dirEntries;
numOfDirectories = scandir(".", dirEntries, NULL, alphasort);
我没有收到任何编译器错误,但是当我 运行 程序时,我收到 Segmentation fault (core dumped)
错误。
我确实有一个过滤器函数作为第三个参数,但决定设置为 NULL 以防是我的过滤器函数导致错误。使用 NULL
.
仍然得到相同的 segmentation fault
错误
我也试过 运行调试器,它确实显示了这个:
然后基本上在我声明我的 dirEntries
之后我有一个 if 语句被输入并立即 运行s numOfDirectories = scandir(".", &dirEntries, NULL, alphasort);
我不知道发生了什么。任何想法可能是什么?谢谢你。感谢您的帮助和反馈。
Why would it be giving me this error when I'm essentially following the example it has in the man-page?
您没有按照示例进行操作。该示例声明类型为 struct dirent **
的事物并使用 &
传递其地址。你声明一个类型为 struct dirent **
的东西并传递它,而不是它的地址,而不是使用 &
.
更改您的代码以传递 &dirEntries
,而不是 dirEntries
。
I did try changing the program by declaring dirEntries as a triple pointer: … I don't get any compiler errors, but when I run the program I then get a Segmentation fault (core dumped)
error.
您传递的内容类型正确,但值不正确。 scandir
不只是要您传递 struct dirent **
类型的东西,它要您传递您提供的 struct dirent **
的地址。这是因为它将用一些值填充 struct dirent **
。 (该值将是 struct dirent **
;它将指向 struct dirent *
。)
当你用 struct dirent ***dirEntries;
生成 dirEntries
时,你没有给它任何值,所以将它作为参数传递是错误的。相反,struct dirent **dirEntries
生成一个指针,然后传递 &dirEntries
将该指针的地址提供给 scandir
。它使用该地址将值放入 dirEntries
.
我之前写了一篇关于 scandir()
的 post,但我想我会写一篇新的 post,因为我认为每个 [=60= 的要求是只有一个问题].
我正在编写一个使用 scandir()
的程序,当我 运行 该程序时收到 incompatible pointer type
警告。
根据 scandir() 的 man-page,该函数接收指向 dirent 结构的“三重指针”:
int scandir(const char *dirp, struct dirent ***namelist,
int (*filter)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **));
但是,该页面还提供了以下示例:
#define _DEFAULT_SOURCE
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
struct dirent **namelist;
int n;
n = scandir(".", &namelist, NULL, alphasort);
if (n == -1) {
perror("scandir");
exit(EXIT_FAILURE);
}
while (n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
exit(EXIT_SUCCESS);
}
在我的程序中,我决定按照示例并在我的程序中使用下面的代码(仅提炼为与 scandir()
相关的语句):
int numOfDirectories = 0;
struct dirent **dirEntries;
numOfDirectories = scandir(".", &dirEntries, NULL, alphasort);
当我 运行 程序时,我收到以下警告:
当我基本上遵循手册页中的示例时,为什么会给我这个错误?
另外,根据我另一个post的消息,当dirEntries
是一个双指针时,传入&dirEntries
应该是一个三指针。
我确实尝试通过将 dirEntries
声明为三重指针来更改程序:
int numOfDirectories = 0;
struct dirent ***dirEntries;
numOfDirectories = scandir(".", dirEntries, NULL, alphasort);
我没有收到任何编译器错误,但是当我 运行 程序时,我收到 Segmentation fault (core dumped)
错误。
我确实有一个过滤器函数作为第三个参数,但决定设置为 NULL 以防是我的过滤器函数导致错误。使用 NULL
.
segmentation fault
错误
我也试过 运行调试器,它确实显示了这个:
然后基本上在我声明我的 dirEntries
之后我有一个 if 语句被输入并立即 运行s numOfDirectories = scandir(".", &dirEntries, NULL, alphasort);
我不知道发生了什么。任何想法可能是什么?谢谢你。感谢您的帮助和反馈。
Why would it be giving me this error when I'm essentially following the example it has in the man-page?
您没有按照示例进行操作。该示例声明类型为 struct dirent **
的事物并使用 &
传递其地址。你声明一个类型为 struct dirent **
的东西并传递它,而不是它的地址,而不是使用 &
.
更改您的代码以传递 &dirEntries
,而不是 dirEntries
。
I did try changing the program by declaring dirEntries as a triple pointer: … I don't get any compiler errors, but when I run the program I then get a
Segmentation fault (core dumped)
error.
您传递的内容类型正确,但值不正确。 scandir
不只是要您传递 struct dirent **
类型的东西,它要您传递您提供的 struct dirent **
的地址。这是因为它将用一些值填充 struct dirent **
。 (该值将是 struct dirent **
;它将指向 struct dirent *
。)
当你用 struct dirent ***dirEntries;
生成 dirEntries
时,你没有给它任何值,所以将它作为参数传递是错误的。相反,struct dirent **dirEntries
生成一个指针,然后传递 &dirEntries
将该指针的地址提供给 scandir
。它使用该地址将值放入 dirEntries
.