Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!amdcad!ames!hao!boulder!sunybcs!rutgers!iuvax!pur-ee!uiucdcs!uiucdcsb!wsmith From: wsmith@uiucdcsb.cs.uiuc.edu Newsgroups: comp.lang.c Subject: initialization of unions Message-ID: <165600017@uiucdcsb> Date: Sun, 25-Oct-87 18:10:00 EST Article-I.D.: uiucdcsb.165600017 Posted: Sun Oct 25 18:10:00 1987 Date-Received: Wed, 28-Oct-87 20:45:50 EST Lines: 39 Nf-ID: #N:uiucdcsb:165600017:000:1096 Nf-From: uiucdcsb.cs.uiuc.edu!wsmith Oct 25 17:10:00 1987 I have a question about initialization of variables in C. Kernighan & Ritchie do not speak of what happens when a union is attempted to be initialized. The 4 compilers available to me disagree on what is legal. K&R say that if an initialization list is incomplete, the remaining fields are initialized to zero, which works on all 4 compilers, if for example the union is the last element of a structure and I do not leave a value for that place. The difference occurs when I try to place a value into the union field. 2 consider this to be an error, one bluntly saying: "unions can not be initialized". The other two allow the initialization to take place if the first option of the union matches the value being placed in. Which two are correct according to the new standard? struct foo { union { char * pch; int a; } j; int l; } unionvar = { "hello\n", 37 }; This ^^^ works on two and not on the other two. struct foo2 { int l; union { char * pch; int a; } j; } unionvar = { 37 }; This ^^^ works on all four. Bill Smith ihnp4!uiucdcs!wsmith wsmith@a.cs.uiuc.edu