PDA

View Full Version : Help me to look at this snippet



huangyingw
January 12th, 2009, 02:53 PM
Hello,
I am debugging this following snippet, it return me no result in GCC. It seems that the right value could not be passed to the left operand: s[dest - s - 1].
While it works if I change s[dest - s - 1] = c; expression to *dest++=*src++; 
================================================== ===============
#include <stdlib.h>
#include <stdio.h>

char *
myStrcpy (
char *dest,
const char *src
)
{
int c;
char * s = (char*)src;

do
{
c = *s++;
s[dest - s - 1] = c;
}
while (c != '\0');

return dest;
}

int main()
{
char* source="123456";
char* target=(char *)malloc(100 * sizeof(char));
myStrcpy(target,source);
printf("%s \n",target);
return 0;
}

Zugzwang
January 12th, 2009, 02:58 PM
So you already traced one execution of the program with a debugger?

huangyingw
January 12th, 2009, 03:11 PM
So you already traced one execution of the program with a debugger?

thank you for attention. Yes, I have debuged it. And it seems that the s[dest - s - 1] is not writable...

Zugzwang
January 12th, 2009, 05:04 PM
thank you for attention. Yes, I have debuged it. And it seems that the s[dest - s - 1] is not writable...

Great, now think about why that could be the case. Hint: Ask yourself what using an address in an expression fpr array index calculation would cause.

huangyingw
January 18th, 2009, 02:14 PM
Great, now think about why that could be the case. Hint: Ask yourself what using an address in an expression fpr array index calculation would cause.
Yes, thank you. I have found the root.
it should be:
s[dest - src - 1] = c; //instead of s[dest - s - 1] = c;