Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!ut-emx!jamshid From: jamshid@ut-emx.uucp (Jamshid Afshar) Newsgroups: comp.lang.c Subject: Re: Why didn't ANSI make initialisation consistent ???? Summary: use C++ Keywords: C++ initialization Message-ID: <47999@ut-emx.uucp> Date: 26 Apr 91 21:42:38 GMT References: <1991Apr24.141206.18103@grep.co.uk> <959@agcsun.UUCP> Organization: The University of Texas at Austin; Austin, Texas Lines: 48 In article <959@agcsun.UUCP> jackm@agcsun.UUCP (Jack Morrison) writes: >In article <1991Apr24.141206.18103@grep.co.uk> vic@grep.co.uk (Victor Gavin) writes: ... >>Which made me believe that I could use the following code: >> struct bert { int a, b; } >> struct fred { struct bert *abc; } blip = { {1,1} }; ... >It's not so much an *initialization* inconsistency, but the limitation that >you can't write a constant structure value the same way you can a constant >string. For example, it would also be nice to be able to call > > void foo(struct bert b); > > foo( {1,1} ); >or, if the compiler would like a little more help, > foo( (struct bert){1,1} ); I just thought I'd mention that this kind of structure-on-the-fly stuff is possible in C++. struct bert { int a, b; bert(int the_a, int the_b) : a(the_a), b(the_b) {} }; foo( bert b ); // you don't need the keyword 'struct' ... foo( bert(5, 6) ); // call foo() with temporary bert Yes, it's a temporary created on the stack, but you can make a static object: static bert a_bert = bert(5, 6); or static bert a_bert(5, 6); // these two definitions are equivalent But, as Jack said, you can't write a constant structure value the same way you can a constant string in C or C++ (can't gcc do something like this, though?). Therefore, there is no way to do what Victor wanted to do without another variable. Anyway, I think every C programmer should take a look at C++, even if she or he doesn't do object-oriented programming (though I have found OOP to be a Very Good Thing). C++ removes alot of restrictions on initializations, has type-safe i/o (doesn't use var. args), is more strongly typed, has type-safe linkage, and IMO is just a better C. An ANSI C program should compile without any changes (except C++ requires a cast when assigning a void pointer to a typed pointer). Just another cog in the wheel of the OOP propaganda machine, :) Jamshid Afshar jamshid@emx.utexas.edu