Search code examples
cidentifierc89

Undeclared Identifier although it is declared


for a program in which I try to create a file and later write into it, I have written the following:

 int main(){
        ...
        ....
       (some code)
        ....
          char DataBuffer[] = "This is the test file";
        ...
        ...


}

I get the error "DataBuffer: undeclared identifier" . I am using Microsoft Visual C++ Express. And in an old asked question here in stackoverflow.com, I have read that Visual C++ uses an old C89 standard and that it does not support C99 standard. For that reason, I must declare the variables at the beginning(which I did for the rest of the parameters of CreateFile() and WriteFile). I mean, when you consider the following:

   DWORD dwCreationDisposition = CREATE_NEW;

Then I split it up and changed it to:

   DWORD dwCreationDisposition;
   ...
   dwCreationDisposition = CREATE_NEW

but I do not know how I should do it with an array. So, for example when I write:

 char DataBuffer[];
 ....
 DataBuffer[] = = "This is the test file";

Then I also get the same error message. What can I do ? Is there any possibility to change the compiler options ? Or a chance to rewrite it such that the integrated compiler accepts it as the other splitted variables/parameters ?

best regards,


Solution

  • If you want your string to be re-writable you should so this:

     char DataBuffer[MAX_SIZE];
     ....
     strcpy(DataBuffer,"This is the test file");
    

    Also consider using strncpy for avoiding buffer overrun error.

    If your string is constant then:

    const char DataBuffer[] = "This is the test file";