Unhandled exception While trying to change a value of a char pointer in C

0

I'm working on basic pointers program om VISUAL STUDIO 2013:

#include <stdio.h>
void try1(char *c);

int main()
{
    int stop;

    char * c = "rotem";

    try2(c);

    printf_s(" %s \n \n ", c);

    scanf_s("%d", &stop);
    return 0;
}

void try2(char *c)
{

    *c = (char)(*c + 1);

}

However, i get this messege:

Unhandled exception at 0x009F152A in Project32.exe: 0xC0000005: Access violation writing location 0x009F5860.

What should I do?

c++
c
visual-studio-2013
asked on Stack Overflow May 10, 2020 by Goe Gilber

1 Answer

0

"rotem" is a literal string, it is in read only memory, you cannot modify it

duplicate it, or use an array because in your case you do not need to increase its size replacing

char * c = "rotem";

by

 char c[] = "rotem";

Also replace

void try1(char *c);

by

void try2(char *c);
answered on Stack Overflow May 10, 2020 by bruno

User contributions licensed under CC BY-SA 3.0