Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!decwrl!sgi!tarolli@westcoast.esd.sgi.com From: tarolli@westcoast.esd.sgi.com (Gary Tarolli) Newsgroups: comp.sys.sgi Subject: Re: Calling real c-men... Summary: this may help Message-ID: <96220@sgi.sgi.com> Date: 9 Apr 91 14:36:25 GMT References: <9104082133.AA15472@karron.med.nyu.edu> Sender: guest@sgi.sgi.com Organization: Silicon Graphics, Inc., Mountain View, CA Lines: 42 In article <9104082133.AA15472@karron.med.nyu.edu>, Dan Karron@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