C and strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CBFalconer

    #16
    Re: C and strings

    "bwaichu@yahoo. com" wrote:[color=blue]
    >
    > If you are looking for safer, look at the strlcpy() over at
    > openBSD or write one like it.[/color]

    Include context, damn it. Follow the instructions in my sig below.

    For strlcpy, you can get a suitable portable source at:

    <http://cbfalconer.home .att.net/download/strlcpy.zip>

    --
    "If you want to post a followup via groups.google.c om, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson


    Comment

    • Keith Thompson

      #17
      Re: C and strings

      Anthony Irwin <nospam@example .com> writes:[color=blue]
      > On Wed, 10 Aug 2005 18:01:54 -0700, kernelxu wrote:
      >[color=green]
      >> I agree with John.
      >> using strncpy() is better than strcpy().
      >>
      >> if teststring1 doesn't have enough room for the copied string , there
      >> will be a overflow.
      >> strncpy() reminds you pay attention to it.[/color]
      >
      > Ok so I do the following:
      >
      > strncpy(teststr ing, "I changed teststring value", sizeof teststring -1);
      >
      > Will that put the null terminator on the string[/color]

      It will if there's enough room for it. If sizeof teststring - 1
      happens to be 26 (the length of your string literal), strncpy() won't
      write a '\0' to teststring.
      [color=blue]
      > or should I do the
      > following:
      >
      > strncpy(teststr ing, "I changed teststring value\0", sizeof teststring -1);[/color]

      No, that's equivalent. strncpy() treats its second argument as a
      pointer to the first character of a string, i.e., as an array of
      characters terminated by the first '\0' character. The string literals
      "I changed teststring value"
      and
      "I changed teststring value\0"
      specify different character array values, but they specify the same
      string.

      You want to copy a string into the variable teststring without
      overflowing it. If you *know* that teststring is big enough to hold
      the value you want to copy to it, just use strcpy(). If you don't
      know this, you have to ask yourself what you want to do if there isn't
      enough room. If strncpy's behavior matches what you need, then by all
      means use it (and don't treat teststring as a string unless you're
      sure it's properly terminated). If not, you'll have to do something
      else.

      Sometimes the hardest part of error handling is deciding what to do if
      the error occurs. And once you've decided that, the error-handling
      code can sometimes be more complex than the code that handles the
      normal case.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
      We must do something. This is something. Therefore, we must do this.

      Comment

      • Peter Nilsson

        #18
        Re: C and strings

        Malcolm wrote:[color=blue]
        > "strings" are sequences of chars terminated by a NUL (ASCII zero).[/color]

        No, strings are sequences of characters terminated by a null (zero)
        byte.

        There is no requirement that ASCII be the basic or execution character
        set
        of any implementation. Of course, most are, but there is rarely any
        reason
        to write C programs that assumes this, even if you are writing a
        program
        that is not strictly conforming.

        --
        Peter

        Comment

        • Lawrence Kirby

          #19
          Re: C and strings

          On Thu, 11 Aug 2005 02:13:53 +0000, Rich Webb wrote:
          [color=blue]
          > On Thu, 11 Aug 2005 01:20:02 GMT, Keith Thompson <kst-u@mib.org> wrote:
          >[color=green]
          >>Second, strncpy() isn't as superior to strcpy() as you might think.
          >>It does let you limit the number of bytes to copy, but its behavior is
          >>questionabl e in many cases. For example, if you do
          >> strncpy(s, "hello", 5);
          >>it will copy exactly 5 characters to s; it will *not* copy the
          >>terminating '\0' character. On the other hand, if you do[/color]
          > [snip...snip...][color=green]
          >>strcpy() is safe *if* you first confirm that the target is big enough
          >>to hold the string being copied. strncpy() can avoid array overflow
          >>bugs, but at the expense of introducing other problems.[/color]
          >
          > There's always the option of
          > strncpy(s, "hello", S_BUF_LEN)[S_BUF_LEN - 1] = '\0';[/color]

          The best advice is simply not to use strncpy(), it isn't a true string
          handling function so don't try to use it as one. A far better function for
          this is strncat()

          s[0] = '\0';
          strncat(s, "Hello", S_BUF_LEN-1);

          and don't forget memcpy() which can sometimes be an appropriate
          alternative. snprintf() is very powerful when you're building a string
          from a number of parts:

          snprintf(s, S_BUF_LEN, "%s %.*s", str1, maxlen, str2);

          Lawrence

          Comment

          • Joe Wright

            #20
            Re: C and strings

            Malcolm wrote:[color=blue]
            > "Anthony Irwin" <nospam@example .com> wrote
            >[color=green]
            >>I am new to c and am having trouble with strings can anyone give me some
            >>quick pointers or point me to a good c strings tutorial?
            >>[/color]
            >
            > C strings are often confusing for people who come from another programming
            > language.
            >
            > C is so low-level that effectively there is no such thing as a string type.
            > "strings" are sequences of chars terminated by a NUL (ASCII zero).
            >
            > The only support C gives for strings in the strict sense of the word is that
            > entering a string literal
            > char *str = "Fred Bloggs";
            > creates the sequence 'F' 'r' 'e' 'd' ' ' 'B' 'l' 'o' 'g' 'g' 's' '\0'
            > in memory.
            >
            > Also there are library functions which operate on such sequences.
            >
            > The best tutorial on strings is to try to implement strlen(), strcpy() etc
            > yourself. call them my-strlen() etc to avoid namespace problems.
            >[/color]

            I would avoid arithmetic operators in composing function names. It's
            actually a syntax error. my_strlen() is much better.
            [color=blue]
            >
            >[color=green]
            >>Below is a
            >>program I am working on.
            >>
            >>Kind Regards,
            >>Anthony Irwin
            >>
            >>#include <stdio.h>
            >>
            >>char teststring[40] = "test string now";
            >>char teststring1[40];
            >>
            >>int main() {
            >> printf("teststr ing = %s", teststring);
            >> strcpy(teststri ng1, teststring);
            >> printf("\ntests tring1 = %s", teststring1);
            >>
            >> /* How can I assign a new value to teststring say
            >> * the value "I changed string text" */
            >>
            >> printf("\n\nI changed teststring to = %s\n", teststring);
            >>
            >> return 0;
            >>}[/color]
            >
            >
            >[/color]


            --
            Joe Wright
            "Everything should be made as simple as possible, but not simpler."
            --- Albert Einstein ---

            Comment

            Working...