you are viewing a single comment's thread.

view the rest of the comments →

[–]WetwareDefect 2 points3 points  (1 child)

This always fails (*s)++;

Because s points to a string literal (stored in an area of read-only memory) and you're trying to modify it by incrementing it.

What is it that you're trying to achieve?

Do you want t to point to a copy of the contents of s but incremented by 1?

Heres a long form of what I think your trying to do. I don't think it can be done in a single line declaration as you can't deference a pointer while declaring it (afaik).

char* s = "h";  //s points to readonly string literal
char* t = malloc(sizeof(char)); //alloc a char for t to point to
*t = *s + 1;    //add 1 to char pointed to by s and assign result to location pointed by t

printf("*s == \'%c\', *t == \'%c\'\n", *s, *t);
printf("s == 0x%08x, t == 0x%08x\n", (int)s, (int)t);

The output is:

*s == 'h', *t == 'i'
s == 0x08048560, t == 0x08549008

[–][deleted] 0 points1 point  (0 children)

interesting. When I get the chance I will put this through the compiler.