Date: Wed, 22 Oct 1997 16:37:38 -0700 (PDT) Message-Id: <199710222337.QAA21267@adit.ap.net> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" To: Brett Porter , hahoyt AT eng2 DOT uconn DOT edu (H. Anthony Hoyt) From: Nate Eldredge Subject: Re: Trouble with bools Cc: djgpp AT delorie DOT com (DJGPP) Precedence: bulk At 01:57 10/22/1997 +1000, Brett Porter wrote: >> Little know fact (well, not really) is that you can initalize >> your array as follows.. >> >> bool scrn[640][480] = {0}; >> >Are you sure? I don't ever recall seeing this done. K&R First Edition section 8.6 states: When the declared variable is an aggregate (a structure or array) then the initializer consists of a brace-enclosed, comma-separated list of initializers for the members of the aggregate, written in increasing subscript or member order. If the aggregate contains subaggregates, this rule applies recursively to the members of the aggregate. If there are fewer initializers in the list than there are members of the aggregate, then the aggregate is padded with zeros. So you can initialize only the first few members if you want, and the rest will contain zeros. > >> You could initalize every variable this way as well (Even 2D, 3D... nD >> arrays) by just putting a comma (,) after ever variable. >> Ex. >> >> bool foo[2][3] = { 0, 1, 0, 1, 0, 1}; >> >I'm almost certain this won't work, I think it should be {{0,1},{0,1},{0,1}} The next paragraph of K&R: Braces may be elided as follows. If the initializer begins with a left brace, then the succeeding comma-separated list of initializers initializes the members of the aggregate; it is erroneous for there to be more initializers than members. If, however, the initializer does not begin with a left brace, then only enough elements from the list are taken to account for the members of the aggregate; any remaining members are left to initialize the next member of the aggregate of which the current aggregate is a part. His example will work. I think `gcc -Wall' will give a warning about `partially bracketed initializers'. > >> Note though, by default if you initalize one variable of the array but not >> the rest, the rest get set to 0 (zero). So.... >> >> bool foo[2][3] = {1}; >> >> only sets the first item of the array to 1 but the rest to 0. As for how >> efficent (sp) this is, I don't know. There could very well be some un >> desireable affects to this (which I would love to know myself) but it >> works. >> >This is not really a standard as mar as I know. What is wrong with just >memset(foo, 0, sizeof(foo)); ? Yes it is standard, see above. The advantages are: - An initialization is done at compile time and the values are loaded from the program image. A memset() is done at runtime and takes time to execute. - On some weird machines, all-bits-zero, which memset() produces, may not be a zero for that data type. Some machines have null pointers which are not all-bits-zero, or floating point numbers where 0 is something weird. Nate Eldredge eldredge AT ap DOT net