why do we have to declare a string like
char *x = "...";
or char x[] = "...";
?
also if i say char x[];
x[] = "..." it wont work why?
thanks
why do we have to declare a string like
char *x = "...";
or char x[] = "...";
?
also if i say char x[];
x[] = "..." it wont work why?
thanks
x[]=" "
doesn't mean anything because when the left hand side isn't a declaration, it expects a value between []. Also, char x[] has the same problem. Instead, you could go with:
char x[];
strcpy(x, "...");
that would copy "..." into x.
Elaborate on the why. I don't see a question.
char x[] defines an array with no size. How does this work?also if i say char x[];
x[] = "..." it wont work why?
thanks
x[] = "..." will not work because you don't specify what index to assign something to.
[] means that you want to assign something to a specific index in an array, to an individual char in this case. [] cannot be used to assign to multiple indexes in one statement.
Undefined behavior at its best. DO NOT DO THIS.
The fact that x has no size specified is disturbing. There is no such thing as an unlimited array. It is not dynamic, thus it always has and MUST have an explicit size. If you try to copy some data into an array whose size you have not specified, what do you think happens?
Yes it will. It's defined as an array of unspecified size, whose size is determined by the size of whatever = gives it. Thus:The size of foo is strlen( "hello world" ) + 1.It is actually defined. Since you have declared it as an array, using [], you are allowed to use the space that was initially created for it. In this case, you would have four characters you could use (in your example, the = was "...", which would be 4 bytes).It's not unlimited, it's an unspecified size--meaning YOU didn't type the actual size, but the compiler/language figured it out because of your = usage.Code:chat foo[] = "hello world";
Note, you cannot do this:You can't do that, because foo is an array, not a pointer. You also shouldn't attempt to modify string literals--no matter what idiots with ancient compilers tell you.Code:char foo[] = "hello"; ... foo = "world";
Quzah.
Hope is the first step on the road to disappointment.
Yes, but it's called initialization. Creating an array of unspecified size and then assigning to it is not the same thing. Assigning to it after creating it with an unspecified is undefined behavior.
Not specifying size when initializing it is fine as the compiler can figure out the size.
Sure, but your example is not what they are talking about. They are talking about this:Originally Posted by quzah
or this:Code:char x[]; x[] = "...";
In both cases, unlike what you are talking about, the array is not initialised by a string literal or initialiser, thus the empty brackets will result in a compile error. As such, Elysia is correct.Code:char x[]; strcpy(x, "...");
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
... if you are programming with respect to the 1999 edition of the C standard.Originally Posted by quzah
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)