Thread: Beginner help about strings

  1. #31
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    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.
    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

  2. #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

  3. #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.

  4. #34
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  5. #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.

  6. #36
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  7. #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.
    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?
    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.
    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
    "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.
    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.


    Quzah.
    Your signature is offensive to little old ladies.
    Suck my ass.
    Last edited by strickyc; 05-01-2009 at 09:12 PM.

  8. #38
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    C and C++ eventhough are closely related are two different programming languages.
    No one claimed otherwise.
    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.

    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.

  9. #39
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  10. #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?

  11. #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.

  12. #42
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. beginner question about strings
    By gp364481 in forum C Programming
    Replies: 4
    Last Post: 09-05-2008, 06:31 PM
  3. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 04:29 PM
  4. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  5. A beginner passing strings
    By jamjar in forum C Programming
    Replies: 2
    Last Post: 09-01-2002, 12:50 AM

Tags for this Thread