This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Expr constructor question


"Weihaw Chuang" <weihawchuang@hotmail.com> writes:

> At the highest level, can someone tell me why the following is causes
> an error:
> #include <stdio.h>
> struct record {
>     int a;
> };
> 
> struct record object;
> struct record * ptr = &object;
> struct record * ptr2 = ptr;        // <============ problem
> 
> int main()
> {
>     printf("done");}
> 
> >gcc rec2.c -S
> rec2.c:8: initializer element is not constant

That is an error because in C you are only permitted to initialize a
global variable to a constant.  "&object" is a constant--object is not
going to move at runtime.  "ptr" is not a constant.

The rules are different for C++.  In C++ ptr2 will be initialized
using a static constructor function.  C does not support this usage.

> Now putting aside C rules, say I want to do use the problem line, and
> am willing to assume that ptr2 always has the same value as ptr.
> Could I have two identifiers "ptr" and "ptr2" in the front-end point
> to the same VAR_DECL record pointer?

I doubt it, but I'm not really sure.  There is no way to express this
sort of notion--variable synonyms--in C, so there is no reason for the
compiler to support it.

> Another route is change the
> declaration mechanism.  The problem here is that there is a great deal
> of guard code in c-typeck.c/digest_init involving TREE_CONSTANT
> checks.  Why are these checks needed?

They are required by the language definition.

What are you really trying to do?  Why not just do
    #define ptr2 ptr
?

Ian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]