[comp.sys.amiga.tech] C pointer question.

wade@pnet01.cts.com (Wade Bickel) (11/13/89)

 
        I'm writing a set of memory management routines and have run across
a dereferencing of pointers I cannot seem to express.
 
        In the routines in question, a header is prefixed to allocations and
doubly linked lists are maintained.  The list head pointer is passed by
reference, and I'd like to modify something that it is pointing at.  It
looks something like this,
 
void *ListAlloc(list,size)
  struct AllocList **list;
  ULONG size;
  {
    ...
    ...
    (*(*list)).prev = something;   /* ???   gives "invalid structure
                                               reference" error
                                   */
  }

 
 
        I've tried all the variations of "*" and "->" I can think of but none
seem to work.  I'm using Lattice 5.04.
 
        For the meantime I've seperated the dereferencing into two steps, but
since it is only done once per routine this seems a waste.  Any help would be
greatly appreciated as I've been running into this problem repeatedly, and
in a few cases what should fit on one line with no temporary variables has
ended up taking four lines and several temps.
 
                                                        Thanks,
 
                                                             Wade W. Bickel

UUCP: {nosc ucsd hplabs!hp-sdd}!crash!pnet01!wade
ARPA: crash!pnet01!wade@nosc.mil
INET: wade@pnet01.cts.com

cmcmanis%pepper@Sun.COM (Chuck McManis) (11/16/89)

In article <698@crash.cts.com> wade@pnet01.cts.com (Wade Bickel) writes:
>        I'm writing a set of memory management routines and have run across
>a dereferencing of pointers I cannot seem to express.

Pointers are such fun aren't they ?

>void *ListAlloc(list,size)
>  struct AllocList **list;
>  ULONG size;
>  {
>    (*(*list)).prev = something;   
>  }
>        I've tried all the variations of "*" and "->" I can think of but none
>seem to work.  I'm using Lattice 5.04.
 
Well, assuming that AllocList is defined (so that the compiler knows what the
tags are in the structure) you want to use :
	*list->prev = <expr>;

Note that the precedence of '*' (dereference pointer) is higher than '->'
(dereference and offset) and so *list is the pointer to an AllocList structure
and the -> operator will dereference the tag 'prev'. If you are using 
doubly linked lists you might want to look in the RKM for the Exec list
structures. They provide one of the best list management packages I've seen
in a while. 

>        For the meantime I've seperated the dereferencing into two steps, but
>since it is only done once per routine this seems a waste.  

Well the compiler will probably code it into two steps anyway so it may or
may not be a waste. 


--Chuck McManis
uucp: {anywhere}!sun!cmcmanis   BIX: cmcmanis  ARPAnet: cmcmanis@Eng.Sun.COM
These opinions are my own and no one elses, but you knew that didn't you.
"If it didn't have bones in it, it wouldn't be crunchy now would it?!"