Link to home
Start Free TrialLog in
Avatar of dwcronin
dwcroninFlag for United States of America

asked on

how to pass a two dimensional array to a function

I have a prototype for a function (that I didn't write but have to use) and I think that it takes a two dimensional array and
I don't know how to pass one to a function.  The function prototype is:

  float func(float *parameter[])

1) Is the argument a two dimensional array?
2) If so, how do you initialize a 2D array and call this function?

Thanks
Avatar of Axter
Axter
Flag of United States of America image

>>1) Is the argument a two dimensional array?

Yes it is.

>>2) If so, how do you initialize a 2D array and call this function?
Check out the following link:
http://www.tek-tips.com/faqs.cfm?fid=5575
Here's a C wrapper function:

float **Allocate2DArray_C(int x, int y)
{
      float **ppi = malloc(x*sizeof(float**));
    float *pool = malloc(x*y*sizeof(float*));
    float *curPtr = pool;
    for( int i = 0; i < x; i++)
    {
        *(ppi + i) = curPtr;
         curPtr += y;
    }
    return ppi;

}

void Free2DArray_C(float ** Array)
{
    free(*Array);
    free(Array);
}
Avatar of dwcronin

ASKER

I've never used a wrapper function.  What should I do with the code that you just wote to apply it to my situation.
Correction on the wrapper functions.
The following version is more generic:

#include <malloc.h>

void **Allocate2DArray(int TypeSize, int x, int y)
{
      void **ppi = malloc(x*sizeof(void*));
      void *pool = malloc(x*y*TypeSize);
      unsigned char *curPtr = pool;
      int i;
      for(i = 0; i < x; i++)
      {
            *(ppi + i) = curPtr;
            curPtr += y*TypeSize;
      }
      return ppi;
      
}

void Free2DArray(void ** Array)
{
      free(*Array);
      free(Array);
}

You can put the above code in a *.c file, and the following code in a *.h header file.

#define ALLOCATE2DARRAY(Type, x, y) Allocate2DArray(sizeof(Type), x, y)

void **Allocate2DArray(int TypeSize, int x, int y);

void Free2DArray(void ** Array);

Here's an example usage for your code.

float func(float *parameter[])
{
      return parameter[1][1];
}

void SomeFunction()
{
      int x = 3;
      int y = 5;
      int ix, iy;
      float f;
      float **My2DFloat = ALLOCATE2DARRAY(float, x, y);
      for(ix = 0;ix < x;++ix)
      {
            for(iy = 0;iy < y;++iy)
            {
                  My2DFloat[ix][iy] = 1.5f * (ix+1) * (iy+1);
                  printf("My2DFloat[%i][%i] = %f\n", ix, iy, My2DFloat[ix][iy]);
            }
      }
      f = func(My2DFloat);
      printf("%f\n", f);
      return;
}
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Holy crap you're fast.  

Is the wrapper function initializing the values of the array?
Why the B grade?
The B grade is my mistake.  I'm using a laptop with on of those touch pads instead of a mouse and it read
my motion as a click above the "B".  I saw what I had done too late and I spent twenty minutes trying t fix it.
I thank you for your help and I apologize for my mistake.  If you know how to change the grade, tell me and
I'll do it.  
>>If you know how to change the grade, tell me and I'll do it.  

Don't worry about it, and thanks for the clarification.

>>Is the wrapper function initializing the values of the array?

Not the values.  The wrapper function only creates the required allocated memory, and set ups the multidimensional pointers.

But it wouldn't be that difficult to make a version that takes an extra parameter for initializing the value.
void **Allocate2DArrayWithValues(int TypeSize, int x, int y, void* Value)
{
      unsigned char **ppi = malloc(x*sizeof(void*));
      void *pool = malloc(x*y*TypeSize);
      unsigned char *curPtr = pool;
      int i;
      int ix, iy;
      for(i = 0; i < x; i++)
      {
            *(ppi + i) = curPtr;
            curPtr += y*TypeSize;
      }
      if (Value != 0)
      {
            for(ix = 0;ix < x;++ix)
            {
                  for(iy = 0;iy < y;++iy)
                  {
                        memcpy(&ppi[ix][iy], (unsigned char *)Value, TypeSize);
                  }
            }
      }
      return ppi;
}

You can call above function like this:
      float f = 1.3f;
      float **My2DFloat = Allocate2DArrayWithValues(sizeof(float), x, y, &f);