-
March 4th, 2011, 11:18 PM
#1
Is char s[3] = "abc" legal?
I tried to compile it in VC++ 6.0, there is a compiler error. But a string could be terminated without '\0', couldn't it? Thanks.
-
March 4th, 2011, 11:44 PM
#2
Re: Is char s[3] = "abc" legal?
"abc" is 4 characters. 'a', 'b', 'c', '\0'.
-
March 4th, 2011, 11:47 PM
#3
Re: Is char s[3] = "abc" legal?
Originally Posted by Speedo
"abc" is 4 characters. 'a', 'b', 'c', '\0'.
I wonder if it is possible for "abc" is terminated without '\0'?
-
March 5th, 2011, 12:12 AM
#4
Re: Is char s[3] = "abc" legal?
Code:
char s[3] = { 'a','b','c' };
Is one way of accomplishing that.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
March 5th, 2011, 12:16 AM
#5
Re: Is char s[3] = "abc" legal?
Originally Posted by Russco
Code:
char s[3] = { 'a','b','c' };
Is one way of accomplishing that.
That is cool. Any other way? Thanks.
-
March 5th, 2011, 01:23 AM
#6
Re: Is char s[3] = "abc" legal?
Code:
char s[3];
memcpy(s, "ABC", 3);
This is asking for trouble unless you know what you are doing...
-
March 5th, 2011, 08:53 AM
#7
Re: Is char s[3] = "abc" legal?
No char s[3] = "abc" is not legal since "abc" is a const char* (or at least considered as const).
-
March 5th, 2011, 08:59 AM
#8
Re: Is char s[3] = "abc" legal?
Originally Posted by S_M_A
No char s[3] = "abc" is not legal since "abc" is a const char* (or at least considered as const).
the problem is not the fact that "abc" is a const literal; you can initialize fixed size char arrays with a const literal; the problem is that the to be initialized array must have sufficient characters to store the literal, including the null terminator;
for example, "char s[3] = "abc";" does not compile, but "char s[4] = "abc";", "char s[5] = "abc";", ... does compile and it's legal.
-
March 5th, 2011, 09:29 AM
#9
Re: Is char s[3] = "abc" legal?
Eh, totally messed this up in my head...
Have lately changed a lot of legacy code due to deprecated warnings about char* being assigned with string literals. Seems like that has gotten to me worse than I thought...
-
March 5th, 2011, 09:57 AM
#10
Re: Is char s[3] = "abc" legal?
Originally Posted by LarryChen
I tried to compile it in VC++ 6.0, there is a compiler error. But a string could be terminated without '\0', couldn't it? Thanks.
why aren't you just writing:
?
but if you really don't want to waste that one byte (which is the '\0' character) you can also write like this:
Code:
char s[3];
s[0] = 'a';
s[1] = 'b';
s[2] = 'c';
it's pretty the same way as what Russco said ( char s[3] = { 'a','b','c' }; ) only that by the method I've shown you write a bit more.
-
March 5th, 2011, 11:55 AM
#11
Re: Is char s[3] = "abc" legal?
Originally Posted by LarryChen
I wonder if it is possible for "abc" is terminated without '\0'?
Sure it's possible. But why would you want to? The concept of c-style strings revolves around them being null-terminated. The only time I can think of when you might even think about doing it is in the internals of a string class. In pretty much any other circumstance, you're just making more work for yourself.
-
March 6th, 2011, 09:52 AM
#12
Re: Is char s[3] = "abc" legal?
Originally Posted by S_M_A
Eh, totally messed this up in my head...
Have lately changed a lot of legacy code due to deprecated warnings about char* being assigned with string literals. Seems like that has gotten to me worse than I thought...
We've all done that, I still have some legacy code which does that.
-
March 6th, 2011, 11:19 AM
#13
Re: Is char s[3] = "abc" legal?
Well, in C, this is valid:
being equivalent to:
Code:
char s[3] = {'a', 'b', 'c'};
But I don't consider it advisable since readers may ask if it is valid.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|