tags:

views:

95

answers:

3

Hi, looking at the declaration for XDrawString from X11, it is

int XDrawString(Display *display, Drawable d, GC gc,
                int x, int y, char *string, int length);

How come the 6th argument is type "char *" instead of "const char *"? Does drawing the string require modifying it? I've seen lots of examples where people pass in constant strings; is that unsafe?

+5  A: 

Either because the function has been around since before const or because those who designed the API have troubles keeping up.

avakar
+1  A: 

I've never seen anything to suggest that XDrawString modifies the argument passed to it. Those low-level X APIs are very old, dating to the mid-80s. It's very likely people were a bit less rigorous in marking constant arguments as such back then.

Ken Keenan
+2  A: 

On both of my systems (Mac OS X v10.4.11 and Ubuntu with libx11-dev 2:1.1.5-2ubuntu1.1), my X11 headers have it declared as const. From /usr/include/X11/Xlib.h:

extern int XDrawString(
    Display*            /* display */,
    Drawable            /* d */,
    GC                  /* gc */,
    int                 /* x */,
    int                 /* y */,
    _Xconst char*       /* string */,
    int                 /* length */
);

And _Xconst is #define'd to be const in X11/Xfuncproto.h.

Adam Rosenfield
This seems to indicate that it is for compatibility.
Johannes Schaub - litb