Thread: Can someone please explain strings to me?

  1. #16
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    stupid question : if older compilers didnt add the /0 at the end of string for you but the ne wones do what happens if in a new compiler you add the /0 will the compiler add another to the end of that or does it know ones already there?

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by lithium
    also never use:
    strcpy
    strcmp

    or functions similar to those. instead use:
    strncpy
    strncmp

    this will aviod buffer overflows (segmentation faults).
    @lithium: Please explain how you get a buffer overflow from using strcmp().

    For those that aren't aware, strncpy() isn't guaranteed to null terminate the array.
    Code:
    /*
     * The error here is that use programmer expected 
     * strncpy() to make s equal BBBB\0 or BBBBB\0.  
     * See the results below to see what actually happens.
     */
    
    #include <string.h>
    #include <stdio.h>
    
    int main(void)
    {
      char s[10] = "aaaaaaaa";
      puts(s);
      strncpy(s, "BBBBBBBB", 5);
      puts(s);
    }
    
    /* 
      Output:
      
      aaaaaaaa
      BBBBBaaa
      
      */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Originally posted by Thantos
    Considering that a good number of students are using a Borland compiler from the 1980s I'd say that there is a good chance.
    Ah, why should I have assumed that *everyone* would be using the latest version of gcc? (kidding, but yeah I was not thinking of that at all)

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