How to Make a Pointer point towards an existing 2dArray

Discussion in 'C' started by falconwise, Sep 1, 2010.

  1. falconwise

    falconwise New Member

    Joined:
    Sep 1, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Original Problem
    I have an existing 2D array that is already filled with data for a matrix. I also have a structure that has a pointer looking like this double ** pointer.

    That being said. Here's a miniature version of the problem
    Code:
    int main (){
            int stuff [2][2];
            stuff[0][0] = 1;
            stuff[0][1] = 2;
            stuff[1][0] = 3;
            stuff[1][1] = 4;
            int **pointer;
            int i;
            int j;
            pointer = stuff[0][0]; //Here lies the problem: Its an incompatible pointer type
            for (i = 0; i < 2; i++){
                    for (j = 0; j < 2; j++){
                            printf("%i\n", (*(*(pointer+i)+j)));
                    }
            }
            return 0;
    }
    So my problem with this is that I don't understand pointers well enough to see what is wrong with this. From what I understand, the pointer is pointing at the first memory address of array, but I get the incompatible pointer type problem, which is confusing me. Can someone explain why this is wrong and how it should be properly coded. Thanks.
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    name of the array is always a pointer...so write:-
    pointer=stuff
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice