Taking addresses of constants

Math Student from Hell entropy at pawl.rpi.edu
Sun Jun 18 04:15:21 AEST 1989


Consider the following code:

static char foo[]="\0\0\0\0\0\0\0\0";
...
{
auto char **x; /* 'foo' has type (char *), 
                   so '&foo' will have type (char **) */
x = &foo;

**x = 'w';
}

Now, this code should be illegal and fail to compile, because 'foo' is
a constant expression and so has no address.  or, if the compiler
allows this, it ought to stick the value of 'foo' in some static area
and then generate code that assigns the address of this area to 'x' at
the appropriate time.

The old IBM 370 C compiler (Meaning the one that came out before last
month) does neither of these things.  It generates assembly
code that carries out the statement

	x = foo;

instead of 

	x = &foo;

And thus, when I execute **x = 'w';,  I get a segmentation violation.

Now:

1) Is this a common mistake?  Is it a mistake at all?

2) What does the ANSI standard have to say about this kind of situation?
   Is '&foo' disallowed entirely, or does the compiler have to stuff
   the value of 'foo' somewhere and then give you a pointer to it?

Thank you.


    What a wonderful thing is the human brain; how I wish I possessed one.
Mark-Jason Dominus 	   entropy at pawl.rpi.EDU	     entropy at rpitsmts (BITnet)



More information about the Comp.lang.c mailing list