473,569 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strcat strncat and strlen

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
Mar 31 '08 #1
11 3372
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?

Mar 31 '08 #2
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",hel lo);
}
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.
Mar 31 '08 #3
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",hel lo);
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.

Mar 31 '08 #4
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/.../string_sort.c

tail = list_append(&he ad, tail, *ptr, strlen(*ptr) + 1);
http://www.mindspring.com/~pfilandr/...les/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
Mar 31 '08 #5
santosh <sa*********@gm ail.comwrites:
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.

Mar 31 '08 #6

"santosh" <sa*********@gm ail.comwrote in message
news:fs******** **@registered.m otzarella.org.. .
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",hel lo);

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
Mar 31 '08 #7
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
Mar 31 '08 #8
On Mar 31, 3:47 am, santosh <santosh....@gm ail.comwrote:
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
uncomprehensibl e sentences and ambiguous statements.
It's a troll. So.. why?
Mar 31 '08 #9
Bartc wrote:
>
"santosh" <sa*********@gm ail.comwrote in message
news:fs******** **@registered.m otzarella.org.. .
>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",hel lo);

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.

Mar 31 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4622
by: Ian Stanley | last post by:
Hi, Having not done any C programming for a while I am trying to get back into it by converting an old java assignment into C. The problem is I am getting a segmentation fault at runtime which I am having trouble fixing(program below) The idea of the program is to convert input(integers) into words eg: # 101 returns #one hundred one I...
9
12772
by: Pascal Damian | last post by:
I read somewhere that strcpy() is safer when dealing with malloc()-ed strings. Is that true? (Of course I know that both are unsafe). -- Pascal
81
7238
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
24
3119
by: diego.arias.vel | last post by:
Hi all I have certain problem when I'm doing this: void copy(char *filename) { char *log_file; log_file=filename; strcat(log_file,"-log.txt");
1
5068
by: frangac | last post by:
Hi All, I am new with c and would like some help. When reading a bin file the year is 06, the decimal value = 6 so I use the &rawtime to determine the year and then cat it with the array (xya) so that the result will printf 2006 and do this with the month as well as the day. How can I strcat the "strncpy(datum,(char *)(ctime...
1
2512
by: frangac | last post by:
Hi All, I am new with c and would like some help. When reading a bin file the year is 06, the decimal value = 6 so I use the &rawtime to determine the year and then cat it with the array (xya) so that the result will printf 2006 and do this with the month as well as the day. How can I strcat the "strncpy(datum,(char *)(ctime...
87
5064
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for the library, and other resources related to managed strings are available for download from the CERT web site at: ...
9
3062
by: Neal Barney | last post by:
I have a C program which runs on a device using a Zilog Z180 microprocessor. While it can address 1MB of RAM, it can only address 64KB at any given time. And of that only 16KB can be used for "stack and heap space". So I'm running in a very memory constricted environment. The program "speaks" a proprietary protocol which sends ASCII...
28
3874
by: Mahesh | last post by:
Hi, I am looking for efficient string cancatination c code. I did it using ptr but my code i think is not efficient. Please help. Thanks a lot
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.