Constness of pointed to data from constant structure

J

Jens Remus

I am trying to port Lua (http://www.lua.org/) 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
data.

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):
- http://groups.google.com/group/alt....3bc687b3cbc/491092b4e2f1d445#491092b4e2f1d445
- http://groups.google.com/group/comp.lang.c/browse_thread/thread/474976ae4ebdce2/335cb17e37ec4e27
- http://groups.google.com/group/comp...2b4a484b610/e2f2dff150a0a24f#e2f2dff150a0a24f
- http://groups.google.com/group/comp...c004fa59cfc/83591ef1b917094d#83591ef1b917094d

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
 
P

Phil Carmody

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

Jens Remus said:
I am trying to port Lua (http://www.lua.org/) 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
data.

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 */

Yikes. It's taken paranoia one step too far.
}

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.

Will that compiler compile any versions of GCC?
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):
- http://groups.google.com/group/alt....3bc687b3cbc/491092b4e2f1d445#491092b4e2f1d445

Not touching a delphi group, in case I get cooties.

Didn't look relevant.

Didn't look relevant.
- http://groups.google.com/group/comp...c004fa59cfc/83591ef1b917094d#83591ef1b917094d
""" """

Bingo.

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)

The pointer which is the formal parameter to the function is indeed as you say.
- 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

Bang on.
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.

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top