473,503 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strncat functionality

Hi group,

Am I guaranteed a null terminated string after using strncat if my
destination buffer is big enough for n + 1 characters ?

....
strncat(dest, source, n);
Is dest[n] == 0 after above statement (assuming dest[n] doesn't cause
any buffer overrun)

Thanks,
Kelvin

Nov 14 '05 #1
4 2171


Kelvin Moss wrote:
Hi group,

Am I guaranteed a null terminated string after using strncat if my
destination buffer is big enough for n + 1 characters ?

...
strncat(dest, source, n);
Is dest[n] == 0 after above statement (assuming dest[n] doesn't cause
any buffer overrun)
Yes it does.

4.11.3.2 The strncat function

Synopsis

#include <string.h>
char *strncat(char *s1, const char *s2, size_t n);

Description

The strncat function appends not more than n characters (a null
character and characters that follow it are not appended) from the
array pointed to by s2 to the end of the string pointed to by s1 .
The initial character of s2 overwrites the null character at the end
of s1 . A terminating null character is *always* appended to the
result./121/ If copying takes place between objects that overlap, the
behavior is undefined.

Returns

The strncat function returns the value of s1 .

Thanks,
Kelvin


Nov 14 '05 #2
Kelvin Moss wrote:

Am I guaranteed a null terminated string after using strncat
if my destination buffer is big enough for n + 1 characters?

...
strncat(dest, source, n);
Is dest[n] == 0 after above statement (assuming dest[n]
doesn't cause any buffer overrun)


Yes. Note that, in a sense, strncat over teriminates the
destination if there is more room than required. Consider...

char dest[10];
strncpy(dest, "hello", sizeof dest);

Not only will dest[5] be a null byte, but so will dest[6]
through dest[9].

The strncat function wasn't designed for concatenation, it
was designed for old style fix width data fields where
excess bytes need to be zero-ed.

--
Peter

Nov 14 '05 #3
Peter Nilsson wrote:

Kelvin Moss wrote:

Am I guaranteed a null terminated string after using strncat
if my destination buffer is big enough for n + 1 characters?

...
strncat(dest, source, n);
Is dest[n] == 0 after above statement (assuming dest[n]
doesn't cause any buffer overrun)


Yes. Note that, in a sense, strncat over teriminates the
destination if there is more room than required. Consider...


You are confusing strncat with strncpy.
A strncat call doesn't write more than one null byte.

--
pete
Nov 14 '05 #4
pete wrote:
Peter Nilsson wrote:
Kelvin Moss wrote:

Am I guaranteed a null terminated string after using strncat
if my destination buffer is big enough for n + 1 characters?

...
strncat(dest, source, n);
Is dest[n] == 0 after above statement (assuming dest[n]
doesn't cause any buffer overrun)


Yes. Note that, in a sense, strncat over teriminates the
destination if there is more room than required. Consider...


You are confusing strncat with strncpy.
A strncat call doesn't write more than one null byte.

Quite correct. My bad.

--
Peter

Nov 14 '05 #5

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

Similar topics

2
2040
by: Vikas | last post by:
Hi y'all, I need logging functionality to provide me following: - logging levels that can be set at run-time. - able to output to different files for different levels - thread safe - line,...
2
382
by: Tom | last post by:
Hello, I'm looking for a tool that would allow me to create a web page with the spreadsheet like functionality. Basically, I want to be able to type in a number in the cell and have all totals...
25
7527
by: KK | last post by:
Hi, I am using history.go(-1) for implementing the back button functionality. Its working fine but with this exception. 1. The page which is having back button has some hyperlinks on it. ...
0
1258
by: blongmire | last post by:
OS: WinXP Home Access 2000 Word 2000 I loaded 1998 vintage 16-bit business mapping software onto a PC and I think it overlayed some OS files dealing with DDE functionality. After that software...
11
385
by: Vaddina Prakash Rao | last post by:
Good morning everyone, Can someone comment what is wrong with these statements .. #include <stdio.h> #include <stdlib.h> #include <string.h> main() { char *final, *s="abcdefghij"; int count...
0
1834
by: rellik | last post by:
Hi All, I've run into a problem with the MenuStrip control and any help would be greatly appreciated! The problem I've got is that when use a control derived from the MenuStrip class all MDI...
1
1090
by: Srini | last post by:
How do I do a functionality for a ASP.NET web application? Is there an automated way to do this? Can ACT do functionality test apart from the stress tests? When I make some change to the...
1
1224
by: Monty | last post by:
I know this isn't necessarily an ASP.Net question, but it is web programming and I intend to try it using ASP.Net, I just need to get pointed in the right direction. I've seen some sites that allow...
11
3355
by: Bill Cunningham | last post by:
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...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
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...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
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...
0
7460
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...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...

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.