multidimensional array as function argument

I want to know if I can declare a function like this.
 
void doublmatrix(int **x, int **y, int N, int M)
 
I have seen functions declare like this before, but I am having trouble with that since the number of column needs to be specified.
 
I received an error message said that "can not convert int (*) [3] to int **".
 
Here a sample code below, I want to take the transform of an image.  This is not the actual code, but use for simplification.  It also gave me the same error message.
 
The code work when I declare it like this: void doublematrix(int x[][3], int y[][3], int N, int M).  I also want to know if a function can declare like this void doublematrix(int x[N][M], int y[N][M], int N, int M) or void doublematrix(int x[N][M], int y[N][M], const int N, const int M).
 
Finally, another problem that I want to solve is declare the function like this
void doublematrix(int **x, int **y), and use the sizeof to determine the number of element.  I did it like that for a simple matrix and it works.  I don't know if I did it right.  This is the first time I have been playing with these stuffs.  
 
I will appreciate any help.  
 
//*************************************
int i, j, k; //k is tne number or raw, j the number of colunm
   i = (unsigned char)sizeof(a);
   j = (unsigned char)sizeof(a[0]);
   k = i/j;
//*****************************
#include <stdio.h>
#include <conio.h>
 
void doublematrix(int **x, int **y, int N, int M);
 
void main()
 {
   int x[3][3] = {{0,1,2},
          {3,4,5},
                {6,7,8}};
   int n,k;
   const int N = 3;
   const int M = 3;
   int y[N][M];
 
   doublematrix(x,y,N,M);
 
   for (n=0;n<N;n++)
    {
      for (k=0;k<M;k++)
       {
       printf("%d ", y[n][k]);
         }
      }
   getch();
   }
 
void doublmatrix(int **x, int **y, int N, int M)
 {
   int n;
   int k;
 
   for (n=0;n<N;n++)
    {
      for (k=0;k<M;k++)
       {
         y[n][k] = 2*x[n][k];
         }
      }
 
}
 
 
 
volvickderoseAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
AlexVirochovskyConnect With a Mentor Commented:
>>.  I typecasted it.
It is exectly error(of JKR). Why?
See chapter 7 of ptrtutor, that i 've sent you. Personally, i avoid using
multidimension array, simpler and better
use 1 dimension!
0
 
jkrCommented:
>>I received an error message said that "can not convert
>>int (*) [3] to int **".

Why don't you simply use a typecast, e.g.

doublematrix( (int**) x, (int**) y,N,M);

?
0
 
AlexVirochovskyCommented:
Next is working text. If you want explanation, i can send you tutorial by ptr or download it youself from
http://www.snippets.org/ptrtutor.txt

#include <stdio.h>
#include <conio.h>

void doublematrix(int x[][3], int y[][3], int N, int M);
 
void main()
 {
   int x[3][3] = {{0,1,2},
          {3,4,5},
                {6,7,8}};
   int n,k;
   const int N = 3;
   const int M = 3;
   int y[N][M];

   doublematrix(x,y,N,M);

   for (n=0;n<N;n++)
    {
      for (k=0;k<M;k++)
       {
       printf("%d ", y[n][k]);
         }
      }
   getch();
   }
 
void doublematrix(int x[][3], int y[][3], int N, int M)
 {
   int n;
   int k;

   for (n=0;n<N;n++)
    {
      for (k=0;k<M;k++)
       {
         y[n][k] = 2*x[n][k];
         }
      }
 
}
Alex
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
volvickderoseAuthor Commented:
Typecast seems to work, but I still receive and error message ":) error expected
0
 
imladrisCommented:
You should be clear on the fact that even though you can access int **x and int x[][3] with the same syntax, they are materially different constructs.

When the compiler encounters int x[][3] it assumes it is dealing with a block of memory representing a bunch of integers. It will access them by calculating an appropriate offset from the start of the block. For instance, if the integers are two byte things, then for x[i][j] it will go

&x + (i*3*2) + j*2

that is: address of the base of x plus skip over i rows (which are 3 integers (which is 3*2 bytes)) and j integers.

However for int **x it will be doing actual pointer dereferencing. For example x[0][0] will go to get the pointer at x and then follow this to a memory location. x[0][1] will get the pointer at x follow this to a new location and the add 2. x[1][0] will get the second pointer at x (i.e. the pointer at location &x+4) then follow that etc. etc. In other words it will literally be doing a double indirection as the declaration implies.


