如果数组大小只能是一个常数值
char d_name[...]
Run Code Online (Sandbox Code Playgroud)
意思?
实际上,在dirent.h文件中声明了一个struct dirent.其声明如下:
struct dirent{
....
ino_t d_ino;
char d_name[...];
...
};
Run Code Online (Sandbox Code Playgroud)
它用于一次读取一个目录内容,即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) */
}
Run Code Online (Sandbox Code Playgroud)
它被称为灵活的数组成员,使用malloc您可以为结构分配更多内存,从而d_name提供可变大小.
OP引用的文字:
目录条目由struct dirent表示
Run Code Online (Sandbox Code Playgroud)struct dirent { ... ino_t d_ino; /* XSI extension --- see text */ char d_name[...]; /* See text on the size of this array */ ... };
根据...作者的信号,每个标准的尺寸不固定.每个实现必须选择固定大小,例如Linux选择256.但它不是有效的代码.