Thread: basic nooby c string question

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    5

    basic nooby c string question

    I am always confused at the way which is used to declare a c string.

    so i assume we can do it either by
    char *str;

    or by doing
    char str[10];

    so what is the difference here? it seems that if i do it the second way then my length of string is limited to 9, and if i do it in the first way, i can just say str="random blah blah blah" and it can be as long as i want.

    can some pros out there give me some enlighten me a lil bit? thx.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's helpful to keep these tips in mind:

    1) Strings are simply one or more char's, which we can elevate to a string, if we add the end of string char after it or them.

    'A' is a char. "A\0" is a string. "ab" is two char's. "ab\0" is a two char which has been elevated to a string.

    You can declare a string either way. Say you put the name "Stephen", in the first string. Your string would be "Stephen\0". 7 char's + 1 end of string char.

    Say now you put the same name into the second array of char's. It would be:
    0123456789
    Stephen\0JU

    Where JU at the end could be anything - junk, NULL's, you name it. The point is that in the second instance, you've wasted 2 bytes of memory. In the first instance, you've wasted zero bytes.

    The second instance, with the array of char's is easier for most people to figure out and work with, and the small waste is acceptable - we're not going to protest in the streets over 2 whole wasted bytes.

    But if you had a dictionary with hundreds or thousands of words for your game, the waste becomes huge and unacceptable, since in the case of the array, every row would need to be the same length: Axe would have just as much memory as Blacksmith.

    Internally, C has no arrays. They are a convenience for us programmers, because people are so good at working things into 1, 2, or 3 dimension arrays. Arrays are immediately transferred by C into pointers with offsets to give their lengths:

    Array[10] is immediately changed into *(Array + 10), by the compiler. It's no coincidence that the name of the array, is also the address of the first element of the array (also called the base of the array). In other words it becomes a constant pointer.

    Since the compiler does this transformation for us, why not just skip the Array[10] format altogether? Because programming is about idea's, and working out problems, and Array[10] is more helpful than *(Array + 10), is. So I let the compiler do it's job, and I'll do mine.
    Last edited by Adak; 01-19-2008 at 08:16 AM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Another point to keep in mind, speaking of memory storage, is that the declaration str[10] allocates us room for 10 characters (of which we can use 9) while the declaration *str gives us no memory whatsoever.[1] If we use *str, it must either be initialized to a string literal (in which case, we can't change it), or must have allocated memory to it with malloc().

    [1] Of course, we get some memory, namely enough memory to put an address in, but not any memory we can use to store characters in.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Array[10], gives us 10 elements we can use, not 9. 0-9 IS 10.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Just a small nit... the end of string character, also called the null terminating character, is an escape sequence defined by the 2-character sequence \0 (back slash, zero), not /0 (forward slash, zero).

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by tabstop View Post
    Another point to keep in mind, speaking of memory storage, is that the declaration str[10] allocates us room for 10 characters (of which we can use 9) while the declaration *str gives us no memory whatsoever.[1] If we use *str, it must either be initialized to a string literal (in which case, we can't change it), or must have allocated memory to it with malloc().

    [1] Of course, we get some memory, namely enough memory to put an address in, but not any memory we can use to store characters in.
    char *str = "string";

    Does create a string literal - however, it is not necessarily located in read-only memory. AFAIK; it's undefined where it gets put and depends on the compiler and architecture.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thank you, Todd. Made that correction.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Adak View Post
    Array[10], gives us 10 elements we can use, not 9. 0-9 IS 10.
    I just meant that we must be sure to not use the 10th character, since we need a \0 at the end.

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    5
    thank you kind people. i can go and do my project now:d

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM