GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #21  
Old 01-Aug-2007, 08:09
aijazbaig1's Avatar
aijazbaig1 aijazbaig1 is offline
Member
 
Join Date: May 2006
Location: India
Posts: 174
aijazbaig1 has a spectacular aura aboutaijazbaig1 has a spectacular aura about
Exclamation

Re: Help analyzing a linked list program


Hello People. (PLS SEE THIS ONE BEFORE THE ONE ABOVE)
I made an incredible blunder in the post above. I forgot to precede List with the keyword struct. So now i have made some changes to the code above such that I have now declared a variable called mylist which is a structure of type List and declared another pointer variable called mylist_ptr which is of type pointer to List and made it point to the address of mylist
Heres the C equivalent:
CPP / C++ / C Code:
    struct List mylist;
    struct List *mylist_ptr;
    struct Listelmt *tmp_idx;

    mylist_ptr = &mylist;
And replaced all occurences of mylist in the function calls and the subsequent text to mylist_ptr. Doing that i get the following set of error messages:
Code:
gcc -std=c99 -Wall -W -pedantic -c list.c -o list.o gcc -std=c99 -Wall -W -pedantic -c testlist.c -o testlist.o testlist.c: In function `main': testlist.c:7: error: storage size of 'mylist' isn't known testlist.c:33: error: request for member `size' in something not a structure or union testlist.c:34: warning: passing arg 1 of `list_ins_nxt' from incompatible pointer type testlist.c:40: error: request for member `size' in something not a structure or union testlist.c:45: error: request for member `head' in something not a structure or union testlist.c:47: error: request for member `next' in something not a structure or union testlist.c:48: warning: passing arg 1 of `list_ins_nxt' from incompatible pointer type testlist.c:48: warning: passing arg 2 of `list_ins_nxt' from incompatible pointer type testlist.c:54: warning: passing arg 1 of `list_destroy' from incompatible pointer type testlist.c:59: error: request for member `size' in something not a structure or union testlist.c:65: error: request for member `size' in something not a structure or union testlist.c:72: error: request for member `head' in something not a structure or union testlist.c:74: error: request for member `next' in something not a structure or union testlist.c:75: warning: passing arg 1 of `list_rem_nxt' from incompatible pointer type testlist.c:75: warning: passing arg 2 of `list_rem_nxt' from incompatible pointer type testlist.c:7: warning: unused variable `mylist' make: *** [testlist.o] Error 1
I do not understand why it says that storage size of 'mylist' isn't known where as I have explicitly defined a struct by the name of mylist. If i remove this line and directly define a pointer to a struct of type List, this error disappears. Doesn't doing an explicit variable declaration automatically allocate space for the given variable?

And I don't get as to why it still says that mylist_ptr is NOT pointing to a variable of type structure or union.

Keen to hear from you guys
__________________
Hope to hear from you guys!

--------------------------------------------------

Best Regards,
Aijaz Baig.
  #22  
Old 01-Aug-2007, 10:05
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,148
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help analyzing a linked list program


Quote:
Originally Posted by aijazbaig1
CPP / C++ / C Code:
.
.
.
    List *mylist;
 .
.
.
                if (mylist.size == 0)
The most obvious question is ...

From a syntax point of view, mylist is a pointer, not a struct, so the compiler would accept mylist->size. It is telling you that you can't use mylist.size since mylist is not a struct.

However...
From a functionality point of view, since mylist is an unitialized pointer (its value is not the address of a List struct), dereferencing it would be undefined behavior. (So don't do it.)

Why not just declare
CPP / C++ / C Code:
    List mylist;

Now, the mylist.size refers to the element of the struct that you have declared.

However... All of the functions that take a pointer to struct as an argument will require you to give them the address of mylist:

CPP / C++ / C Code:
..
    list_init(&mylist, deallocatelist);
.
.
                list_ins_nxt(&mylist, NULL, &mydata);
.
.
.
                list_ins_nxt(&mylist, tmp_idx, &mydata);
.

Also note that you can't have a "void" data type. Period. Full Stop.

There may be a few other compiler errors to track down and destroy.

Regards,

Dave
  #23  
Old 01-Aug-2007, 10:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,148
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help analyzing a linked list program


Quote:
Originally Posted by aijazbaig1
I made an incredible blunder in the post above. I forgot to precede List with the keyword struct
But you had a typedef for List in your "list.h", right? So if you hadn't changed anything else or forgotten some things or something else for which I don't have a clue, it has to work just fine with declarations like the following:

CPP / C++ / C Code:
/* in mylist.h */
.
.

typedef struct Listelmt_ {
    void *data;
    struct Listelmt_ *next;
} Listelmt;

typedef struct List_ {
    int size;
    int (*match) (const void *key1, const void *key2);
    void (*destroy) (void *data);

    Listelmt *head;
    Listelmt *tail;
} List;
.
.
.

CPP / C++ / C Code:
/*in testlist.c */
.
.
#include "mylist.h"
.
.
.
    List mylist;                /* the typedef tells what kind of variable mylist is: a struct */
    List *mylist_ptr = &mylist; /* since it knows what List is, it knows what "List *" is      */
.
.
.
    list_init(mylist_ptr, deallocatelist);
.
.
.
            if (mylist.size == 0)
.
.
.

On the other hand, when you make the typedef for List, then you would not (not) make a declaration like]
CPP / C++ / C Code:
struct List mylist; /* this is an error for your typedef for List */

If you really like to type "struct" a lot, then you could use it with the struct tag:
CPP / C++ / C Code:
struct List_ mylist; /* legal, but why would you do it when you have a typedef */

Regards,

Dave
 
 

Recent GIDBlogCompress Your Web Site by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
linked list error message Krandygrl00 C++ Forum 4 22-Jun-2005 14:13
search linked list itsmecathys C++ Forum 20 18-Apr-2005 01:34

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:20.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.