If array sizes can only be a constant value than what does
char d_name[...]
mean?
Actually, there is a struct dirent declared in dirent.h file. its declaration is as under:
struct dirent{
....
ino_t d_ino;
char d_name[...];
...
};
It is used to read directory contents one at a time i.e. inode numbers and filenames etc...
I mean what is the max size of such an array and how much space is statically allocated in the memory once such an array is defined? Is such a definition portable?
Assuming it's from struct linux_dirent
, it's actually 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) */
}
It's called a flexible array member, using malloc
you can allocate more memory to the struct giving d_name
a variable size.
The text the OP is quoting:
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 */ ... };
With the ...
the authors signals the size isn't fixed per standard. Each implementation must choose a fixed size, for example Linux chooses 256. But it's not valid code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With