如果数组大小只能是一个常量值,那么
char d_name[...]
是什么意思?
实际上,在dirent.h 文件中声明了一个struct dirent。它的声明如下:
struct dirent{
....
ino_t d_ino;
char d_name[...];
...
};
它用于一次读取一个目录内容,即 inode 号和文件名等...
我的意思是这样一个数组的最大大小是多少,一旦定义了这样一个数组,在内存中静态分配了多少空间?这样的定义可移植吗?
最佳答案
假设它来自struct linux_dirent
,它实际上是char d_name[]
:
struct linux_dirent {
unsigned long d_ino; /* Inode number */
unsigned long d_off; /* Offset to next linux_dirent */
unsigned short d_reclen; /* Length of this linux_dirent */
char d_name[]; /* Filename (null-terminated) */
}
它被称为灵活的数组成员,使用 malloc
您可以为结构分配更多内存,从而为 d_name
提供可变大小。
编辑
OP 引用的文本:
Directory entries are represented by a struct dirent
struct dirent { ... ino_t d_ino; /* XSI extension --- see text */ char d_name[...]; /* See text on the size of this array */ ... };
对于 ...
,作者表示大小不是固定的按照标准。每个实现都必须选择一个固定大小,例如 Linux 选择 256。但它不是有效代码。
关于c - 如果数组大小只能是一个常量值,那么 char d_name[...] 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9645472/