scanf( "%s" ) question

Alan McKay 840445m at aucs.uucp
Thu Mar 1 05:38:15 AEST 1990


eat this you nasty line-eater!!
------------------------------------------

I actually have 2 questions both of which are related.  This first is
just a matter of syntax ... I want to declare an array of 20 strings
of length 5, and I am doing the following:

char array[20][5];       /* is this correct? */

Ok, that is the easy one.  Now, each of these 20 strings gets read from
the keyboard and I am using 'scanf' to do it.  However, I want to ensure
that only the first 4 characters get read into the string ( saving the
last space for \0, of course ).  I am not sure how to do this.  Is there
some way I can specify that in my format statement?  I tried "%4s" but
that does not seem to do it.  

scanf( "%4s", array[0] );     /* to read in the first string */

What is happening now is that if more characters are entered they end
up over-writing array[1] .. [2] ... etc depending upon how man are
entered.  

I am using Turbo-C (what version??) but it is absolutely mandatory that
the solution be totally portable since I will be re-compiling on a number
of other systems, and running the program there as well.

Oh, and finally, I do not read this group so could you please reply 
via e-mail rather than a follow-up?  Thank you very much.



More information about the Comp.lang.c mailing list