• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5440
  • Last Modified:

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
0
dwcronin
Asked:
dwcronin
  • 8
  • 3
1 Solution
 
AxterCommented:
>>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
0
 
AxterCommented:
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);
}
0
 
dwcroninAuthor Commented:
I've never used a wrapper function.  What should I do with the code that you just wote to apply it to my situation.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AxterCommented:
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);
}

0
 
AxterCommented:
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);

0
 
AxterCommented:
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;
}
0
 
AxterCommented:
I forgot to call the Free2DArray function for above example.
Should be the following:
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);

      Free2DArray(My2DFloat);
}
0
 
dwcroninAuthor Commented:
Holy crap you're fast.  

Is the wrapper function initializing the values of the array?
0
 
AxterCommented:
Why the B grade?
0
 
dwcroninAuthor Commented:
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.  
0
 
AxterCommented:
>>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);
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now