comp.lang.c
[Top] [All Lists]

Re: strcat strncat and strlen

Subject: Re: strcat strncat and strlen
From: pete
Date: Sun, 30 Mar 2008 20:57:05 -0500
Newsgroups: comp.lang.c

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);


                                www.mindspring.com/~pfilandr/C/lists_and_files/list_lib.c">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

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy