Is it possible to do the following:
I get the following error:Code:136 struct s 137 { 138 int a; 139 char b[a]; 140 };
Code:file.h:138: error: invalid use of non-static data member `s::a' file.h:139: error: from this location
Is it possible to do the following:
I get the following error:Code:136 struct s 137 { 138 int a; 139 char b[a]; 140 };
Code:file.h:138: error: invalid use of non-static data member `s::a' file.h:139: error: from this location
The bad way to do it would be to make the variable a global. So don't do that. Instead you could use a either number:
Or - the way I would do it - use a string.Code:struct s { char b[120]; };
You could use a vector as well.Code:struct s { string b; };
I may be completely wrong i'm just tapping from the top of my head so experiment
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
The reason I don't want to use a string is because the data is binary and may have null characters.
Try
May be what you're looking forCode:struct s { vector<char> b; };
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
It looks like std::string will work. Thanks for your input, ahluka.
Anytime
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
The reason why your compiler is complaining is because a is not initialized to anything, so b will become a very large array from a garbage value. Also when you change a, it will change the size of b and you cannot change sizes of indexing arrays.Originally Posted by OP
Strings can handle null characters.Code:The reason I don't want to use a string is because the data is binary and may have null characters.
Um no it can't. Not more than one it can't. A string by definition ends in a null. You can't use the string class for plain old binary data that may potentially contain more than one null. The null terminates the string. It can't have more than one. Other wise all of the string length functions and such will vomit when they hit the first null, considering the string as ended.
Someone feel free to show otherwise, but I'm fairly sure the string class considers a null character to be "the" end.
Quzah.
Hope is the first step on the road to disappointment.
I don't know exactly what the C++ standard says about the string class. But generally speaking, implementations of the string class do not use a null-terminator to mark the end. For example:
This will outputCode:#include <iostream> #include <string> using std::string; using std::endl; using std::cout; int main() { string x("abcdefghijklmnop"); x[5] = 0; cout << x << endl; }
on some terminals. Other terminals might behave differently with the zero character; cygwin Xterm is displayingCode:abcde ghijklmnopIf I reroute stdout to a file, then NUL is clearly and obviously written to the file. C++ string classes generally do not use strlen when you call .size().Code:abcdeghijklmnop