What is wrong with this program
William T. McGarry
wtm at bunker.UUCP
Tue Aug 13 14:43:02 AEST 1985
< Why does printf insist that data->ut_line is a null string while
< putchar can display all of the characters of this array?
<
< Sorry if this is a trivial question, but it beats the hell
< out of me.
The problem with this program is the way in which "data" is defined
and used:
struct utmp *data;
This only defines that "data" is to be a pointer to a structure with
the format of "utmp". It does not allocate any space for the structure
itself. You have to either:
1. Do a malloc right after the open the file -
data = (struct utmp *) malloc(sizeof(struct utmp));
2. Since "utmp" is such a small structure, a malloc is problably not
even needed. Instead, write it like this:
struct utmp data;
which does allocate space for the structure. In this case, the "fread"
call should read into "&data" rather than "data" and all of the lines
such as "data->ut_line" should be changed to "data.ut_line"
and change all of the lines from "utmp->ut_line" to "utmp.ut_line"
and the "fread" call should read into "&data".
Both of these methods work fine on my Fortune 32:16.
Hope this helps!
Bill McGarry
decvax!ittatc!bunker!wtm
More information about the Comp.lang.c
mailing list