strcat strncat and strlen

Discussion in 'C Programming' started by Bill Cunningham, Mar 31, 2008.

  1. Strncat is supposed to be better than strcat for some reason I've
    read. Is this because of a potential buffer overflow? I have compiled
    properly and used strlen too and I just wonder what is the need to return a
    strlen?

    Has anyone used quite abit anyway the strlen function?

    Bill
    Bill Cunningham, Mar 31, 2008
    #1
    1. Advertising

  2. Bill Cunningham

    santosh Guest

    Bill Cunningham wrote:

    > Strncat is supposed to be better than strcat for some reason
    > I've read.


    It does something different to what it's name would suggest. We have had
    innumerable threads dealing with this topic. Just do a Google search.

    > Is this because of a potential buffer overflow?


    Well, it can be used with that aim, in which case it leaves an array of
    char instead of a string in the destination.

    > I have compiled properly and used strlen too and I just wonder what is
    > the need to return a strlen?
    >
    > Has anyone used quite abit anyway the strlen function?


    You are not making any sense. Are you asking why strlen exists? It
    should be obvious, even to you. How else do you find the length of a
    zero terminated string?
    santosh, Mar 31, 2008
    #2
    1. Advertising

  3. > You are not making any sense. Are you asking why strlen exists? It
    > should be obvious, even to you. How else do you find the length of a
    > zero terminated string?


    C must not count '\0' as being part of a string. This is the simple code
    I tried,

    int main(void) {
    size_t t;
    char hello[]="hello world\n";
    t=strlen(hello);
    printf("%i",hello);
    }
    The integer returned was 12. When I removed the '\n' from the string
    and recompiled 11 was the number returned.
    Why in production code would someone want to know the length of a
    string? That's what I am asking. Hence my inexperience speaks for itself.
    Bill Cunningham, Mar 31, 2008
    #3
  4. Bill Cunningham

    santosh Guest

    Bill Cunningham wrote:

    >> You are not making any sense. Are you asking why strlen exists? It
    >> should be obvious, even to you. How else do you find the length of a
    >> zero terminated string?

    >
    > C must not count '\0' as being part of a string.


    It is a part of a C string. However it is not counted by strlen.

    > This is the simple code I tried,
    >
    > int main(void) {
    > size_t t;
    > char hello[]="hello world\n";
    > t=strlen(hello);
    > printf("%i",hello);


    The format for size_t is %zu. If your compiler does not support this
    then the next best method is to use %lu and cast it's argument to
    unsigned long.

    > }
    > The integer returned was 12. When I removed the '\n' from the
    > string and recompiled 11 was the number returned.


    Yes. So?

    > Why in production code would someone want to know the length of a
    > string? That's what I am asking. Hence my inexperience speaks for
    > itself.


    Not all situations are such that we can know the length of a string at
    compile time, as in your example above. Often strings are constructed
    at runtime, received from external files or the user and many programs
    do quite a lot of string processing, like splitting, concatenating,
    etc. Also even if your code knows the lengths of your strings, other
    library code will not. One way for them to act sanely upon your strings
    is to use strlen to get their length, or watch out for the null
    character as they process it.

    For example in the program:

    int main(int argc, char **argv) {
    /* ... */
    }

    if the program is given any command line parameters, then there is no
    way for the program to find out their lengths other than by using
    strlen (or equivalent inline code).

    So strlen is necessary with C strings because they don't carry their
    length with them. However good programs try to minimise the use of
    strlen by caching previous results and storing the lengths of known
    strings, instead of discarding the information and recomputing it every
    here and there.
    santosh, Mar 31, 2008
    #4
  5. Bill Cunningham

    pete Guest

    Bill Cunningham wrote:
    >
    > Strncat is supposed
    > to be better than strcat for some reason I've read.
    > Is this because of a potential buffer overflow?


    Probably better to read the some reason again
    and then ask again if you still have questions.

    > I have compiled properly and used strlen too
    > and I just wonder what is the need to return a strlen?
    >
    > Has anyone used quite abit anyway the strlen function?


    It's handy for allocations for strings.

    http://www.mindspring.com/~pfilandr/C/lists_and_files/string_sort.c

    tail = list_append(&head, tail, *ptr, strlen(*ptr) + 1);


    http://www.mindspring.com/~pfilandr/C/lists_and_files/list_lib.c

    list_type *list_append
    (list_type **head, list_type *tail, void *data, size_t size)
    {
    list_type *node;

    node = malloc(sizeof *node);
    if (node != NULL) {
    node -> next = NULL;
    node -> data = malloc(size);
    if (node -> data != NULL) {
    memcpy(node -> data, data, size);
    if (*head != NULL) {
    tail -> next = node;
    } else {
    *head = node;
    }
    } else {
    free(node);
    node = NULL;
    }
    }
    return node;
    }

    --
    pete
    pete, Mar 31, 2008
    #5
  6. Bill Cunningham

    Richard Guest

    santosh <> writes:

    > Bill Cunningham wrote:
    >
    >> Strncat is supposed to be better than strcat for some reason
    >> I've read.

    >
    > It does something different to what it's name would suggest. We have had
    > innumerable threads dealing with this topic. Just do a Google search.
    >
    >> Is this because of a potential buffer overflow?

    >
    > Well, it can be used with that aim, in which case it leaves an array of
    > char instead of a string in the destination.
    >
    >> I have compiled properly and used strlen too and I just wonder what is
    >> the need to return a strlen?
    >>
    >> Has anyone used quite abit anyway the strlen function?

    >
    > You are not making any sense. Are you asking why strlen exists? It
    > should be obvious, even to you. How else do you find the length of a
    > zero terminated string?


    "even to you"???? Jesus H Christ, Santosh, cut the guy some slack.
    Richard, Mar 31, 2008
    #6
  7. > Why in production code would someone want to know the length of a
    >string?


    Why would you want to check input for validity? In the case of strings,
    that often includes minimum and maximum length constraints.

    Why would you want to check if the next word (in a fixed-width font) will
    fit on the current line, or whether you must start a new one?

    Why would you want to check if there is going to be a buffer overflow
    before overflowing it? This is often related to putting the data
    input into a database (C does not define the term 'database' but you
    can build one using files).

    Why would you want to center a title on a page, in a fixed-width
    font? (Take the width of the page in characters, subtract the
    length of the title, divide by 2, round, and print that many spaces,
    then the message, then start a new line).
    Gordon Burditt, Mar 31, 2008
    #7
  8. Bill Cunningham

    Bartc Guest

    "santosh" <> wrote in message
    news:fspevv$2e1$...
    > Bill Cunningham wrote:
    >
    >>> You are not making any sense. Are you asking why strlen exists? It
    >>> should be obvious, even to you. How else do you find the length of a
    >>> zero terminated string?

    >>
    >> C must not count '\0' as being part of a string.

    >
    > It is a part of a C string. However it is not counted by strlen.
    >
    >> This is the simple code I tried,
    >>
    >> int main(void) {
    >> size_t t;
    >> char hello[]="hello world\n";
    >> t=strlen(hello);
    >> printf("%i",hello);

    >
    > The format for size_t is %zu. If your compiler does not support this
    > then the next best method is to use %lu and cast it's argument to
    > unsigned long.


    Printing the value of hello instead of t has a bigger effect than using the
    wrong format spec.

    --
    Bart
    Bartc, Mar 31, 2008
    #8
  9. > Printing the value of hello instead of t has a bigger effect than using
    > the wrong format spec.
    >
    > --
    > Bart


    Yes. I typo on my part.

    Bill
    Bill Cunningham, Mar 31, 2008
    #9
  10. Bill Cunningham

    Guest

    On Mar 31, 3:47 am, santosh <> wrote:
    > Bill Cunningham wrote:
    > > Strncat is supposed to be better than strcat for some reason
    > > I've read.

    >
    > It does something different to what it's name would suggest. We have had
    > innumerable threads dealing with this topic. Just do a Google search.
    >
    > > Is this because of a potential buffer overflow?

    >
    > Well, it can be used with that aim, in which case it leaves an array of
    > char instead of a string in the destination.
    >
    > > I have compiled properly and used strlen too and I just wonder what is
    > > the need to return a strlen?

    >
    > > Has anyone used quite abit anyway the strlen function?

    >
    > You are not making any sense. Are you asking why strlen exists? It
    > should be obvious, even to you. How else do you find the length of a
    > zero terminated string?


    Can you or someone else please explain why everyone is still replying
    to this "Bill Cunningham' troll?
    It's clear he is a troll. It's been noted countless times and he
    *never* replies to the posts that call him a troll.
    He always choses to take things out of context, to make
    uncomprehensible sentences and ambiguous statements.
    It's a troll. So.. why?
    , Mar 31, 2008
    #10
  11. Bill Cunningham

    santosh Guest

    Bartc wrote:

    >
    > "santosh" <> wrote in message
    > news:fspevv$2e1$...
    >> Bill Cunningham wrote:
    >>
    >>>> You are not making any sense. Are you asking why strlen exists? It
    >>>> should be obvious, even to you. How else do you find the length of
    >>>> a zero terminated string?
    >>>
    >>> C must not count '\0' as being part of a string.

    >>
    >> It is a part of a C string. However it is not counted by strlen.
    >>
    >>> This is the simple code I tried,
    >>>
    >>> int main(void) {
    >>> size_t t;
    >>> char hello[]="hello world\n";
    >>> t=strlen(hello);
    >>> printf("%i",hello);

    >>
    >> The format for size_t is %zu. If your compiler does not support this
    >> then the next best method is to use %lu and cast it's argument to
    >> unsigned long.

    >
    > Printing the value of hello instead of t has a bigger effect than
    > using the wrong format spec.


    Oops. Yes, another case of reading what I expected to read I suppose. In
    which case the output that the OP mentioned is wrong. I think this
    proves finally that the OP *is* a troll.
    santosh, Mar 31, 2008
    #11
  12. Bill Cunningham

    santosh Guest

    wrote:

    > On Mar 31, 3:47 am, santosh <> wrote:
    >> Bill Cunningham wrote:
    >> > Strncat is supposed to be better than strcat for some
    >> > reason I've read.

    >>
    >> It does something different to what it's name would suggest. We have
    >> had innumerable threads dealing with this topic. Just do a Google
    >> search.
    >>
    >> > Is this because of a potential buffer overflow?

    >>
    >> Well, it can be used with that aim, in which case it leaves an array
    >> of char instead of a string in the destination.
    >>
    >> > I have compiled properly and used strlen too and I just wonder what
    >> > is the need to return a strlen?

    >>
    >> > Has anyone used quite abit anyway the strlen function?

    >>
    >> You are not making any sense. Are you asking why strlen exists? It
    >> should be obvious, even to you. How else do you find the length of a
    >> zero terminated string?

    >
    > Can you or someone else please explain why everyone is still replying
    > to this "Bill Cunningham' troll?
    > It's clear he is a troll.


    I was doubtful for a while, but I believe you are right. In any case,
    even if he were not a troll, he seems to be unable to learn, so
    replying to him seems increasingly pointless.

    > It's been noted countless times and he
    > *never* replies to the posts that call him a troll.
    > He always choses to take things out of context, to make
    > uncomprehensible sentences and ambiguous statements.
    > It's a troll. So.. why?


    It was a case of "cutting him some slack" as Richard said. Bill has been
    cut more slack than probably anyone else, but personally I'm done with
    him.
    santosh, Mar 31, 2008
    #12

  13. > It's handy for allocations for strings.
    >
    > http://www.mindspring.com/~pfilandr/C/lists_and_files/string_sort.c
    >
    > tail = list_append(&head, tail, *ptr, strlen(*ptr) + 1);
    >
    >
    > http://www.mindspring.com/~pfilandr/C/lists_and_files/list_lib.c
    >
    > list_type *list_append
    > (list_type **head, list_type *tail, void *data, size_t size)
    > {
    > list_type *node;
    >
    > node = malloc(sizeof *node);
    > if (node != NULL) {
    > node -> next = NULL;
    > node -> data = malloc(size);
    > if (node -> data != NULL) {
    > memcpy(node -> data, data, size);
    > if (*head != NULL) {
    > tail -> next = node;
    > } else {
    > *head = node;
    > }
    > } else {
    > free(node);
    > node = NULL;
    > }
    > }
    > return node;
    > }
    >


    Thanks Pete. I have found a book called "Algorithms in C" that might be
    what I'm looking for.

    Bill
    Bill Cunningham, Mar 31, 2008
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JC

    strcpy and strcat problem

    JC, Sep 26, 2003, in forum: C Programming
    Replies:
    23
    Views:
    1,892
    Robert B. Clark
    Sep 29, 2003
  2. Matt
    Replies:
    82
    Views:
    1,768
    Douglas A. Gwyn
    Sep 29, 2004
  3. Vaddina Prakash Rao

    strncat copies junk values ....

    Vaddina Prakash Rao, Dec 20, 2004, in forum: C Programming
    Replies:
    11
    Views:
    636
  4. Vaddina Prakash Rao

    strncat copies junk values ....

    Vaddina Prakash Rao, Dec 21, 2004, in forum: C Programming
    Replies:
    4
    Views:
    362
    CBFalconer
    Dec 21, 2004
  5. Kelvin Moss

    strncat functionality

    Kelvin Moss, Jan 25, 2005, in forum: C Programming
    Replies:
    4
    Views:
    334
    Peter Nilsson
    Jan 25, 2005
Loading...

Share This Page