Copying from one char[] to another char[]

  • Thread starter Thread starter Marcelo
  • Start date Start date
M

Marcelo

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
 
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.
 
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]

Code:
char tmp[9];
char tmp2[6];

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

printf("%s\r\n",tmp2);
 
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]

Code:
char tmp[9];
char tmp2[6];

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

printf("%s\r\n",tmp2);
 
Marcelo said:
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
 
Thanks a lot, Carl!
-----Original Message-----
Marcelo said:
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




.
 
Thanks a lot, Nish!
-----Original Message-----
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]

Code:
char tmp[9];
char tmp2[6];

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

printf("%s\r\n",tmp2);

--
Regards,
Nish [VC++ MVP]



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


.
 
Back
Top