Request for Comments: Aggregate Assignment in C ...

Mayer Goldberg mayer at iuvax.cs.indiana.edu
Tue Dec 11 14:42:11 AEST 1990


Pascal programmers who learn C, frequently complain of C's
lack of a "with" construct. We propose an alternative to
adding such a keyword, through a natural extention of C's
aggregate initialization statement.

Consider the following program fragment:

struct ag {
  int a, b, c;
};

struct ag my_vec = {1, 2, 3};

This form of an "assignment" is permitted only during the
initialization of a variable (at compile time). Any other way
of assignment to fields in my_vec will have to be done by routing
through my_vec, as in:

my_vec.a = 4; my_vec.c = 7; /* etc. */

We submit that a better way of doing this would be to allow a
kind of assignment that we allow during initialization.
Something like:

my_vec = {4,, 7};

If for example we wished to assign 2 and 3 to the 'a' and 'b'
fields of my_vec, any one of the following should be ok:

my_vec = {2, 3}; 

or 

my_vec = {2, 3,};

This mechanism should work for both structures and arrays.

This solves the "with" problem in C. It also opens a delicious
can of worms: Should the order of evaluation of the aggregated 
expressions be specified? Perhaps not, for this would be in
line with C's philosophy of not specifying the order of
evaluation of parameters to a function, or alternately, we
could demand a left-to-right order of evaluation as implied by
the comma operator.

Mayer Goldberg 		 	Nick Cline
mayer at iuvax.cs.indiana.edu	cline at silver.ucs.indiana.edu



More information about the Comp.lang.c mailing list