0

How do I check the existence of the EOF in the name field of the following structure?

struct dirent * ent;
ent->d_name;

i.e. I want to know if ent->d_name owns the EOF.

On the other hand, could initialize the variable is of type char[256]

ent->d_name[255]='\0';

But I get the following warning:

warning: can be used 'ent' uninitialized in this function [-Wuninitialized]
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
eduardosufan
  • 1,441
  • 2
  • 23
  • 51
  • 1
    It's right, you didn't initialize it. What does "owns the EOF" mean? Please clarify your question. – Carl Norum Jun 11 '13 at 18:31
  • What do you mean by existence of EOF or ownership of EOF? It's normally a property of streams, and there is no stream here at all. – aschepler Jun 11 '13 at 18:31
  • `EOF` is (a macro that expands to) a negative `int` value returned by `getchar()` when it encounters an end-of-file or error condition. I don't believe it applies here. – Keith Thompson Jun 11 '13 at 18:33
  • @eduardosufan `eof` means *end of file* and it isn't represented by a `char`. Instead, you can check for a null-terminated `char` which essentially is the same thing. – tay10r Jun 11 '13 at 18:39
  • @eduardosufan What are you trying to do? list files in a directory and read all the content of regular files (in which case reading of the file has to go until you reach [EOF](http://stackoverflow.com/questions/1782080/what-is-eof-in-the-c-programming-language))? – A4L Jun 11 '13 at 19:23
  • d_name is the name of a file, and due to malfunctions at the opening of it, I started to wonder if there was the EOF. I read the documentation of dirent.h (where is the structure) and the d_name field contains EOF. – eduardosufan Jun 11 '13 at 22:38

1 Answers1

1

You get the uninitialized warning because you didn't initialize the value ent would point to. struct dirent * ent is merely a pointer that should point to a struct dirent but you haven't initialized the memory it points to. In order to use ent either malloc it:

struct dirent * ent = malloc(sizeof(*ent));

or allocate it on the stack and get a pointer to it using the address-of operator:

struct dirent ent;
struct dirent * entPointer = &ent;

As to the EOF question: the EOF macro can be used to detect an end-of-file in a stream, such as one opened by fopen. It is not relevant to the string d_name[255]. Unless you mean the terminating-'\0', which is guaranteed to be there. [citation needed]

Kninnug
  • 7,992
  • 1
  • 30
  • 42