c - Header file help & structures -
i have header file, dimensions.h
, in it:
#ifndef dim_h #define dim_h typedef struct dimensions { int width; int height; } dim; dim * getheader (file * fp); #endif
from here have c file has this:
#include "dimensions.h" . . . dim * getheader (file * fp) { char s[1024]; struct dimensions * d = (struct dimensions *)malloc(sizeof(dimensions)); if (fgets(s, sizeof(s), fp) != null) { printf("%s", s); d->width = atoi(strtok(s, " \n\0")); d->height = atoi(strtok(null, " \n\0")); } return d; }
but when run errors dimensions undeclared
. played around code changing dimensions
dim
(still not understanding 2 name 'scheme'.
struct dim * d = (struct dim *)malloc(sizeof(dim));
then errors d->width
, d->height
saying it's dereferencing pointers incomplete types, swapped ->
.
, error request member width/height in not structure or union
.
so not sure what's going on, might simple i'm missing if that'd awesome.
thanks!
part 1: dimensions undeclared
see line in original code?
struct dimensions * d = (struct dimensions *)malloc(sizeof(dimensions));
if use struct dimensions
somewhere, use everywhere. change sizeof
to
sizeof(struct dimensions)
and dimensions undeclared
should go away.
part 2: dim
version
your code:
struct dim * d = (struct dim *)malloc(sizeof(dim));
when typedef struct {...} dim
, need refer dim
- not struct dim
.
dim * d = (dim *)malloc(sizeof(dim));
part 3: swap ->
.
use ->
when left side pointer structure, , .
when left side structure.
tl;dr
struct dimensions * d = (struct dimensions *)malloc(sizeof(struct dimensions));
should fix code.
Comments
Post a Comment