Thread: Can someone please explain strings to me?

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    2

    Can someone please explain strings to me?

    I am really stupid and should have done this long ago. I have had my uni classes and struggled with C when it came to strings and pointers after reading the books and googling.

    Anyway, I am very competent at Java, but with C I can't understand strings at all. In Java all you do is
    Code:
    String myString = "blah";
    but in C there is all this stuff with arrays - I really can't get it.

    I know that the string is stored in contiguous form in the computer memory, however, I can't understand much more than that.

    Could some kind soul tell me how to initialise strings in C and (preferably in Lehmann's terms) explain exactly what is happening?

    Cheers!

    PS. I struggled so badly that I failed the coursework for the class and have had to do extra stuff over the summer instead of going out drinking every day.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    String myString = "blah"; --> char myString[] = "blah";

    Each letter in blah is just a character, the string is a bunch of characters called a array.

    So
    myString[0] = 'b'
    myString[1] = 'l'
    myString[2] = 'a'
    myString[3] = 'h'
    myString[4] = '\0';

    The '\0' marks the end of the string(called NULL). The "'" mark means takes its ASCII vaule, since thats all a char really is. Its one block of memory so it can be read straight threw like a line of skipped threw. The first points to the second wihich pont to the...

    If you wat to work with strings(like adding to them, searching) string.h comes in handy.

    Theres a couple of threads and sites that explains this better, its not that bad.

    I think Java is much worse(to learn) Im still fighting with it....

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    make sure you allocate memory for strings.

    Code:
    char sample[50];
    
    char* sample;
    sample = malloc(50);
    this shows two samples that will both work.

    string literals can be declared but not changed later on.

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    in the String class for java, I believe there is a function .toChar() or something. Well what that does is it turns the String into a char[]

    A string in C, is comprised of just an array of characters, there is no class in C.
    >Terminated by a NULL character ('\0')
    >Without the NULL character, it is just an array of characters, C will not recognize it as a String without the NULL
    >Thanks Quzah
    if your a beginner to C, try to avoid dynamic strings (strings where you change the size of it)

    and go ahead and be wasteful of memory like
    Code:
    char myStr[256];
    and to use strings, you can't just say
    myStr = "hi";
    there is a function in string.h to assign Literal strings (strings int he quotes) to the character array
    prototype:
    Code:
    int strcpy(char* destination, char* cpyString);
    lets say you allocate some memory for a string and then you want to put Hello world in it, you can do this many ways, here are some:

    Code:
    1:
      char *str = "Hello, World\n";
    2:
      char str[] = "Hello, World\n";
    3:
      char str[256]; 
      strcpy(str,"Hello, World\n");
    just remember that arrays are treated as pointers and similar. Please read the FAQ for more details.

    -LC
    Last edited by Lynux-Penguin; 08-27-2003 at 02:09 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Saravanan.T.S.
    Beginner.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A string in C, is comprised of just an array of characters
    No. This is not a string:
    Code:
    char foo[3] = { 'c', 'a', 't' };
    It has no null terminator, and as such, isn't a string. A string is an array of one or more charactes, terminated by a null. Without the null, it's just an array of characters.

    I know you know the difference, but just to clarify for those who don't.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    2
    thanks a lot you guys!

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Originally posted by quzah
    No. This is not a string:
    Code:
    char foo[3] = { 'c', 'a', 't' };
    It has no null terminator, and as such, isn't a string. A string is an array of one or more charactes, terminated by a null. Without the null, it's just an array of characters.

    I know you know the difference, but just to clarify for those who don't.

    Quzah.
    Of course that is not a string. It is an array of character constants. It would be a string constant if it was a seqence of one or more characters surrounded by double quotes. And then of course if it has the null character at the end (as you mentioned). But technically speaking, a string is an array of characters.
    Last edited by kermit; 08-27-2003 at 06:38 PM.

  9. #9
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    That is because the compiler adds a NULL character at the end of the double quotes, not all compilers do this, older C compilers do NOT do this, you have to add the '\0' at the end.

    and a string is NOT technically an array of characters. It is an array of characters with NULL at the end. The standard defines a string as an array of characters that is NULL terminated. In the world of programming, techincalities get sent to /dev/null

    in the older C compilers "cat" was illegal to use as a string. "cat\0" was the way you would have to do it. the NULL is VERY important in C, just dont forget the NULL and you will be alright


    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Ok, being precise does not hurt I suppose. I realised that a string is an array of characters terminated with NULL - I was not disputing that fact. Just as an aside, and not trying to be smart, how often do you think someone would be using an older compiler that required the insertion of '\0' at the end of a string?

  11. #11
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    im sorry, the point I was trying to make was, that it must be made very clear that there is in fact a NULL character at the end of every standard c string.

    and beacuse you don't see the null when you use quotes it must be made very clear (especially to new C programmers) that there is a NULL character.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    also never use:
    strcpy
    strcmp

    or functions similar to those. instead use:
    strncpy
    strncmp

    this will aviod buffer overflows (segmentation faults).

  13. #13
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    >this will aviod buffer overflows (segmentation faults)<

    Don't be so sure. strn* functions do not protect agains stupidity :
    Code:
    int main(void)
    {
        char s[5];
    
        strncpy(s, "A", 10);
    }
    $ENV: FreeBSD, gcc, emacs

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    how often do you think someone would be using an older compiler that required the insertion of '\0' at the end of a string?
    Considering that a good number of students are using a Borland compiler from the 1980s I'd say that there is a good chance.

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >NULL character

    [NITPICK]
    NULL, by its C standard definition, is "an implementation-defined null pointer constant" or "[t]he null pointer constant to which the macro NULL expands".

    This may be viewed as mere pedantry -- though it is an FAQ -- but I do think it may be worth pointing out that in C, NULL is implied to be a pointer and not a character. By using "NULL character", it might add to confusion for newcomers to the language, because it can have a distinctly different meaning than "null character", which '\0' represents.
    [/NITPICK]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to read strings?
    By happyclown in forum C Programming
    Replies: 10
    Last Post: 01-23-2009, 05:06 AM
  2. Passing strings to functions and modifying them
    By diddy02 in forum C Programming
    Replies: 6
    Last Post: 08-11-2008, 01:07 AM
  3. C++ Strings under the STL?
    By laserlight in forum C++ Programming
    Replies: 9
    Last Post: 07-19-2007, 02:55 AM
  4. Help w/ comparings two strings case sensitive
    By ikkin in forum C Programming
    Replies: 7
    Last Post: 11-13-2003, 08:26 AM
  5. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM