Hi there,
Does anybody know why this code results in a run time error?
Code:char *code = "ASCII"; char* s; s = code; *(code+1) = *(s+1);
Hi there,
Does anybody know why this code results in a run time error?
Code:char *code = "ASCII"; char* s; s = code; *(code+1) = *(s+1);
This is what happens when you try to modify a string literal. If you are going to use a string literal this way, make sure the pointer type is const char * so that the compiler can help you spot mistakes with warnings.
Although I would also like to point out that *(code+1) = *(s+1); does nothing you'd see since these are two ways to reference the same character, anyway.
If you saw this in a book which suggests it should work, then you have a very old book.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.