Re: char * vs. const char *



Robert Wilhelm <robert@physiol.med.tu-muenchen.de> writes:
> On Tue, Mar 03, 1998 at 01:46:53PM -0600, Federico Mena wrote:
> > >  Would it make sense to chage this to 
> > >  gnome_pixmap_new_from_file (const char *filename) ?
> > >  Note that quite a few function iprototypes down to imlib would need to be changed.
> > 
> > I wouldn't mind such a change.  If you think it is useful, feel free
> > to do it.
> I have appended a patch. It would be nice if someone with cvs write access
> could check it in.
> 
> Note that there are two new warnings are introduced with this patch:
>  
> gcc -DHAVE_CONFIG_H -I. -I. -I.. -I.. -I./.. -I/usr/local/include -I/usr/X11R6/include -g -O2 -c -fPIC -DPIC gnome-pixmap.c
> gnome-pixmap.c: In function `load_file':
> gnome-pixmap.c:344: warning: passing arg 1 of `gdk_imlib_load_image' discards `const' from pointer target type
> gnome-pixmap.c: In function `load_pixmap':
> gnome-pixmap.c:494: warning: passing arg 2 of `g_hash_table_lookup' discards `const' from pointer target type
> 
> I will make a smilar patch to imlib soon to fix the first warning.
> The second warning looks like a gcc flaw, because g_hash_table_lookup() takes
> a const gpointer.
> 
> I have made a small test case. Maybe some C guru can comment on it.
> snoopy$ cat  g.c
> 
> typedef void* gpointer;
> 
> gpointer    g    (const gpointer);
> gpointer    g2   (const void *);
> 
> void f(const char *file)
> {
>         gpointer pit;
> 
>         pit = g ( file );
>         pit = g2( file );
> }
> snoopy$ gcc -c  g.c
> g.c: In function `f':
> g.c:12: warning: passing arg 1 of `g' discards `const' from pointer target type

Nope `gcc' is correct.  Try adding the lines

	gpointer g3 (void *const);

	pit = g3( file );

It gives the same error message.

	const void *x  ==> Pointer to something that cannot be changed
			  (thru this pointer)
        void *const x  ==> Const pointer to something
			  (Pointer cannot be changed, but pointed-to
			   can be changed via this pointer)

Converting const char* to void *const is dangerous, and `gcc' is right
in warning this.  Please excise this portion of the patch before
applying it.

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.

- Hari
-- 
Raja R Harinath ------------------------------ harinath@cs.umn.edu
"When all else fails, read the instructions."      -- Cahn's Axiom
"Our policy is, when in doubt, do the right thing."   -- Roy L Ash



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