Because you are trying to play a pointer as the value of the variable. The literal string determined by quotation marks record these characters in memory and generate a pointer that can be played in a variable. Note that the pointer itself must be assigned to the variable directly. What was used is to place the pointer as the value of the pointer pointed by the variable. Read that line as "in string pointing assign the EFGH pointer". Then just take out the pointing operator:
#include <stdio.h>
int main(void) {
char *string = "ABCD";
string = "EFGH";
printf("%s", string);
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
In the variable declaration the asterisk is not an operator, it is part of the declaration, so char *
means that the variable type will be "pointer to char".
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero