Code:
{
char *p;
char string;
printf("Type in a string: ");
string = getchar();
p=string;
printf("%s\n", string);
printf("%c\n", *p);
return 0;
}
How many threads do you want? Keep to one please. Also, the last code tag needs a forward slash to make it work (see my signature!)
Now to store a string you need an array of chars. Calling a variable string, does not make it one. "char" is a single byte, so this:
>char string;
is giving you variable called string, which is one byte long. Therefore is it not a string.
You need something like this:
>char string[100];
which will give you 99 bytes to store text, and 1 byte for the null terminator that ends the array, and makes it usable as a string.
At your level, I wouldn't bother using pointers (the *p in your code). Just get to grips with basic the concept of arrays first.
Here's one version (there are many ways) :
Code:
#include <stdio.h>
int main(void)
{
char string[100];
printf("Type in a string: ");
if ((fgets(string, 100, stdin)) != NULL)
{
printf("%s\n", string);
}
return 0;
}
Compare this, and research your books. Post again if you have any more questions.