Mail Archives: djgpp/1996/07/17/00:57:01
Reply to message 6399161 from PLATKO AT IX DOT NET on 07/14/96 10:15PM
>Anyways, I'm writing a program that handles some 3d graphic routines.
>Yet, I am having some problems with some non-graphic related stuff.
>
>So here's my questions:
>
>1. How can you pass undetermind sized arrays to a function?
As pointers, i.e.: object *.
> Let's say I have a function:
>
> void RotateObject(int x, int y, int z, object[][][])
>
> See the []'s. I have a 3 dimention array that holds the object:
>
> object[Num_of_Polys][Poly_Points][Coordinates]
C requires that all but the first bracket pair have a constant size. That
way it knows how far it needs to jump for each increment of the array
indices. The only easy way around this is with pointers. You can
construct an algorithm to determine the correct location by means of
a simple multiplication.
> Now each object being feed to the function has a diffrent number
> of polygons, or sides. Thus, the Num_of_Polys will be different.
>
> Heres what I came up with, but I can't get it to work.
>
> void RotateObject(int x, int y, int z, int num_of_sides, object);
> ^^^^^^
> I know that is wrong. How do I get around it. BTW, object is
> an integer.
Try this:
void RotateObject( int x, int y, int z, int num_of_sides, object * );
Then in your function, instead of:
(Assuming your variable is called "list" and your indices are "i", "j", and
"k"...)
list[i][j][k]
you would use this:
list[i * y * z + j * z + k]
This is in effect what the compiler itself does when it sees the expression
list[i][j][k], and should make it obvious why it needs to know the values of
'y' and 'z' in advance. :)
>2. How do I return multiple values from a function?
>
> The function described above must return the new coordinates.
> I heard besides using return, you can also use a pointer to
> return the value through a argument inthe fuction. How do I
> do this with arrays?
There are several ways to do something like what you are looking
for. There's no way, obviously, to say something like:
int, char foo( int x, int y );
But if you need to return multiple values, here's a couple of possible
methods:
1) Define a struct that holds the necessary return values and have
your function return such a struct. Example:
struct return_data
{
int answer;
float quotient;
char *processed_name;
};
struct return_data foo( int x, int y );
2) Create the necessary variables within the calling function and pass
pointers to them to your function. Example, from above:
void foo( int x, int y, int *pans, float *pqt, char *proc_name );
Then, within your function, do:
*pqt = /* the quotient */
*pans = /* the answer */
proc_name = /* the name */
return;
You don't need to do anything special to pass arrays because they are
already pointers.
3) Create some static area of memory, such as global variables or
static local variables, and have your function place its return values
in there. Example:
int answer;
int quotient;
char processed_name[100];
void foo( int x, int y )
{
...
answer = /* the answer */
quotient = /* the quotient */
processed_name = /* the name */
return;
}
See? Simple as pie. :)
John
- Raw text -