0

Where is the problem? When running, the application crashes...

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void test(char* x) {
    (*x)++;
}

int main() {
    char* x = "xD";
    test(x);
    puts(x);
    getch();
    return 0;
}

2 Answers2

2

You are Trying to modify a string literal that is stored in a read-only memory adress, because with char* x = "xD"; you declare a pointer to that kind of data. use this char x [] = "xD"; instead, that is NOT a pointer, is an array that you are allowed to modify because it is stored in the stack. or if you want to use a pointer you need to allocate memory for it.

sir psycho sexy
  • 780
  • 7
  • 19
1

it crashes in the line (*x)++; because x Points to a read only Memory due to the Definition char* x = "xD";.

Change it to char x[] = "xD";. so x is an Array and it´s values can be changed

mch
  • 9,424
  • 2
  • 28
  • 42