Hi, I want to scan a word from the user and store it in a char pointer. But I couldn't do it. The thing I just want as the following:

char *word;
scanf("%s",word);

but, it doesn't work... please help for this simple problem. it sounds a bit noob, i know...

You cannot do it like that, 'word' is there a pointer to a char.
So, try with an array of chars instead

char word[21] = "";
scanf("%20s",word); // at max, 20 characters get scanned to the 'word'

You cannot do it like that, 'word' is there a pointer to a char.
So, try with an array of chars instead

char word[21] = "";
scanf("%20s",word); // at max, 20 characters get scanned to the 'word'

Thank you, I've changed the code as you described and it worked. I had wanted to use char pointer instead of this solution, as you said i think it's impossible for using scaning words and storing...

But now, I've a bigger problem, if you can help I'd really appreciate it:

http://www.daniweb.com/forums/thread118776.htm

The same thing using a pointer would be char *word = malloc( 21 * sizeof *word );

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.