[net.unix-wizards] structure member names global?

pn (01/30/83)

Running V7 on a PDP11/70, I can't seem to use the same variable name
for members of different structures. Is this a bug in the compiler?
Can anyone help me get around it?

	phil ngai

goldfarb (01/30/83)

In the Ritchie compiler, members of different structures can only have
the same names under certain restrictive circumstances.  Johnson's pcc
is more liberal.

For more info, look at K & R p. 197, starting at:

	"Two structures may share a common initial sequence of members..."

ken (02/01/83)

Sorry, Phil,
	That's just the way your version of the C compiler is.  Subsequent
versions of the compiler have removed this "bug".  You can, however, use the
same structure member name in different structures AS LONG AS they are in the
same position or offset.  For example,
	struct coordinate	{ int x, y; };
	struct three_space	{ int x, y, z; };
	struct coordlist	{
		int x, y;
		struct coordlist *backcord, *forecord;
	};
are all legal.  This results because the C compiler treats members-of-structures
as global to each file.  You could partition your program up into separate
modules in which there are no duplicate members.
	There is an advantage to this, though; one could access data structures
in various ways without going through the bother of declaring a union, as in:
	struct { char byte1, byte0, byte3, byte2; };
	long rambled, scrambled;
	scrambled.byte0 = 'a';
	scrambled.byte1 = x + y / 3;
	scrambled.byte2 = -256;
	scrambled.byte3 = 0;
Newer versions of the compiler are both more flexible and restrictive.  First,
the same structure member names can be used in different structures, regardless
of their position.  Second, variables which are not declared as a particular
structure, cannot use its structure members for referencing.
Although some may prefer the old way, I think most people prefer the newer way
to access structure members.
				Ken Turkowski
				turtlevax!ken