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 6 2456
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
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] - char tmp[9];
-
char tmp2[6];
-
-
strcpy (tmp, "58257619");
-
strcpy (tmp2, tmp + 3);
-
-
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
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] - char tmp[9];
-
char tmp2[6];
-
-
strcpy (tmp, "58257619");
-
strcpy (tmp2, tmp + 3);
-
-
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
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
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]
- char tmp[9];
- char tmp2[6];
- strcpy (tmp, "58257619");
- strcpy (tmp2, tmp + 3);
- 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
.
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
. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
15 posts
views
Thread by curium |
last post: by
|
8 posts
views
Thread by Bryan Parkoff |
last post: by
|
12 posts
views
Thread by Gactimus |
last post: by
|
5 posts
views
Thread by Roy Hills |
last post: by
|
4 posts
views
Thread by Lafer |
last post: by
|
10 posts
views
Thread by Cocy |
last post: by
|
21 posts
views
Thread by hermes_917 |
last post: by
|
3 posts
views
Thread by Giox |
last post: by
|
9 posts
views
Thread by Mr John FO Evans |
last post: by
| | | | | | | | | | |