Find the answer to your Linux question:
Results 1 to 3 of 3
you have a matrix Code: int mat1[4][4]; and you have a pointer to a matrix Code: int **mat2; can you do this; Code: void func(int **mat) { } and call ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User
    Join Date
    Apr 2003
    Location
    TEXAS
    Posts
    314

    I seem to remember to be able to do this


    you have a matrix
    Code:
    int mat1[4][4];
    and you have a pointer to a matrix
    Code:
    int **mat2;
    can you do this;
    Code:
    void func(int **mat) {
    }
    and call the function like this
    Code:
    func(mat1);
    [/code]
    The computer made me do it!! Slackware and SUSE too Gig\'em WHOOOOP!!
    \"God put me on this earth to accomplish a certain amount of tasks, At the rate I\'m going I will never die.\" (I don\'t know)

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    No, those aren't the same. A matrix declared like mat1 does really just work like an int * when used without subscripts. It's really an array of 16 ints, and only when used as itself does the compiler know that the first subscript should be multiplied by four. Using double-subscribed arrays is really just kind of a hack, although admittedly a rather useful one.
    To do what you want, you might want to use something like this instead:
    Code:
    #define ROWS 4
    #define COLS 4
    
    void test(int **m)
    {
        int x, y;
        
        for&#40;y = 0; y < ROWS; y++&#41;
        &#123;
            for&#40;x = 0; x < COLS; x++&#41;
                m&#91;x&#93;&#91;y&#93; = dostuff&#40;x, y&#41;;
        &#125;
    &#125;
    
    int main&#40;void&#41;
    &#123;
        int i;
        int **m;
        
        m = malloc&#40;sizeof&#40;*m&#41; * ROWS&#41;             /* Or sizeof&#40;int *&#41; */
        for&#40;i = 0; i < ROWS; i++&#41;
            m&#91;i&#93; = malloc&#40;sizeof&#40;**m&#41; * COLS&#41;;    /* Or sizeof&#40;int&#41; */
        test&#40;m&#41;;
        return&#40;0&#41;;
    &#125;

  3. #3
    Just Joined!
    Join Date
    Nov 2003
    Posts
    7
    You can do something like:

    void a1(int **x) {
    ...
    }

    or you can do something like:

    void a2(int x[4][4]) {
    ...
    }

    or you can even do something like (not with any compiler, though):

    void a3(int n, int x[n][n]) {
    ...
    }

    But you cannot use int** where int[][] supposed to be and vice versa. These are different types and should be treated in different ways (at the machine level).

    Still, while C does not support polymorphism, C++ does. So, you can use the following in C++ code, but not in C:
    Code:
    #include <stdio.h>
    int a&#40;int x&#91;4&#93;&#91;4&#93;&#41; &#123;
        return x&#91;0&#93;&#91;0&#93;;
    &#125;
     
    int a&#40;int **x&#41; &#123;
        return x&#91;1&#93;&#91;1&#93;;
    &#125;
     
    int main &#40;&#41; &#123;
        int p&#91;4&#93;&#91;4&#93;;
         
        p&#91;0&#93;&#91;0&#93; = 1;
        printf &#40;"%d\n", a&#40;p&#41;&#41;;
        return 0;
    &#125;
    [/code]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •