Home › Forums › C Programming › How do I use char* variable type in C? › Reply To: How do I use char* variable type in C?
Hello
try this code.
1 2 3 4 5 6 7 | #include <stdio.h> void main(){ char* yourName; printf("Please enter your name: "); scanf("%s", yourName); printf("nWelcome %s, I was waiting for you!n", yourName); } |
As you have declared a pointer which points a character.
char* yourName;
Now you are getting a value from the user and trying to store it in pointer type variable. scanf() function takes the address of the variable i.e. pointer to that variable and store the input. As you have already declared the pointer and you code scanf(“%s”, &yourName); is trying to save that input value in the address of that pointer. Which is logically incorrect. Your code looks fine and no error but it has a logical mistake that is why it compiles fine but gives error at runtime. You code should look like scanf(“%s”, yourName);