On May 6, 1:21 am, Nick Keighley <nick_keighley_nos...@[EMAIL PROTECTED]
>
wrote:
> On 6 May, 03:33, "Michael.Z" <zhangqiuy...@[EMAIL PROTECTED]
> wrote:
>
> > Hi Flash Gordon:
> > The Table.c was required to be implemented as generic type.
>
> I'm not sure what a "generic type" is and I have been programming
> for quite a while.
>
> > If I am
> > right, Table is declared as void * in header file, the reason is that,
> > when used later on, it can be casted to any other type of pointers.
>
> It may be you trying to hide implementation details from users
> of your code. The outside world uses a void* and your
> code (the library) casts it to the right type before use.
> This is good. The internal data structure can change
> without causing a recompile of the user code.
>
> /* pippo.h */typedefvoid* Table_handle;
> Table_handle create();
> void befunge (Table_handle);
>
> /* pippo.c */
> void befunge (Table_handle handle)
> {
> Table* table = (Table*)handle;
> ...
>
> }
>
> /* user.c */
> int main (void)
> {
> Table_handle* my_table;
> my_table = create();
> befunge(my_table);
> return 0;
>
> }
>
> The point is I *don't* try and have two differing
> definitions of Table.
>
> > The implementation of Table in Table.c was defined as pointer to
> > struct Table, because I need to implement the members of Table.
>
> > My professor told us, void * in header indicates a generic type.
> > He has some sample codes where List wastypedef'd void pointer but the
> > definition was struct List Pointer:
>
> so he had two different types didn't he?
>
>
>
> > /* Header file List.h*/
> > #ifndef LIST_H
> > #define LIST_H
>
> > #include "bool.h"
>
> > typedefvoid * List ;
> > typedefvoid * DataL ;
> > typedefvoid * ListIt ;
>
> > List makeList ( int , void * (*copy)() , void (*free)() ) ;
> > void freeList ( List ) ;
> > void clearList ( List ) ;
>
> > boolean accessList ( List , DataL , int ) ;
> > int lengthList ( List ) ;
>
> > boolean accessHead ( List , DataL ) ;
> > boolean insertHead ( List , DataL ) ;
> > boolean deleteHead ( List , DataL ) ;
>
> > /* not all the header file is copied here*/
>
> > /* List.c */
>
> > #include "bool.h"
> > #include <stdio.h>
> > #include <stdlib.h>
>
> > #ifndef DATA
> > #define DATA
> >typedefvoid * Data ;
> > #endif
>
> >typedefstruct ListNode {
> > struct ListNode * next ; /* reference to following
ListNode */
> > struct ListNode * previous ; /* reference to preceding
ListNode */
> > Data dptr ;
>
> > } ListNode ;
>
> >typedefstruct List {
> > ListNode * head ; /* Head end of List
*/
> > ListNode * tail ; /* Tail end of List
*/
> > int _lengthList ; /* number of items within List
*/
> > int _sizeData ; /* byte size of data to be
stored */
> > void (*_freeData)( ) ; /* returns data item to heap
*/
> > void * (*_copyData)( ) ; /* copies data item to another
*/
>
> > } * List ;
>
> arg!!!
>
> don't top post. Please put your reply after the text you are
> replying to.
>
>
>
> > On May 3, 2:32 am, Flash Gordon <s...@[EMAIL PROTECTED]
> wrote:
>
> > > Michael.Z wrote, On 03/05/08 08:22:
>
> > > > Anyone who can help:
>
> > > > Given a Table.h file I am writing a Table.c file.
> > > > I keep getting the compile error:
>
> > > > previous declaration of Table was here / conflicting types for
>
> > > > I think the problem was the result of two pieces of code.
> > > > First:typedefstruct Table; /* in Table.c*/
>
> > > This says that Table is a struct.
>
> > > > Second: struct Table { /*struct definition */ } *Table; /* in
> > > > Table.h */
>
> > > This says it is a pointer to a struct, that is what the * means.
>
> > > > How can I solve the problems?
>
> > > By not providing different definitions.
>
> > > > Here are the two files
>
> > > <snip>
>
> > > >typedefvoid * Table ;
>
> > > This is not what you said you had and is defining table as yet
another type.
>
> > > <snip>
>
> > > >typedefstruct Table{
> > > > int sizeTable;
> > > > unsigned sizeData;
> > > > int (*diff)();
> > > > unsigned (*hash)();
> > > > void *(*copy)();
> > > > void (*free)();
> > > > } * Table;
>
> > > This is what you said. However pointers to void and pointers to
structs
> > > are different things for the simple reason that void and struct are
> > > different.
>
> > > Also hiding pointers behind atypedefis generally considered a bad
thing.
> > > --
> > > Flash Gordon-
>
> don't quote sigs (the bit after the "-- ")
>
Hi Nick,
Thanks so much for your reply and your advice for not top posting and
not quote signature, I am new to this, let me know if I need more
further improvements.
Regarding to your post:
>
> so he had two different types didn't he?
>
>
He did have two different types. Its "void *" in header file while
"struct List *" in List.c;
eg.
/*List.h*/
typedef void *List;
/*List.c*/
typedef struct List{....} *List;
However there was no conflicting type error, the reason is he did not
include file "List.h" in his "List.c".
So I removed #include "Table.h" from my "Table.c", it complied well.
Regards.
Michel Zhang


|