cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/09/18:47:42

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Doing wrong! But where?
Date: Mon, 09 Feb 1998 18:38:22 -0500
Organization: Two pounds of chaos and a pinch of salt.
Lines: 35
Message-ID: <34DF936E.255F@cs.com>
References: <34df149c DOT 11485161 AT news DOT hiway DOT fi>
NNTP-Posting-Host: ppp212.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019