473,394 Members | 1,971 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,394 software developers and data experts.

Copying from one char[] to another char[]

Hi,

I have a declaration like the following:

char tmp[8];
char tmp2[5];

strcpy (tmp, "58257619");
strcpy (tmp2, "");

Now, I'd like to copy the last five digits from tmp to
tmp2. I've tried doing a for loop, but there's always
garbage at the end of tmp2, and when the function returns,
it says that the memory near tmp was corrupted. Can
someone help me on how this should be done?

TIA

marcelo
Nov 16 '05 #1
6 2490
Get a beginner's book on C or C++ programming. It will explain how
character arrays and strings work.

char tmp[8] is not big enough for "58257619" because strcpy will add a NUL
character at the end, thereby overwriting memory which is not a part of tmp.

If you copy the last five characters of tmp into tmp2, there isn't any
garbage at the end of tmp2 - it is a character array of 5 characters.
HOWEVER, the debugger may display it as a string, which would require a
terminating NUL.

Regards,
Aaron Queenan.

"Marcelo" <sm***@hotmail.cilsai7er6rta93jcxa.com> wrote in message
news:1d****************************@phx.gbl...
Hi,

I have a declaration like the following:

char tmp[8];
char tmp2[5];

strcpy (tmp, "58257619");
strcpy (tmp2, "");

Now, I'd like to copy the last five digits from tmp to
tmp2. I've tried doing a for loop, but there's always
garbage at the end of tmp2, and when the function returns,
it says that the memory near tmp was corrupted. Can
someone help me on how this should be done?

TIA

marcelo

Nov 16 '05 #2
If you want to copy "58257619" into tmp then it should be declared as char
tmp[9] to accomodate for the terminating NULL
And similarly to copy 5 chars into tmp2 it needs to be declared as char
tmp2[6]

Expand|Select|Wrap|Line Numbers
  1. char tmp[9];
  2. char tmp2[6];
  3.  
  4. strcpy (tmp, "58257619");
  5. strcpy (tmp2, tmp + 3);
  6.  
  7. printf("%s\r\n",tmp2);
--
Regards,
Nish [VC++ MVP]

"Marcelo" <sm***@hotmail.cilsai7er6rta93jcxa.com> wrote in message
news:1d****************************@phx.gbl...
Hi,

I have a declaration like the following:

char tmp[8];
char tmp2[5];

strcpy (tmp, "58257619");
strcpy (tmp2, "");

Now, I'd like to copy the last five digits from tmp to
tmp2. I've tried doing a for loop, but there's always
garbage at the end of tmp2, and when the function returns,
it says that the memory near tmp was corrupted. Can
someone help me on how this should be done?

TIA

marcelo

Nov 16 '05 #3
If you want to copy "58257619" into tmp then it should be declared as char
tmp[9] to accomodate for the terminating NULL

And similarly to copy 5 chars into tmp2 it needs to be declared as char
tmp2[6]

Expand|Select|Wrap|Line Numbers
  1. char tmp[9];
  2. char tmp2[6];
  3.  
  4. strcpy (tmp, "58257619");
  5. strcpy (tmp2, tmp + 3);
  6.  
  7. printf("%s\r\n",tmp2);
--
Regards,
Nish [VC++ MVP]

"Marcelo" <sm***@hotmail.cilsai7er6rta93jcxa.com> wrote in message
news:1d****************************@phx.gbl...
Hi,

I have a declaration like the following:

char tmp[8];
char tmp2[5];

strcpy (tmp, "58257619");
strcpy (tmp2, "");

Now, I'd like to copy the last five digits from tmp to
tmp2. I've tried doing a for loop, but there's always
garbage at the end of tmp2, and when the function returns,
it says that the memory near tmp was corrupted. Can
someone help me on how this should be done?

TIA

marcelo

Nov 16 '05 #4
Marcelo wrote:
Hi,

I have a declaration like the following:

char tmp[8];
char tmp2[5];

strcpy (tmp, "58257619");
This copies 9 (yes 9) characters into tmp, which is only sized to hold 8 - 1
byte past temp is overwritten with a '\0'.
strcpy (tmp2, "");
This copies 1 (yes 1) character into tmp2.

Now, I'd like to copy the last five digits from tmp to
tmp2. I've tried doing a for loop, but there's always
garbage at the end of tmp2, and when the function returns,
it says that the memory near tmp was corrupted. Can
someone help me on how this should be done?


