473,222 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,222 software developers and data experts.

strncpy copying beyond max length

I am quite new to C++ and am haveing dificulty with the strncpy
function. I wrote this peice of code but the output I get from the
destination string after the copy is not what I expected, instead it
leaks into the memory space of string 1. Here is the output and the
code, Im hopeing someone can point me in the correct direction to solve
this problem:

const int MAX_STRING = 5;
char str1[] = "Hello, world!";
char str2[MAX_STRING+1]; // include room for null
strncpy(str2, str1, MAX_STRING);
cout << "String1: " << str1 << endl;
cout << "String2: " << str2 << endl;

output: Hello -w 3-wHello, world!

I was wanting to set string 2 to Hello but instead I got much more then
5 characters.

Jul 23 '05 #1
4 1373
at******@gmail.com wrote:

I was wanting to set string 2 to Hello but instead I got much more then
5 characters.


strncpy only copied five characters. There's a catch, though. Read the
documentation for strncpy carefully.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2
Thanks, I have located the documentation and read it carfully and I can
now see the problem is a null was not appended because the value was
shorter then the length of the string being copyed thus i must apend my
own terminateing null caracter.

Much apreciation for this, I will read the documentation first for now
on. My book stated that it apends the null for me and I guess I asumed
this to be corect.

Problem solved.

Jul 23 '05 #3
at******@gmail.com wrote:

My book stated that it apends the null for me and I guess I asumed
this to be corect.


Sounds like a good book to get rid of. <g>

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #4
If you really want to use C++ properly, you should generally learn to use
string objects instead of char arrays. All these problems you are having
with string length and null termination are handled in a completely
transparent fashion by the standard C++ string class. Sounds nice doesn't
it? 8*)

HTH,

Dave Moore
Jul 23 '05 #5

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

Similar topics

4
by: dbuser | last post by:
Dear experts: Hope you can point my error here. This function of mine receives an array of length 32 and then the for loop adds some scrambled characters at patterns. I think, I can not read the...
12
by: mZ | last post by:
I am a C programming beginner... I wonder, why strncpy(s, t, n) does not put '\0' at the end of the string. Because when I output the copied string, it output more than what I want, until I put...
9
by: fybar | last post by:
Hi, I am in the progress of writing a program that will read in the contents of a directory and then do some stuff to some of the files. So far I am getting the contents of the directory but I get...
27
by: Shagy | last post by:
Greetings, I've been trying to find an equivant c funtion to the c++ copy function. Description: copy(char *cstring, size_t count, size_t offset); Copies "count" characters from a C-style...
53
by: SK | last post by:
Hi I appreciate all of the feedback I have recieved. I am enjoying working on my program and I hope that it can be appreciated. I have my program compiling now and I am continuing to work out...
12
by: anars | last post by:
Hi, I have this struct initialized globally: struct riddle { char *text; .... } below, the function I call in main with the riddle struct as arg. is defined:
43
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy....
4
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char...
4
by: lurch132002 | last post by:
i am trying to create an array of structs to hold some information but whenever i get to the second element and try to strncpy it i get a segmenation fault. ive searched around for similar...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.