Jack said:
Thanks for the reply. So I changed the code to the following:
int main(){
char * myVar;
int size;
printf ("Enter the size of the array: ");
scanf("%d", &size);
myVar = new char[size];
myVar[0] = 'a';
printf ("%c\n",myVar[0]);
printf ("Enter value for the first cell: ");
scanf("%c", &myVar[0]);
printf ("%c",myVar[0]);
return 0;
}
The code still doesn't work since it doesn't stop to get the input
from user on the second scanf() function. But if I change the %c in
scanf("%c", &myVar[0]); to %s, it works fine. Any idea why this is?
Don't top post and quote unnecessarily please.
You should also do some error checking. See "scanf problem" posted in
this newsgroup for more info on that.
The reason for it not working is probably because (IIRC) the whitespace
isn't processed after the last scanf, so your "%c" reads in that
whitespace (probably a \n), "%s" skips whitespace till it gets to a
non-whitespace char and reads in from there. If you want that behavior,
use "%1s".
Hope this helps.
Adrian
--
_____________________________________________________________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under a Creative Commons /
\ Attribution-Share Alike 3.0 License /
\_______[
http://creativecommons.org/licenses/by-sa/3.0/]______/
\/_______[blog:_
http://adrians-musings.blogspot.com/]______\/