Last edited by BEN10; 04-30-2009 at 10:07 PM.
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
nowhere, i said it to strickyc about the such initialization of char arrayOriginally Posted by BEN10
I never thought that in arr[N] array N+1 elements, it was seemed to strickyc
But your the one who says
char a[5] = "ABCDE";
is correct coding. And I'm pretty sure K&R point out that this is incorrect.
C by example also says this is wrong. C how to program by Deitel & Deitel also point it out as wrong.
I'm sure every book with a beginners chapter on character arrays points this out as wrong.
Last edited by strickyc; 05-01-2009 at 07:57 PM.
I've already pointed out that it's legal in C, when initializing character arrays at the time of declaration. For any other variable type it's not. But they allow it for character arrays in C. Not in C++, but C allows it.
In my opinion, it shouldn't be legal; but it is allowed.
Quzah.
Hope is the first step on the road to disappointment.
Like I said earlier, Its a logical error and not a programming error. In some cases you don't really need the last '\0', but thats only when you don't plan to use the char array as a string and just need the space of the last element of the array. I personally don't know much about C++ and I was under the impression this was a C board. If I wanted to learn C++ I'd go to a C++ forum, But I use C so I come here.
It's both. It's a logic error, but were it any other data type, and you were providing more initializers than elements, it would be a programming error. There's no reason it should be allowed, other than for the sake of "because we wanted it to".
And no one gives a ........ if you want to learn C++ or not. I was illustrating the point that while I feel it should be an error in C, the authors of C++ apparently decided it was wrong as well, since they've changed the legality of it.
Providing more initializers than element space should always generate an error. There shouldn't be one specific case where it isn't an error. I know why they did it; because they wanted an easy way to fill character arrays, without having to initialize them by hand. But you can't do it for any other data type, so you shouldn't be able to do it for this one.
Your "in come cases you don't really need..." statement is irrelevant. In the event you don't need a null character, you should be forced to initialize each element one at a time, the same as any other array. It's a poor idea, and it's one of the reasons people get confused about the boundries of arrays.
"Well how come I can do it here?"
"Oh, just because. There's no logical reason for it. This is an exception for JUST this data type."
"That's dumb."
"Yeah well that's how it goes."
Furthermore, since half the new people that show up here are compiling on a C++ compiler anyway, we end up answering the question "But why does it generate an error for me if it's allowed?" anyway. So regardless if YOU want to learn C++ or not, you'll need to understand that I'm not here just for YOU.
Quzah.
Hope is the first step on the road to disappointment.
But what if I was using a char array as a bitfield instead of creating a string? The it wouldn't be a logical error.
C and C++ eventhough are closely related are two different programming languages. Where inAnd no one gives a ........ if you want to learn C++ or not. I was illustrating the point that while I feel it should be an error in C, the authors of C++ apparently decided it was wrong as well, since they've changed the legality of it.
C++ you have a boolean in C you tehnicaly don't. If you have the need of several Bools in c
you can just use one char bits. C++ isn't C, but they do influence each other. What would be the use of two programming languages that do exactly the same thing?
I agree to an extent.Providing more initializers than element space should always generate an error. There shouldn't be one specific case where it isn't an error. I know why they did it; because they wanted an easy way to fill character arrays, without having to initialize them by hand. But you can't do it for any other data type, so you shouldn't be able to do it for this one.
but his code even though a logical error isn't wrong cause the array has room for what hes trying to do. They are just more uses for a char than storing letters and symbols this is why they did it.
I think the only reason I know that people get confused about arrays is the off by one error.Your "in come cases you don't really need..." statement is irrelevant. In the event you don't need a null character, you should be forced to initialize each element one at a time, the same as any other array. It's a poor idea, and it's one of the reasons people get confused about the boundries of arrays.
takes some time to remeber that and array of 20 elements end on the 19th one.
and with initialized character arrays the null terminating character is added automatically
Like I said sometimes its logical to do it and other times it isn't."Well how come I can do it here?"
"Oh, just because. There's no logical reason for it. This is an exception for JUST this data type."
"That's dumb."
"Yeah well that's how it goes."
Don't try to teach C by teaching C++. C isnt a superset of C++ but C++ is a superset of C.Furthermore, since half the new people that show up here are compiling on a C++ compiler anyway, we end up answering the question "But why does it generate an error for me if it's allowed?" anyway. So regardless if YOU want to learn C++ or not, you'll need to understand that I'm not here just for YOU.
Your signature is offensive to little old ladies.Quzah.
Suck my ass.
Last edited by strickyc; 05-01-2009 at 09:12 PM.
No one claimed otherwise.C and C++ eventhough are closely related are two different programming languages.
Actually, in C you technically do. For the last 10 years, C has had bool.Where in C++ you have a boolean in C you tehnicaly don't.
Actually, C++ is not a superset of C. There are things you can do in C that you cannot do in C++. VLAs for instance.Don't try to teach C by teaching C++. C isnt a superset of C++ but C++ is a superset of C.
It's nice of you to come here and give us a lesson on the differences between C and C++ though.
What does that have to do with this conversation? First of all, you typically want unsigned values for bitfields. Second of all, why would you be initializing it as a string? Your bitfield use is going to be restricted enough so that it's all readable as a string? Then why are you bothering with bitfields? Yes, you would have a logical error, because it would be stupid to initialize bitfields as strings in nearly any way I can conceive. You want to initialize a bitfield something like:Not as a string. So again, we're all at a point where none of us know what it is you're trying to say.No it doesn't, and no he didn't. He never tried storing more than characters in his string. Neither did you. As a matter of fact, no one in this entire thread tried using it for anything other than letters.Code:unsigned char bitfield[ SOMESIZE ] = { FLAG_X | FLAG_Y, FLAG_Z | FLAG_C | FLAG_G , ... };
I'm not even sure what you're arguing here any more. We both agreed that he was using an incomplete string, and that his initialization was fine. We also both agreed that using string.h was fine, so long as he pays attention to the length allocated for his incomplete array.
What you seem to have been confused on, was if this was legal or not:It's been covered, it is. But only for this data type. All other data types cannot have more initializers than elements.Code:char array[ 5 ] = "12345";
It's never logical to have more initializers than you have elements. Using a string gives you one more initializer than you have elements, if you have as many non-null-character elements as you have array elements. It's just a stupid "well, we'll pretend it's not there" clause for this one special case.
Stop telling me how to teach, since I'm fairly certain that everyone here would agree that I am far better at getting my points across than you are yours.
To quote Rhett Butler, "Frankly, my dear, I don't give a damn."
Quzah.
Hope is the first step on the road to disappointment.
I have Visual Studio 2005 and it doesn't compile:
The error, as alluded to many times in this thread, is:Code:#include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { char a1[5] = "ABCDE"; return 0; }
I just thought I'd put the record straight for Microsoft's sake! (And also because I thought it should throw an error too.)Code:'a1' : array bounds overflow
It is offensive, but then it's also childish. Why you'd want people to have to continuously read your filth is beyond me. It probably drives people away - or is that your intent?
Code:C99 6.7.8 [#14] An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
Microsoft compilers have never been ones to keep with the standards. Their compiler deciding to compile or not deciding to compile doesn't mean they're any more or less compliant with the standard. The standard, as we've mentioned in this thread multiple times, states that it's fine for character arrays.I have Visual Studio 2005 and it doesn't compile:One person's trash is another person's treasure. If you can't handle some "bad" words, get off of the internet.It is offensive, but then it's also childish. Why you'd want people to have to continuously read your filth is beyond me. It probably drives people away - or is that your intent?
Quzah.
Hope is the first step on the road to disappointment.