-2

I need to pass a pointer to a two dimensional array to a function but can't get it working, what I've tried:
array definition:

#define BFD_T_CNT           4096
static UINT32 lan_port_bitfield_by_bfd[2][BFD_T_CNT] = {0};

function prototype

int MyFunc(int ncols, int BfdSessionId, UINT32 *puser_ports, 
           UINT32 *ptdm_ports, UINT32 pPortBitmap[][ncols])

call:

MyFunc(BFD_T_CNT, BfdSessionId, &Cacheuser_ports[BfdSessionId-1], 
       &Cachetdm_ports[BfdSessionId-1], lan_port_bitfield_by_bfd);

but I keep getting an error that says:
argument type does not match prototype

Why is this and how di I do this otherwise?

EDIT 1

I tried it as suggested by dbush with the prototype as follows: int MyFunc(int ncols, int BfdSessionId, BITFIELD *puser_ports, BITFIELD *ptdm_ports, UINT32 **pPortBitmap) and casting lan_port_bitfield_by_bfd into a (UINT32)** when the function is called which compiled but I could not succesfully access thedata within the function, my access looks like if (pPortBitmap[0][BfdSessionId] & (0x01<<port)) and I got an exception error right there. I've tried as mch suggests in the comment to supply ncols & nrows to the function but it would not compile but give me the ** argument type does not match prototype** error again...

stdcerr
  • 13,725
  • 25
  • 71
  • 128
  • 1
    You should post the error message with information about other arguments declarations. – haccks Oct 23 '15 at 20:47
  • 1
    ncols is not known at compile time, this cannot work – Chris Oct 23 '15 at 20:48
  • there's no further clarification other than `(etoa:1551): argument type does not match prototype` - the compileris with **diab** from WindRiver – stdcerr Oct 23 '15 at 20:49
  • @Chris; You should not forget about variable length arrays. – haccks Oct 23 '15 at 20:51
  • @cerr you edited your question, so the only visible problem disappeared, can you post something like http://ideone.com/9gwnQQ so that we can see the exact problem? – mch Oct 23 '15 at 20:59
  • @haccks: Thanks I actually forgot about that – Chris Oct 23 '15 at 21:01
  • 1
    do you call this function only with this array? In this case you can change the prototype to `int MyFunc(int ncols, int BfdSessionId, UINT32 *puser_ports, UINT32 *ptdm_ports, UINT32 pPortBitmap[][BFD_T_CNT]);`, so it is no VLA anymore. This would only be a problem if you are using a C++ compiler. Also you should not descripe what you do, you should post the code for a complete program, which produces your error. If you look at my link above, you will see that your code works for me. – mch Oct 23 '15 at 21:30

1 Answers1

-1

Here's a pair of examples on how to work with a two-dimensional array without knowing either dimension at compile time. The first uses a plain int * and figures out the row/column offset manually, while the other uses variable length array arguments.

#include <stdio.h>
#include <stdlib.h>

void f1(int *matrix, int row, int col)
{
    int i,j;
    int *p;
    for(i=0;i<row;i++) {
        // each row is "col" ints long
        p = matrix + (i*col);
        for(j=0;j<col;j++) {
            printf("matrix[%d,%d]=%d\n",i,j,p[j]);
        }
    }
}

void f2(int row, int col, int matrix[row][col])
{
    int i,j;
    for(i=0;i<row;i++) {
        for(j=0;j<col;j++) {
            printf("matrix[%d,%d]=%d\n",i,j,matrix[i][j]);
        }
    }
}

int main()
{
    int matrix[3][5] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
    f1(&matrix[0][0],3,5);
    f2(3,5,matrix);
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Why don't you switch the parameters and make `matrix` to an real 2d array so you can remove that illegal cast from `int[3][5]` to `int**`: `void f1(int row, int col, int matrix[row][column])`? – mch Oct 23 '15 at 21:09
  • @mch please see **EDIT 1** above. Thanks – stdcerr Oct 23 '15 at 21:24
  • @mch Edited to get rid of the sloppy cast, and added a variable length array example as well. – dbush Oct 23 '15 at 22:30