strncat functionality

K

Kelvin Moss

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
 
R

Ravi Uday

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

Peter Nilsson

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

pete

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

Peter Nilsson

pete said:
Peter said:
Kelvin said:
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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top