memcpy(tmp2,tmp+3,5);

Note though that this is only moving 5 characters, which means tmp2 does not
contain a trailing '\0' - it's an array of char, not a string. If you want
these arrays to be valid C-style "strings", you need to make each array 1
character larger in order to provide space for the trailing '\0', and then
after the memcpy, set the last element of tmp2 to '\0'.

A better idea, IMO, is to abandon the use of character arrays for such
manipulations and use a string type:

#include <string>

std::string tmp("58257619");
std::string tmp2(tmp.begin()+tmp.length()-5,tmp.end());

HTH

-cd


Nov 16 '05 #5
Thanks a lot, Nish!
-----Original Message-----
If you want to copy "58257619" into tmp then it should be declared as chartmp[9] to accomodate for the terminating NULL
And similarly to copy 5 chars into tmp2 it needs to be declared as chartmp2[6]

Expand|Select|Wrap|Line Numbers
  1. char tmp[9];
  2. char tmp2[6];
  3. strcpy (tmp, "58257619");
  4. strcpy (tmp2, tmp + 3);
  5. printf("%s\r\n",tmp2);

--
Regards,
Nish [VC++ MVP]

"Marcelo" <sm***@hotmail.cilsai7er6rta93jcxa.com> wrote in messagenews:1d****************************@phx.gbl...
Hi,

I have a declaration like the following:

char tmp[8];
char tmp2[5];

strcpy (tmp, "58257619");
strcpy (tmp2, "");

Now, I'd like to copy the last five digits from tmp to
tmp2. I've tried doing a for loop, but there's always
garbage at the end of tmp2, and when the function returns, it says that the memory near tmp was corrupted. Can
someone help me on how this should be done?

TIA

marcelo

.

Nov 16 '05 #6
Thanks a lot, Carl!
-----Original Message-----
Marcelo wrote:
Hi,

I have a declaration like the following:

char tmp[8];
char tmp2[5];

strcpy (tmp, "58257619");
This copies 9 (yes 9) characters into tmp, which is only

sized to hold 8 - 1byte past temp is overwritten with a '\0'.
strcpy (tmp2, "");
This copies 1 (yes 1) character into tmp2.

Now, I'd like to copy the last five digits from tmp to
tmp2. I've tried doing a for loop, but there's always
garbage at the end of tmp2, and when the function returns, it says that the memory near tmp was corrupted. Can
someone help me on how this should be done?


memcpy(tmp2,tmp+3,5);

Note though that this is only moving 5 characters, which

means tmp2 does notcontain a trailing '\0' - it's an array of char, not a string. If you wantthese arrays to be valid C-style "strings", you need to make each array 1character larger in order to provide space for the trailing '\0', and thenafter the memcpy, set the last element of tmp2 to '\0'.

A better idea, IMO, is to abandon the use of character arrays for suchmanipulations and use a string type:

#include <string>

std::string tmp("58257619");
std::string tmp2(tmp.begin()+tmp.length()-5,tmp.end());

HTH

-cd


.

Nov 16 '05 #7

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

Similar topics

15
by: curium | last post by:
Hi I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i need to copy bits 0-6 from another byte into bits 7-14 of this word. I am going gray from messing around with AND, OR, >>...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
12
by: Gactimus | last post by:
I wrote a C++ program that is supposed to copy one text file to another. It works fine if I specify the files to be opened and copied but when I try to have the file names inputted by the user it...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
4
by: Lafer | last post by:
Hello, I am attempting to write a terminal interface program using PowerPC Assembly/C, where I am using an integer-to-ASCII conversion algorithm. Unfortunately, I have run into some difficulties...
10
by: Cocy | last post by:
Hi, This might be a sort of FAQ, but I don't see why, so I would someone help me to understand what's wrong? I've just created following code which wold trim white space(s) in a (given) string....
21
by: hermes_917 | last post by:
I want to use memcpy to copy the contents of one struct to another which is a superset of the original struct (the second struct has extra members at the end). I wrote a small program to test...
3
by: Giox | last post by:
Hello everybody I want to create a routine that mimic the fread behavior operating on char array. I wrote size_t fread_char(void* buf, size_t sz, size_t n, char* string_source) { ...
9
by: Mr John FO Evans | last post by:
Am looking at adding structures to an embedded C emulator but am wondering what the standard is for copying structures(ie structa=structb) which contain pointers to memory and which are therefore...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.