On 9 Nov 2003 07:52:59 -0800
jr********@hotmail.com (Matt) wrote:
I am writing a function to reverse a string. It compiles fine, but
when I run it under VS6.0, it says "The instruction at 0X004011
reference memory at 0x0042e00. The memory could not be written."
Any ideas??
void reverseString(char* s)
{ char temp;
char* s1 = new char[strlen(s) + 1];
^^^
new is not part of the C language. comp.lang.c++ is just down the hall
on the right.
for (int i=0; i<strlen(s)/2; i++)
{ temp = s[i];
s[strlen(s)-1-i] = temp;
s1[i] = s[strlen(s)-1-i];
s1[strlen(s)-1-i] = temp;
}
strcpy(s, s1);
}
I would assume you have to do something to get rid of s1 here or you
have a memory leak. However, you don't really need s1 at all so you
could just delete all the lines referring to s1 and tweak your code
slightly.
First decide if you are using C++ or C then ask only in the correct
group for help.
Also provide a small complete program (including the definition of main
you are using to test the function) and we (or the nice folks in
clc++) will stand a better chance of helping you.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.