Re: Need some help with dereferencing structures and such

From: Thomas Matthews (Thomas_MatthewsSpitsOnSpamBots_at_sbcglobal.net)
Date: 02/02/04


Date: Mon, 02 Feb 2004 17:18:56 GMT

Andrew Falanga wrote:
> Hi there everybody,
>
> Wow, it's been a while since I've posted to this NG. Ok, now to the point.
>
> I'm rewriting my last class project to expand my understanding of C and will eventually re-write it in C++. There is a structure defined as:
>
> struct products {
> char UPC[13];
> char DESC[20];
> float PRICE;
> float DISC;
> char TAX;
> struct products* prev;
> struct products* next;
> };

BTW, in most programming styles, UPPERCASE_IDENTIFIERS are reserved
for constants and macros:
   const unsigned int MAXIMUM_PRODUCTS = 129;
   products bag[MAXIMUM_PRODUCTS];

>
> As you can tell, by the the pointer types in the structure definition, I'm trying to create a doublely linked list. Now, I have a function called accessData(). This function is what will be responsible for reading the products "database" from disk and creating the linked list.

In some instances, the pointer to the data is a void pointer:
struct Node
{
   void * p_data;
   Node * prev;
   Node * next;
};

Extract the linked list functionality from the product. Let the
client of the linked list be responsible for casting the data
pointer.

>
> The accessData function is prototyped liked this;
>
> int accessData(void* p, FILE* dataIn);
>
> I was trying to take advantage of the void pointer here. However, I'm aparently missing something because every time I try to compile my driver program (to see if I've coded the accessData function right) the compiler complains something to the effect of, "prev not a member of structure or union." It has this problem when encountering lines like this:
>
> p->prev = temp; // temp is a temp holder created in the function
>
> What's wrong? If more is needed, I'll see what I can do. The source code isn't actually on the computer I'm writing this message from.

When playing with pointers to void, you need to cast them before
deferencing them or almost any other operation:

unsigned int accessData(void * p, std::istream& inp)
{
   products * p_prod((products *) p);
   p_prod->PRICE = 14.5;
   return;
}

struct Cow
{
   const char * name;
   float mass;
};

int main(void)
{
   Cow Jessie;
   access_data(&Jessie, cin);
   return EXIT_FAILURE;
}

The above main() function demonstrates a problem with using
void pointers.

>
> Andy

-- 
Thomas Matthews
C++ newsgroup welcome message:
          http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq:   http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
          http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
     http://www.josuttis.com  -- C++ STL Library book


Relevant Pages

  • Re: Function Question
    ... > frame because variables are always global inside struct. ... > inside pointer to function list. ... Consider using a Function Object or Functor. ... C++ Faq: http://www.parashift.com/c++-faq-lite C Faq: http://www.eskimo.com/~scs/c-faq/top.html alt.comp.lang.learn.c-c++ faq: ...
    (comp.lang.cpp)
  • Re: [PATCH 0/2] x86: per-device dma_mapping_ops
    ... but I'd rather we handle the common case first. ... struct dma_mapping_ops pointer. ... Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)
  • Re: [PATCH] agpgart: Allow drm-populated agp memory types
    ... Just pass it a pointer to the struct it fills in anyway... ... More majordomo info at http://vger.kernel.org/majordomo-info.html ... Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)
  • Re: [PATCH 3/4] WorkStruct: Merge the pending bit into the wq_data pointer
    ... the wq_data pointer together. ... This shouldn't cause misalignment problems as ... struct list_head entry; ... Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)
  • Re: how to store list of varying types
    ... represent that with those in the struct definitions? ... pointer null, and the second one the CString object. ... then have to finish constructing the packet by copying the two data objects ... start with an upper-case letter (so, I would use DataElement ...
    (microsoft.public.vc.mfc)