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