views:

76

answers:

6

Hello,

I have a 2 dimensional array dynamically allocated in my C code, in my function main. I need to pass this 2D array to a function. Since the columns and rows of the array are run time variables, i know one way to pass it is :

-Pass the rows,column variables and the pointer to that [0][0] element of the array

myfunc(&arr[0][0],rows,cols)

then in the called function access it as a 'flattened out' 1D array like:

ptr[i*cols+j]

But i don't want to do it that way, because that would mean a lot of change in code, since earleir, the 2D array passed to this function was statically allocated with its dimensions known at compile time.

So how can i pass a 2D array to a function and still be able to use it as a 2D array with 2 indexes like

arr[i][j].

Any pointers(pun intended) will be helpful.

Thank You,

-AD.

+1  A: 

You really can't do this without changing a lot of code. I suggest to wrap this in a structure which contains the limits and then use regexp search'n'replace to fix the accesses.

Maybe use a macro like AA(arr,i,j) (as in Array Access) where arr is the structure.

Aaron Digulla
@Aaron:So basically u are saying - no programatic way to get it done, but 'search and replace'. Is it correct?
goldenmean
Yes, that's the gist of it.
Aaron Digulla
+1  A: 

As far as I know, all you can pass to a function is a pointer to the first element of an array. When you pass an actual array to a function, it is said that "the array decays into a pointer" so no information about the size(s) of the pointed array remains.

A reference to an object of type array-of-T which appears in an expression decays (with three exceptions) into a pointer to its first element; the type of the resultant pointer is pointer-to-T.

I believe you will be able to find more information about this on the C FAQ.

Axel
A: 

Numerical recipes in C (a book dealing mostly in matrices) suggests a way to do this cleanly and still deal with 2D matrices. Check out the section 1.2 "Matrices and 2D arrays" and the method convert_matrix().

Essentially what you need is to allocate a set of pointers to point to your row vectors. And pass this to your function along with the number of rows and columns.

renick
A: 

If your compiler supports C99 variable-length-arrays (eg. GCC) then you can declare a function like so:

int foo(int cols, int rows, int a[][cols])
{
    /* ... */
}

You would also use a pointer to a VLA type in the calling code:

int (*a)[cols] = calloc(rows, sizeof *a);
/* ... */
foo(cols, rows, a);
caf
A: 

A 2-d array in C is just an array of arrays. Here ya go:

void with_matrix(int **matrix, int rows, int cols) {
    int some_value = matrix[2][4];
}

int main(int argc, char **argv) {
    int **matrix;
    ... create matrix ...
    with_matrix(matrix, rows, cols);
}
Noah Watkins
A: 
#include <stdio.h>
#include <stdlib.h>
void doit(int ** s , int row , int col ) 
{
    for(int i =0 ; i <col;i++){
            for(int j =0 ; j <col;j++)
                printf("%d ",s[i][j]);
            printf("\n");
    }
}
int main()
{
    int row =10 , col=10;
    int ** c = (int**)malloc(sizeof(int*)*row);
    for(int i =0 ; i <col;i++)
        *(c+i) = (int*)malloc(sizeof(int)*row);
    for(int i =0 ; i <col;i++)
            for(int j =0 ; j <col;j++)
                c[i][j]=i*j;
    doit(c,row,col);
}

Hope this is the way you want ....

Arjit
This is it - albeit the code has slight errors in the mem. alloc part.. but got the gist.
goldenmean