Mail Archives: djgpp/1998/02/09/18:47:42
JODE wrote:
>
> struct Man {
>
> char stick[1];
>
> }man1;
>
> fgets(man1.stick, 1, thefile1);
>
> printf("%C\n", man1.stick);
>
> The thefile1 first line contains only a single "Y" charracter!
> I just can't get it right! The printf prints something realy wierd!
> Where do I do wrong?
Use %s to print strings, not %c (note case). That is the major error
you have, but I must point out another - when you create the array in
your struct you are not allocating sufficient space to store the
terminating null character that is a part of all strings. You are lucky
in that gcc automatically packs structure members along 4 byte
boundaries, but on another compiler this just might cause seriously
undefined behavior.
It sounds like you're confused about how arrays and strings work in C.
I suggest reading a tutorial on the subject; there are lots of C
textbooks available, and plenty of documents on the net.
--
---------------------------------------------------------------------
| John M. Aldrich | "Always listen to experts. They'll |
| aka Fighteer I | tell you what can't be done, and why.|
| mailto:fighteer AT cs DOT com | Then do it." |
| http://www.cs.com/fighteer/| - Lazarus Long |
---------------------------------------------------------------------
- Raw text -