zero-sized array in struct/union
-
If you are on C11 ... C11 6.7.9/14 allows the option
char file_name[];
It was addedd for exactly that reason
In vino veritas
-
The original code is:
typedef struct { .... .... char\* file\_name\[0\]; /\* File name in Unicode. \*/ }; // warning C4094: untagged 'struct' declared no symbols
also, I get another warning here:
warning C4094: untagged 'struct' declared no symbols
I don't know how to get rid of this warnings ...pretty sure you could just use char* name; since the array size is 0.
-
You are using a typedef but have not given it the name that you wish to use. It should be something like:
typedef struct {
....
....
char* file_name[0]; /* File name in Unicode. */
} myStruct;
// myStruct is now an alias for the above structureAlso the comment on the last line makes no sense; firstly it is declaring an aray of pointers rather than characters. And secondly, you should not store Unicode characters in a
char
type array. It will most likely cause problems at run time. The zero length array is possibly valid, but it depends on how the code uses the struct. It can be used as a placeholder name for space that will be allocated for a dynamic structure at run time. Something like:struct foo
{
int i;
char text[0];
};// ... other code
struct foo* myFoo = (struct foo*)malloc(sizeof(struct foo) + 20); // additional 20 bytes for the char data.
I was about to write an answer when i saw this. Yes, zero length char arrays at the end of a struct appeared to be quite common in C programming some years (or decades, rather) ago. I haven't seen it in any C**++** code ever, although it probably works the same. Whatever you wish to achieve, there's probably a better solution available in C++ syntax. Usually, std::string is the go to solution here. That said, yes, it must be
char []
, notchar* []
, otherwise it doesn't make any sense at all.GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
I guess disabling this warning is best solution … how can I do that ? With pragma statement ? If yes, which version of pragma should I use ?
Don't disable warnings unless you are 100% sure what they're telling you, 100% sure that this is not a problem for the syntactic and semantic functionality of your code, and at least 90% sure there's no reasonable way to avoid them. Under these conditions, the best way is to use #pragma push immediately before the disable command and #pragma pop after the code that causes the warning. That way you can be sure that the remainder of the code will use the same warning settings as defined in the compiler options.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
I was about to write an answer when i saw this. Yes, zero length char arrays at the end of a struct appeared to be quite common in C programming some years (or decades, rather) ago. I haven't seen it in any C**++** code ever, although it probably works the same. Whatever you wish to achieve, there's probably a better solution available in C++ syntax. Usually, std::string is the go to solution here. That said, yes, it must be
char []
, notchar* []
, otherwise it doesn't make any sense at all.GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)