strcat strncat and strlen

B

Bill Cunningham

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
 
S

santosh

Bill said:
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?
 
B

Bill Cunningham

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.
 
S

santosh

Bill said:
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.
 
P

pete

Bill said:
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;
}
 
R

Richard

santosh said:
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.


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


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.
 
G

Gordon Burditt

Why in production code would someone want to know the length of a

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).
 
B

Bartc

santosh said:
Bill said:
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.
 
V

vippstar

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.


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



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?
 
S

santosh

Bartc said:
santosh said:
Bill said:
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.
 
S

santosh

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.
 
B

Bill Cunningham

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top