Now you appear to be wanting to deal with a variable sized matrix. There are various solutions to this problem. None are quite straightforward or clean. Firstly you must not mix the two paradigms. If a block is being handed in to the function, it must declare it as a block (i.e. x[][3]). This may make it difficult to do variable sizes. The alternative is to construct it as an array of pointers to one dimensional arrays of integers (i.e. int **x). Note though, that in this case you will need some other mechanism for indicating what size the arrays are (and note that each one dimensional array of integers COULD be a different length). This is because there is no data in memory that tracks object sizes. Any object size information in C comes from the compilers knowledge of the data, i.e. the declarations. But in this case, since you would be doing dynamic allocation (so that the matrices can be dynamically sized), the compiler doesn't know what size things will be.

0
 
volvickderoseAuthor Commented:
Did not work
0
 
ozoCommented:
void doublematrix(int *x, int *y, int N, int M)
                 {
                   int n;
                   int k;

                   for (n=0;n<N;n++)
                    {
                      for (k=0;k<M;k++)
                       {
                         y[n*N+k] = 2*x[n*N+k]+k;
                         }
                      }

                }
0
 
volvickderoseAuthor Commented:

This is the most important problem I have now

how to call this function

void doublematrix(int **x, int **y, int N, int M);

I tried to use that argument in the main, but it did not work

void doublematrix((int**)x, (int**)y, int N, int M);

Any help will be appreciated

thanks
0
 
ozoCommented:
void doublematrix(int **x, int **y, int N, int M);
 
void main() {
   static int x0[3]={0,1,2};
   static int x1[3]={3,4,5};
   static int x2[3]={6,7,8};
   static int y0[9]={0,0,0,0,0,0,0,0,0};
   int *x[3] = {x0,x1,x2};
   int n,k;
   const int N = 3;
   const int M = 3;
   int *y[3] = {&y0[0],&y0[3],&y0[6]};
 
   doublematrix(x,y,N,M);
 
   for( n=0; n<N; n++){
      for( k=0; k<M; k++ ){
         printf("%d ", y[n][k]);
      }
   }
   getch();
}
 
void doublematrix(int **x, int **y, int N, int M){
   int n;
   int k;
   for( n=0; n<N; n++ ){
      for( k=0; k<M; k++){
         y[n][k] = 2*x[n][k];
      }
   }
}
0
 
volvickderoseAuthor Commented:
Thank you OZO

Your code works, but I don't understand it very well.  The problem I try to solve is for a bigger matrix.  I put this as an example.  The problem I tried to solve, si to  take the transform of an image which is 256X256.  

The way I had wanted to declare the function is

void doublematrix(int **x, int **y), and use the size of operator the find the number of row and colunm.  Since I know how to find the number of row and colunm, I put the fucntion like this

void doublematrix(int **x, int **y, int N, int M)

to simplify it.

What I want, the user to input the x matrix, which is the same size as y without enter their dimensions.  

Any way, it would be helpfull if you can do it witout introducing x0, x1, x2,
x3, and y0.  What I want, start wih the constant like

const int N = 3;
const int M = 3;

int x[N][M], y[N][M]//x input, y ouput

y can be initialized to zero by a for loop

The problem is calling the function knowing only the matrix X and the size N and M.  

Somebody suggested using malloc or new, but I don't know how to do that.

I am going to rework your code and try to put it in the form that I want, but in t he mead time, if somebody can work it out the way I want it, it will be fantastic.

Again, the problem I tried to solve is:

void doublematrix(int **x, int **y);

call a function to double the element in a matrix; user only input the matrix size probalby without the dimesion.

Thanks a lot




0
 
AlexVirochovskyCommented:
volvickderose, you are rejected my working code and , i see, don't read
the tutorial with replies to you questions..
0
 
volvickderoseAuthor Commented:
The link is bad
0
 
AlexVirochovskyCommented:
Write you EMail, i 'll send you
0
 
volvickderoseAuthor Commented:
send to volvick.derose@senstytech.com
0
 
volvickderoseAuthor Commented:
I don't reject your code, it simply doesn't work.  I typecasted it,
it compiled, but it doesn't work.  

When I wrun it it crashed, here is the code.

Try it and let me know

#include <stdio.h>
#include <conio.h>

