PREV INDEX NEXT

Author: Stan Eisenstat
Subject: Re: [Cs223] Trouble with createD in Deque.c
Date: Tuesday, 17 Mar 2020, 10:12:07


    > Message Posted By: Unknown
    ...
    > I am having two issues with the code in Deque.c so far. The first is a
    > compilation error that I know how to fix but don't understand why I need
    > to fix it. I am setting d, the argument input to createD, equal to a
    > malloc statement. Therefore d should be a pointer to a malloced block (for
    > a struct). Then I am trying to use the arrow notation, d->, to set the
    > malloced block's values. However, setting d-> equal to something gives me
    > the error: error: `*d' is a pointer; did you mean to use `->'?. My problem
    > is solved when I use (*d)-> instead. If d is pointing to the block
    > already, why do I need to dereference d twice?

The argument to createD() is a Deque* (i.e., a pointer
to a Deque) so that createD() can change the value of a
Deque variable in the calling function.  Since C is call
by value, the statement

  d = malloc (...);

resets the value of the local copy of that pointer, but
has no effect on the deque variable in the calling
function.  Thus the statement above should be

  *d = malloc (...);

d->FIELD is equivalent to (*d).FIELD.  Since d is a
Deque*, *d is a Deque (i.e., a pointer to a struct
deque), not a struct deque, and thus does not have any
fields, which generates the error message.

(*d)->FIELD is equivalent to (**d).FIELD.  Since **d
is a struct deque, there is no error message.
=====

    > Another issue I am having that isn't causing problems in my code yet but I
    > fear may later is the fact that the declarations of the structs are
    > confined to the brackets I declared them in. I am declaring two structs, a
    > deque struct and an elt struct. I would want the deque struct to include
    > pointers to pointers to elts, but compilation errors tell me elt is
    > unknown when I try to do that in the deque. For now, I have my deque
    > struct written as pointers to pointers to chars instead, but they are
    > actually pointing to pointers to elts so I am confused as to how to change
    > this.

The names of structs and new types (from typedef) are
lexically scoped, which means that they must be defined
before they are used.  The one exception is something
like

  typedef struct deque *Deque;

which does not need to know what a struct deque is since
all pointers to structs are the same size.

--Stan-
PREV INDEX NEXT