Re: Need some help with dereferencing structures and such
From: Thomas Matthews (Thomas_MatthewsSpitsOnSpamBots_at_sbcglobal.net)
Date: 02/02/04
- Next message: Bill Reed: "Re: Splitting mixed character/number input into seperate variables"
- Previous message: nospam_at_nospam.com: "Splitting mixed character/number input into seperate variables"
- Next in thread: Andrew Falanga: "Re: Need some help with dereferencing structures and such"
- Maybe reply: Andrew Falanga: "Re: Need some help with dereferencing structures and such"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Bill Reed: "Re: Splitting mixed character/number input into seperate variables"
- Previous message: nospam_at_nospam.com: "Splitting mixed character/number input into seperate variables"
- Next in thread: Andrew Falanga: "Re: Need some help with dereferencing structures and such"
- Maybe reply: Andrew Falanga: "Re: Need some help with dereferencing structures and such"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|