Calling real c-men...
Gary Tarolli
tarolli at westcoast.esd.sgi.com
Wed Apr 10 00:36:25 AEST 1991
In article <9104082133.AA15472 at karron.med.nyu.edu>, Dan Karron at UCBVAX.BERKELEY.EDU writes:
>
> Here is a problem at the border of the C-language. It is on the edge of
> c's value as a terse, compact language, and a slippery mess when it comes to
> arrays and pointers. I would like someone to show me that this is really
> a beautiful feature to be appreciated and loved!
>
> Here is my problem. I have allocated a tree of structures, and I want to
> pass a pointer to a piece of a structure to a subroutine. Now what I want to
> pass is a Pointer to an 2 dim CONTIGUOUS array of floats (Like a Matrix typedef,
> in that each float is in storage adjacent to each other, not an array of pointers to a list of floats)
>
> In the receiving subroutine, I would like to be able to reference each
> element in the array pointed to by the same indexes as in the main structure.
>
I am not sure that this is what you need, but here goes. If you have an
array of float, eg. mat[4][4] and you pass it to a subroutine, you can
declare the incoming parameter as
float (*inmat)[4]; /* inmat is a pointer to 4 floats */
You can then index the array as inmat[row][column]; With different
declarations (or castings) you can treat the array as having different
dimensions, eg. float (*inmat5)[5]; inmat5 = ((*)[5])inmat; inmat5[1][4];
You can also assign these pointers and increment them:
float mat[4][4];
inmat = mat[2]; /* assign it to point to third row */
inmat++; /* increment it to fourth row */
Although you can do most of this with a paramenter declaration,
eg. inmat[][4], you cannot declare extra parameters. Thus this technique
of using a pointer to a single dimen. array of floats is much more
versatile.
WARNING - although I have done this and I know it works, I have not checked
my examples above with the compiler. So they may be off by a paren
or two.
--------------------
Gary Tarolli
More information about the Comp.sys.sgi
mailing list