Solved

# how to pass a two dimensional array to a function

Posted on 2005-02-24
Medium Priority
5,438 Views
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
Question by:dwcronin
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 8
• 3

LVL 30

Expert Comment

ID: 13398978
>>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?
http://www.tek-tips.com/faqs.cfm?fid=5575
0

LVL 30

Expert Comment

ID: 13399027
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

Author Comment

ID: 13399179
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

LVL 30

Expert Comment

ID: 13399203
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

LVL 30

Expert Comment

ID: 13399207
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

LVL 30

Expert Comment

ID: 13399208
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

LVL 30

Accepted Solution

Axter earned 1500 total points
ID: 13399214
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

Author Comment

ID: 13399281
Holy crap you're fast.

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

LVL 30

Expert Comment

ID: 13400037
0

Author Comment

ID: 13401682
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

LVL 30

Expert Comment

ID: 13402163
>>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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: Iâ€™ve never triâ€¦
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infâ€¦
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
###### Suggested Courses
Course of the Month10 days, 10 hours left to enroll