Initializing 'const LPCSTR'

Ian Pilcher ian.pilcher at home.com
Sat Apr 21 23:41:11 CDT 2001


The "single glyph name store" patch that I submitted recently defines:

    typedef struct
    {
	int		index;
	const LPCSTR	sz;
    } GLYPHNAME;

I intially couldn't figure out a way to initialize the sz element with-
out generating compiler warnings, so I defined a "non-constant" version
of the structure in glyphlist.c:

    typedef struct
    {
	int		index;
	LPSTR		sz;
    } _glyphname;

This works, but it would break in really ugly ways if anyone ever
changes the definition of GLYPHLIST without making a corresponding
change to _glyphlist.

I finally figured out how to use typecasts to suppress the compiler
warnings without the "non-constant" structure.  Conveniently, gcc -O2
nets out all the pointer junk, and generates the same assembly that a
simple assignment (which would generate a warning) does.

    Modified files:
	dlls/wineps: glyphlist.c

    Log message:
	Ian Pilcher
	Use typecasts to suppress compiler warnings
-- 
========================================================================
Ian Pilcher                                         ian.pilcher at home.com
========================================================================
-------------- next part --------------
diff -urN ../wine-20010420cvs/dlls/wineps/glyphlist.c ./dlls/wineps/glyphlist.c
--- ../wine-20010420cvs/dlls/wineps/glyphlist.c	Fri Apr 20 13:30:38 2001
+++ ./dlls/wineps/glyphlist.c	Sat Apr 21 00:53:29 2001
@@ -61,21 +61,12 @@
  *  Inserts a copy of the  glyph name into the list at the index, growing the
  *  list if necessary; returns index on success (-1 on failure)
  *
- *  _glyphname is a version of GLYPHNAME with non-constant members, so it can
- *  be initialized without generating compiler warnings
- *
  */
-typedef struct
-{
-    INT	    index;
-    LPSTR   sz;
-} _glyphname;
-
 inline INT GlyphListInsert(LPCSTR szName, INT index)
 {
-    _glyphname	*g;
+    GLYPHNAME	*g;
 
-    g = (_glyphname *)HeapAlloc(PSDRV_Heap, 0,
+    g = (GLYPHNAME *)HeapAlloc(PSDRV_Heap, 0,
 	    sizeof(GLYPHNAME) + strlen(szName) + 1);
     if (g == NULL)
     {
@@ -84,9 +75,14 @@
 	return -1;
     }
 
+    /*
+     *	All this typecasting prevents the compiler from complaining about g->sz
+     *	(const char *const) being modified
+     *
+     */
+    *((LPCSTR *)&(g->sz)) = (LPCSTR)(g + 1);
+    strcpy((LPSTR)(g->sz), szName);
     g->index = -1;
-    g->sz = (LPSTR)(g + 1);
-    strcpy(g->sz, szName);
 
     if (glyphListSize % GLYPHLIST_ALLOCSIZE == 0)	/* grow the list? */
     {
@@ -114,7 +110,7 @@
 		(glyphListSize - index) * sizeof(GLYPHNAME *));
     }
 
-    glyphList[index] = (GLYPHNAME *)g;
+    glyphList[index] = g;
     ++glyphListSize;
 
     TRACE("Added '%s' at glyphList[%i] (glyphListSize now %i)\n",



More information about the wine-patches mailing list