Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!gatech!udel!haven!mimsy!chris From: chris@mimsy.umd.edu (Chris Torek) Newsgroups: comp.lang.c Subject: Re: How do you declare a pointer to a two dimensional array? Message-ID: <27417@mimsy.umd.edu> Date: 4 Nov 90 08:51:23 GMT References: <9197@aggie.ucdavis.edu> <16189@csli.Stanford.EDU> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 48 >In article <9197@aggie.ucdavis.edu> rogers@iris.ucdavis.edu >(Brewski Rogers) asks: >> float spam[4][4]; >>How do I declare a pointer to it? Is this possible? In article <16189@csli.Stanford.EDU> poser@csli.Stanford.EDU (Bill Poser) answers: > float (*spamp) [4][4]; >declares spamp to be a pointer to a 4x4 array of floats. Correct, but chances are that this is not what is desired anyway. Most likely Mr. Rogers (sorry, I just *had* to write it that way :-) ) wants either `float (*p)[4];' or `float *p;'. Consider, e.g., the array int x[10]; This declares `x' as an object of type `array 10 of int'. Objects of type `array N of T', when placed in value contexts (most C contexts are value contexts), `decay' into values of type `pointer to T' which point to the first element of that array, i.e., the element whose index is 0. So to make use of `x' one would not write int (*ap)[10] = &x; /* NB: legal only in ANSI C, not K&R-1 */ but rather int *ip = x; /* or `= &x[0]' */ After all, the only thing you can do with `ap' that you cannot do with `ip' is index (or point) off into the n'th array-10-of-int objects, and there is no other such object. There is only `ap[0]'. The same holds for a `float spam[4][4];': this is an object of type array N=4 of T=(array 4 of float), so you want a pointer to T, or a pointer to `array 4 of float': float (*p)[4] = spam; /* or `= &spam[0]' */ and now you can index p to get objects 0, 1, 2, and 3: float *f = p[1]; /* or &p[1][0] */ p[3][2] += 3.0; /* bump element 2 of array number 3 */ -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750) Domain: chris@cs.umd.edu Path: uunet!mimsy!chris