void doublematrix(int **x, int **y, int N, int M);

void main()
      {
   int x[][3] = {{0,1,2},
                         {3,4,5},
                {6,7,8}};
   int n,k;
   const int N = 3;
   const int M = 3;
   int y[N][M];

   doublematrix((int**)x,(int**)y,N, M);

   for (n=0;n<N;n++)
         {
      for (k=0;k<M;k++)
            {
            printf("%d ", y[n][k]);
         }
      }
   getch();
   }

void doublmatrix(int **x, int **y, int N, int M)
      {
   int n;
   int k;

   for (n=0;n<N;n++)
         {
      for (k=0;k<M;k++)
            {
         y[n][k] = 2*(x[n][k]);
         }
      }

   }
0
 
AlexVirochovskyCommented:
All my post returned. Send me EMail to
alexvirochovsky@yahoo.com and i send you reply. Now is only chapter 7:
->================================================================
CHAPTER 7: More on Multi-Dimensional Arrays

    In the previous chapter we noted that given

  #define ROWS 5
  #define COLS 10

  int multi[ROWS][COLS];

we can access individual elements of the array "multi" using
either:

    multi[row][col]    or  *(*(multi + row) + col)

To understand more fully what is going on, let us replace

    *(multi + row)    with   X   as in:

    *(X + col)

    Now, from this we see that X is like a pointer since the
expression is de-referenced and we know that col is an integer.
Here the arithmetic being used is of a special kind called
"pointer arithmetic" is being used.  That means that, since we
are talking about an integer array, the address pointed to by
(i.e. value of)   X + col + 1   must be greater than the address
X + col   by and amount equal to sizeof(int).

    Since we know the memory layout for 2 dimensional arrays, we
can determine that in the expression     multi + row    as used
above,   multi + row + 1    must increase by value an amount
equal to that needed to "point to" the next row, which in this
case would be an amount equal to     COLS * sizeof(int).

    That says that if the expression     *(*(multi + row) + col)
is to be evaluated correctly at run time, the compiler must
generate code which takes into consideration the value of COLS,
i.e. the 2nd dimension.  Because of the equivalence of the two
forms of expression, this is true whether we are using the
pointer expression as here or the array expression
multi[row][col].

Thus, to evaluate either expression, a total of 5 values must be
known:

    1)  The address of the first element of the array, which is
        returned by the expression  "multi", i.e. the name of the
        array.

    2)  The size of the type of the elements of the array,  in
        this case sizeof(int).

    3)  The 2nd dimension of the array

    4)  The specific index value for the first dimension, "row"
        in this case.

    5)  The specific index value for the second dimension, "col"
        in this case.

    Given all of that, consider the problem of designing a
function to manipulate the element values of a previously
declared array. For example, one which would set all the elements
of the array "multi" to the value 1.

   void set_value(int m_array[][COLS])
   {
     int row, col;
     for(row = 0; row < ROWS; row++)
     {
       for(col = 0; col < COLS; col++)
       {
          m_array[row][col] = 1;
       }
     }
   }

And to call this function we would then use:

    set_value(multi);

    Now, within the function we have used the values #defined by
ROWS and COLS which set the limits on the for loops. But, these
#defines are just constants as far as the compiler is concerned,
i.e. there is nothing to connect them to the array size within
the function.  row and col are local variables, of course.  The
formal parameter definition informs the compiler that we are
talking about an integer array.  We really don't need the first
dimension and, as will be seen later, there are occasions where
we would prefer not to define it within the parameter definition
so, out of habit or consistency, I have not used it here.  But,
the second dimension _must_ be used as has been shown in the
expression for the parameter.  The reason is that it is needed in
the evaluation of   m_array[row][col]   as has been described.
The reason is that while the parameter defines the data type (int
in this case) and the automatic variables for row and column are
defined in the for loops, only one value can be passed using a
single parameter.  In this case, that is the value of "multi" as
noted in the call statement, i.e. the address of the first
element, often referred to as a pointer to the array. Thus, the
only way we have of informing the compiler of the 2nd dimension
is by explicitly including it in the parameter definition.

    In fact, in general all dimensions of higher order than one
are needed when dealing with multi-dimensional arrays.  That is
if we are talking about 3 dimensional arrays, the 2nd _and_ 3rd
dimension must be specified in the parameter definition.

0
All Courses

From novice to tech pro — start learning today.