Thread: Character arrays

  1. #1
    Registered User
    Join Date
    Aug 2001
    Location
    Melbourne, Australia
    Posts
    92

    Question Character arrays

    When I have a character array like this...

    Code:
    char mystring[10];
    Doesn't it actually create 11 elements? (one for the null-termination).

    I just need this confirmed... i'm a little iffy on this.

    Thanks,
    PsychoBrat
    psychobrat at gmail

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    yep, the first one is at mystring[0]

  3. #3
    Unregistered
    Guest
    No!

    char array[10];
    this creates array of 10(ten) charracters

    array[0] .... array[9]

    And none of them is '\0' char

    same goes for
    int array[10];

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    oh crap...woops. My post could have been a result of the fact that it is pretty late in Australia right now...

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    29
    It creates array of 10 sizeof(char), so 10 bytes usually.
    If you want to use '\0'(and you have to, if you want to use that array as string), it contains only 9 useful characters, so if you are creating array, it is better to be careful and create it with enough size, for example 11 because if you don't you can get very queer run-time errors then..

  6. #6
    Registered User
    Join Date
    Aug 2001
    Location
    Melbourne, Australia
    Posts
    92
    Oh... so if you're using something that needs null-termination will it treat a full string of 10 characters as if it has a \0 after it anyway?

    Sorry i'm a little confused... cuz i was told by some other guy that it automatically added an extra element, which seems a bit weird! Now its starting to make a *little* more sence...

    Thanks!
    PsychoBrat

  7. #7
    Unregistered
    Guest
    char temp[10] reserves space in memory for 10 elements of type char. Each char in temp must be valid, but since every key on the keyboard generates a valid char, the concern about validity is another topic. Other than that the char can be anything you want it to be. You could have 10 null char in temp, or 8 lower case a's and an ampersand and a tab character. You could have 10 spaces. you could have 10 upper case letters working backward from Z. If your character set allows it (meaning it is valid) you could have french or russian or chinese characters. Whatever.

    Having said that, just because you have the space reserved doesn't mean you need to use it all. You could have 10 valid elements of type char in temp, but you could have 8 or 2 or none, too. The point being, you can't have _more than_ 10.

    In order for an array of type char to be considered a string it must have a null char in it. Let's say you have, in this order, the characters p i '\0' e, in temp. Then you could post all 4 char to the screen using a loop (you wouldn't actually see the third char as it is the explicit representation of the null char, which is not visible, but that's beside the point). Alternatively, you could use the >> istream operator to post the first two letters to the screen as a string, because they constitute a null terminated char array, which is the definition of a string in standard C/C++ (note members of a string class may or may not be the same thing as a null terminated char array so beware!). Most of the time you don't put additional char elements in the char array after the null char, but it is legal to do so, it is just superfluous in most circumstances.

    Ok, so what. Well, if you want to store a group of 10 char in an array as a single string, then the array must have room for 10 non null char elements and room for one null char element (which will be found at the end of the array). That is, you need an array with space for 11 char elements if you want to be able to hold a string with 10 useable char elements. The compiler doesn't care what characters you put in the array as long as you use loops to display each one, but if you want to use the array as a string, then there must be a null char in the array somewhere, and the null char will be the last, or terminal, char of a given string, eventhough you never visualize that null character in the output to the screen (or file or wherever).

    I'm jussa ramblin now so I better shudup.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Look at the function I posted HERE. Notice how I have to manually add a NULL (0) value to the end of the string...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  4. Comparing Character Arrays problem...
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 07:54 PM
  5. Spaces in Character Arrays
    By ADLOTS in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2002, 04:24 AM