So I copied a code exactly from the book here is the snipet where the problem is:
1 2 3 4 5 6 7 8
int main(int nNumberofArgs, char* pszArgs[])
{
cout << "This program concatenates two strings\n(this version may crash.)" << endl;
char szStrBuffer[256];
char szString1[];
strncpy(szString1, "This is a string", 16);
char szString2[];
strncpy(szString2, "THIS IS A STRING", 16);
The compiler(Microsoft Visual) gives me "error C2133: 'szString1' : unknown size"
I know that declaring an arbitrary size fixes the problem but why can't I just declare the string without a size like in the book?
The size must be known in the declaration of the array furthermore, the compiler doesn't know what strncpy does and it wouldn't be executed at compile time anyway
Use strings not char[]. No size issues in sight.
And what's up with those arguments for main? Are those valid? (I suppose that the names are not forced; it just has to be int and char** types.)
I thought so. It would really make no sense to force the names to be something, not to mention that that would be effectively un-enforceable.
Also, although strings are superior to char[] in pretty much every way (the only disadvantage being that char[] is primitive while you need includes to get <string>), most books should at least cover them to explain their use and their glaring weaknesses (array size declaration I'm talking about you).
Strings would be perfect if they could somehow be a primitive type. But that kind of defeats the purpose of it being a class.