0

I am trying to replace 'G' with 'R' but I am getting unhandled exception.

int main()
{
    char *pszStr1 = "EFGH";

    (++pszStr1)[1] = 'R';

    printf("%s", pszStr1);
    return 0;
}
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
debugger
  • 31
  • 3

6 Answers6

6

Your string lies in a read-only area.

Instead, do

int main()
{
    static char pszStr1arr[] = "EFGH";
    char *pszStr1 = pszStr1arr;

    (++pszStr1)[1] = 'R';
    printf("%s", pszStr1);
    return 0;
}
glglgl
  • 89,107
  • 13
  • 149
  • 217
  • 5
    Is there a reason you're using `static` here? – Timothy Jones Aug 12 '13 at 08:56
  • your above code is working fine. I got the idea of read only but if you replace (++pszStr1)[1] with (pszStr1) only it replaces the whole string with 'R'. In this case where is the idea of read-only – debugger Aug 12 '13 at 09:03
  • @debugger No the point is if you declares as `char *pszStr1 = "EFGH";` then `pszStr1` is pointer to a constant string literal Whereas suppose if you declares it as `char pszStr1[] = "EFGH";` then `pszStr1` is an array that can be modify. Notice in Glglgl's code `pszStr1` points to an array notice the declaration `char *pszStr1 = pszStr1arr;` – Grijesh Chauhan Aug 12 '13 at 09:09
  • @debugger If you replace `(++pszStr1)[1]` with `(pszStr1)` then the assignment becomes an assignment of the `int` value `'R'` to the pointer `pszStr1`. If your compiler does not warn about that, change your compiler (or learn how to enable warnings in the one you already have). – Pascal Cuoq Aug 12 '13 at 09:59
  • @TimothyJones In order to have the stuff not on the stack, but in the data segment. Not necessary, but nicer, IMHO. – glglgl Aug 12 '13 at 10:15
3
int main()
{
    char *pszStr1 = "EFGH";

    (++pszStr1)[1] = 'R';

    printf("%s", pszStr1);
    return 0;
}

In this code pszStr1is a string literal, which must be considered as const char *pszStr1 = "EFGH";. You're trying to modify a read-only memory space.

Timothy Jones
  • 21,495
  • 6
  • 60
  • 90
nouney
  • 4,363
  • 19
  • 31
1
char *pszStr1 = "EFGH";

Is a string literal, but here it is like you wrote : const char* pszStr1 = "EFGH";

You are trying to modify a read-only memory-space...

You can solve it simply by changing it into :

char pszStr1[] = "EFGH";
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
0

char *pszStr1 = "EFGH";

is kept on readonly section of the executable. Compiler internally allocates the string "EFGH" then makes pszStr1 point to it. So, you can't change it. Use array instead of char pointer, like below:

        char pszStr1[] = "EFGG";
rakib_
  • 136,911
  • 4
  • 20
  • 26
0

char *pszStr1 = "EFGH"; is a constant and it is undefined, if you change the content.

If you want to change the content, you have to take an array:

char arr[] = "EFGH";

and you are able to do this:

arr[2] = 'R';
Zagatho
  • 523
  • 1
  • 6
  • 22
0

String literals are const in modern C, so the declaration char *pszStr1 = "EFGH"; is wrong, it should be const char *pszStr1 = "EFGH";. In many operating systems they end up in a section in memory that is marked as readonly, and the OS enforces that you can't write to them. If you copy the string literal to a temporary string on the stack (or heap), the program works.

int main()
{
    char str[5];
    char *pszStr1=str;
    strcpy(pszStr1,"EFGH");

    (++pszStr1)[1] = 'R';

    printf("%s", pszStr1);
    return 0;
}
user2671945
  • 315
  • 1
  • 6