C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-30-2009, 09:56 PM   #31
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
Quote:
Originally Posted by c.user View Post
no, I don't think that, it is from a book K&R

Can u tell me where it is said in K&R that char a[5] has 6 elements in it. I'm asking this because i'm reading K&R. And as far as i know cahr a[5] has 5 characters in it including the '\0' character so in effect it has 4 usable characters.
__________________
HOPE YOU UNDERSTAND.......

for( ; ; )
printf("If you can't make it good, at least make it look good");

PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition

Last edited by BEN10; 04-30-2009 at 10:07 PM.
BEN10 is offline   Reply With Quote
Old 04-30-2009, 11:01 PM   #32
Registered User
 
Join Date: Apr 2009
Location: Russia
Posts: 116
Quote:
Originally Posted by BEN10
Can u tell me where it is said in K&R that char a[5] has 6 elements in it
nowhere, i said it to strickyc about the such initialization of char array
I never thought that in arr[N] array N+1 elements, it was seemed to strickyc
c.user is offline   Reply With Quote
Old 05-01-2009, 07:53 PM   #33
Registered User
 
Join Date: Apr 2009
Posts: 41
Quote:
Originally Posted by c.user View Post
nowhere, i said it to strickyc about the such initialization of char array
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.
strickyc is offline   Reply With Quote
Old 05-01-2009, 07:59 PM   #34
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,260
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 05-01-2009, 08:09 PM   #35
Registered User
 
Join Date: Apr 2009
Posts: 41
Quote:
Originally Posted by quzah View Post
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.
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.
strickyc is offline   Reply With Quote
Old 05-01-2009, 08:25 PM   #36
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,260
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 05-01-2009, 09:08 PM   #37
Registered User
 
Join Date: Apr 2009
Posts: 41
Quote:
Originally Posted by quzah View Post
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".
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.
Quote:
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.
C and C++ eventhough are closely related are two different programming languages. Where in
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?
Quote:
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.
I agree to an extent.
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.
Quote:
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.
I think the only reason I know that people get confused about arrays is the off by one error.
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
Quote:
"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."
Like I said sometimes its logical to do it and other times it isn't.
Quote:
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.
Don't try to teach C by teaching C++. C isnt a superset of C++ but C++ is a superset of C.


Quote:
Quzah.
Your signature is offensive to little old ladies.
Suck my ass.

Last edited by strickyc; 05-01-2009 at 09:12 PM.
strickyc is offline   Reply With Quote
Old 05-01-2009, 10:56 PM   #38
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Quote:
C and C++ eventhough are closely related are two different programming languages.
No one claimed otherwise.
Quote:
Where in C++ you have a boolean in C you tehnicaly don't.
Actually, in C you technically do. For the last 10 years, C has had bool.

Quote:
Don't try to teach C by teaching C++. C isnt a superset of C++ but C++ is a superset of C.
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.

It's nice of you to come here and give us a lesson on the differences between C and C++ though.
bithub is offline   Reply With Quote
Old 05-01-2009, 11:38 PM   #39
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,260
Quote:
Originally Posted by strickyc View Post
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.
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:
Code:
unsigned char bitfield[ SOMESIZE ] =
{
    FLAG_X | FLAG_Y,
    FLAG_Z | FLAG_C | FLAG_G ,
    ...
};
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.
Quote:
Originally Posted by strickyc View Post
I agree to an extent.
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.
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.

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:
Code:
char array[ 5 ] = "12345";
It's been covered, it is. But only for this data type. All other data types cannot have more initializers than elements.
Quote:
Originally Posted by strickyc View Post
Like I said sometimes its logical to do it and other times it isn't.
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.
Quote:
Originally Posted by strickyc View Post
Don't try to teach C by teaching C++. C isnt a superset of C++ but C++ is a superset of C.
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.
Quote:
Originally Posted by strickyc View Post
Your signature is offensive to little old ladies.
To quote Rhett Butler, "Frankly, my dear, I don't give a damn."


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 05-18-2009, 09:55 AM   #40
Registered User
 
Join Date: May 2009
Posts: 1
Quote:
Originally Posted by strickyc View Post
char a[5] = "ABCDE";

is junk. incorrect, and a very bad example.
does that even compile with your compiler?
If it does tell me which one.
Quote:
Originally Posted by BEN10 View Post
its in my signature. visual studio 2005. and i dont think its a bad compiler at all.
I have Visual Studio 2005 and it doesn't compile:

Code:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
	char a1[5] = "ABCDE";
	return 0;
}
The error, as alluded to many times in this thread, is:
Code:
'a1' : array bounds overflow
I just thought I'd put the record straight for Microsoft's sake! (And also because I thought it should throw an error too.)

Quote:
Originally Posted by strickyc View Post
Your signature is offensive to little old ladies.
Quote:
Originally Posted by quzah View Post
To quote Rhett Butler, "Frankly, my dear, I don't give a damn."
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?
smelt is offline   Reply With Quote
Old 05-18-2009, 09:18 PM   #41
Registered User
 
Join Date: Apr 2009
Location: Russia
Posts: 116
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.
c.user is offline   Reply With Quote
Old 05-18-2009, 10:41 PM   #42
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,260
Quote:
I have Visual Studio 2005 and it doesn't compile:
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.
Quote:
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?
One person's trash is another person's treasure. If you can't handle some "bad" words, get off of the internet.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Reply

Tags
string

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with comparing strings (I'm a beginner C programmer) Bandizzle C Programming 2 04-29-2009 10:13 AM
beginner question about strings gp364481 C Programming 4 09-05-2008 06:31 PM
beginner: dynamic array of strings pc2-brazil C++ Programming 10 04-29-2008 04:29 PM
Strings Program limergal C++ Programming 4 12-02-2006 03:24 PM
A beginner passing strings jamjar C Programming 2 09-01-2002 12:50 AM


All times are GMT -6. The time now is 04:12 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22