Further down on the page, we get to a description of str.
Pointer to an array of chars where the C string is stored.
Key word is "array". You don't have an array of chars, you have a pointer that doesn't point to anything so there's no place to store what the user enters. Try changing str to an array of chars in your program. We'll make it size 100 because it's a nice round number. So change line 4 to this...
As for line 6, you don't want to give a "%c" arguiment a char* parameter. Give it a char paramter. Or better yet, since you read in a string, make it a "%s".
Note that you're in trouble if the user types 100 or more characters in. The buffer will be overrun. Read this and consider fgets instead.
Do you want to say that, if i define pointer to a char,it doesn't reserve the memory for entering a string?? However, my problem being, the below code does take the string,prints it in the 6th line, but doesn't print the character at the adress. And Yes, now i also have memory issues,since we are not exactly telling in the code, the places to be reserved.
Do you want to say that, if i define pointer to a char,it doesn't reserve the memory for entering a string??
However, my problem being, the below code does take the string,prints it in the 6th line, but doesn't print the character at the adress.
Your problem is that invoking undefined behavior (eg. writing to memory you don't own) can do anything, including work as expected. That's why the whole "it works for me" argument is invalid when it comes to undefined behavior. Unfortunately, working as expected gives people a false sense of security in broken code, which is why you have to know what causes undefined behavior and avoid it.
#define HOW_MANY_CHARS 2
char *s = NULL;//does not point to allocated memmory
s = (char*) malloc (sizeof(char) * HOW_MANY_CHARS);//create nice old string
memset(s, 0, HOW_MANY_CHARS);//initialize it to zeroes
gets(s);//though better to use getchar and check for overflow
printf("[%s]\n",s);// print whole c string
printf("[%c]\n",s);//acces first char and print it