Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: previous de...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 11 of 13 Topic 26048 of 26960
Post > Topic >>

Re: previous declaration of Table was here / conflicting types for

by Chris Torek <nospam@[EMAIL PROTECTED] > May 10, 2008 at 05:43 AM

In article
<0f2fcd7d-9fd0-47cd-8458-d517e7defc07@[EMAIL PROTECTED]
>
Michael.Zhang <zhangqiuyuan@[EMAIL PROTECTED]
> wrote:
[in a reply to Ben Bacarisse]
>I just want to make sure I got you right.You are suggesting:
>typedef struct Table{ ...} Table;
>Table * makeTable( void* size, void * data);

I can only speak for myself, not for Ben Bacarisse, but I rather
doubt that is what he is suggesting.

In the absence of additional details, here is how *I* might do
this.  Note that I prefer not to use "typedef" -- which does not
actually define types -- at all, but there are some "pro-typedef"
reasons.  See <http://web.torek.net/torek/c/types2.html>
for
details on this.

In this particular case, I might have a "table.h" file.  In table.h,
one would find, e.g., the following (assuming the table is indexed
by simple "row,column" integer-pairs):

    struct table;

    struct table *table_create(void);
    void *table_lookup(struct table *table, int row, int col);
    int table_insert(struct table *table, int row, int col, void *data);
    void *table_delete(struct table *table, int row, int col);
    void table_iterate(struct table *table, void *aux,
        void (*iter)(struct table *table, void *aux,
                     int row, int col, void *data));
    void table_destroy(struct table *table, int free_all_data);

Note that there is no "{" after "struct table".  This means that
the entire contents of a "struct table" are hidden.  So where are
they?  The answer is: in table.c (or perhaps in table-impl.h, which
is then #include-ed by table1.c and table2.c, if for some reason
"table.c" is not suitable to be used as a single file).

"Table clients", as it were, now have no idea what is *inside* a
table.  They only know that they can "create" one, do "lookup"
operations, do "insert" operations, do "delete" operations, ask to
"iterate" a function (of their choosing) over all table entries,
and "destroy" a table (with, in this case, optional automatic calls
to free() on each table item, so that they need not write a "freeing
iterator").

There is still quite a bit exposed here.  For instance, this makes
it clear that tables are indexed exclusively by integral (row,column)
pairs.  We must also decide whether "iterate" is called for "empty"
entries (e.g., if there is a row 7, but only a column 3 in row 7
-- even though other rows have columns 0 through 5 -- does the
user's iterator get called with NULL entries for columns 0, 1, 2,
4, and 5 on row 7?).  Nonetheless, the actual details of how the
tables are implemented are safely tucked away: clients know only
as much about tables as you choose to expose, while the "table
server" code (in table.c) sees all the details.

(Also, rather than "table", the above should probably called a
"sparse matrix" anyway.  "Tables" might actually instead be hash
tables that implement what one uses associative arrays for in
languages like awk, for instance.)
-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)      http://web.torek.net/torek/index.html
 




 13 Posts in Topic:
previous declaration of Table was here / conflicting types for
"Michael.Z" <  2008-05-03 00:22:21 
Re: previous declaration of Table was here / conflicting types
ade ishs <no-spam@[EMA  2008-05-03 19:34:39 
Re: previous declaration of Table was here / conflicting types
Flash Gordon <spam@[EM  2008-05-03 10:32:51 
Re: previous declaration of Table was here / conflicting types f
"Michael.Z" <  2008-05-05 19:33:26 
Re: previous declaration of Table was here / conflicting types f
Nick Keighley <nick_ke  2008-05-06 02:21:25 
Re: previous declaration of Table was here / conflicting types f
Ben Bacarisse <ben.use  2008-05-06 12:32:13 
Re: previous declaration of Table was here / conflicting types f
"Michael.Z" <  2008-05-07 01:05:08 
Re: previous declaration of Table was here / conflicting types f
Flash Gordon <spam@[EM  2008-05-07 19:14:36 
Re: previous declaration of Table was here / conflicting types f
CBFalconer <cbfalconer  2008-05-07 12:01:43 
Re: previous declaration of Table was here / conflicting types f
"Michael.Zhang"  2008-05-09 02:40:34 
Re: previous declaration of Table was here / conflicting types f
Chris Torek <nospam@[E  2008-05-10 05:43:46 
Re: previous declaration of Table was here / conflicting types f
Ben Bacarisse <ben.use  2008-05-10 01:19:38 
Re: previous declaration of Table was here / conflicting types f
"Michael.Zhang"  2008-05-10 20:11:23 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 16:31:19 CDT 2008.