Re: char * vs. const char *




Dear Hari!

> The confusion here is that typedef is not textual substition.  `const
> gpointer' is not `const void *', it is `void *const'.  Look at it this
> way: `t' is a type, and `const t' is its const type.  If a variable x is
> of type `t', you can change x.  If a variable y is of type `const t',
> you cannot change y.  That is all: no special magic for pointer types
> that says "The programmer meant that the const applies to the pointed-to
> type.  I will break my rules and do what I think he means".  Now, plug
> in `gpointer' for `t' and see how the rules affect it.
> 
> The correct workaround is to get into `glib' and declare a new type:
> 
> 	typedef const void *gcpointer;
> 
> or something more mnemonic, and use `gcpointer' wherever you want to
> assure the pointed to stuff will not be modified through that pointer.
>

Thanks for this C lecture.

Seems to me that in glib the const is used other than intended:

g_hash_table_lookup (GHashTable     *hash_table,
		     const gpointer  key)
gint  g_str_equal (const gpointer v,
		   const gpointer v2);
guint g_str_hash  (const gpointer v);

I will send a patch to the gtk guys. 

Robert



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]