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 2489
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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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, >>...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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)
{
...
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |