Constness of pointed to data from constant structure

Discussion in 'C Programming' started by Jens Remus, Jul 20, 2009.

  1. Jens Remus

    Jens Remus Guest

    I am trying to port Lua ( to a non-PC platform and
    am facing a C compiler issue regarding standards.

    The issue is about the C compiler's interpretation of "const struct",
    pointers inside the structure and the constness of the pointed to

    The Lua sourcecode has some places where parameters are passed as
    "const struct ... *" to functions. The function then accesses parts of
    the structure as being non-const.

    Simplified Example:

    typedef struct Node {
    } Node;

    struct X {
    Node * node;

    void dosthwithx(const struct X * x_ptr) {
    Node * n = &x_ptr->node[0]; /* compilation error */

    The used compiler is complaining that the operands types are not
    compatible (i.e. "pointer to structure Node" vs. "pointer to constant
    structure Node"). There is no other C compiler on this platform to
    choose from.

    Searching comp.lang.c, comp.std.c, and de.comp.lang.c on the ANSI/ISO
    definition of "const struct" regarding pointers inside the structure,
    I stumbled upon the following threads that cover the subject at least
    partly (the last one being the most relevant from my point of view):

    My understanding of "const struct *" from the threads above and other
    C documentation is:
    - the pointed to data (structure) is treated as being constant
    - the pointer itself is treated as non-constant (one had to specify
    "const struct * const" to make the pointer itself constant)
    - pointers inside a constant structure are treated as constant (as
    they are part of the data), but the pointed to data (structure) is to
    be treated as non-constant

    The C compiler on my platform fails on the last point and treats
    pointed to data of a constant structure as being constant too (which
    makes somewhat sense to me).

    Is this a compiler error or are the ANSI/ISO standards not clear on
    whether to treat pointed to data from a const struct as const or not?

    Where can I read more on the ANSI/ISO definition of "const"? If it is
    a compiler error I need some proof to convince the compiler
    programmers of my platform to investigate the issue.

    Thanks in advance, greetings from Germany,
    Jens Remus, Jul 20, 2009
    1. Advertisements

  2. Jens Remus

    Phil Carmody Guest

    replying to both newsgroups, as I don't regularly read c.s.c

    Yikes. It's taken paranoia one step too far.
    Will that compiler compile any versions of GCC?
    Not touching a delphi group, in case I get cooties.
    Didn't look relevant.
    Didn't look relevant.
    The pointer which is the formal parameter to the function is indeed as you say.
    Bang on.
    Well, it's all in n1256.pdf, but alas there's a lot of it.
    n869.txt is possibly easier to browse, but a more dated draft of the
    standard (pre-C99 standardisation, n1256 is post-standardisation).

    Phil Carmody, Jul 20, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.