Thread: pointer to multidimensional array

  1. #1
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38

    pointer to multidimensional array

    I'm trying to use a pointer to access an array, since I need to be able to switch between two identically-sized arrays. However, I get compiler errors when I try to reference an array to my pointer. Here is a sample of the code that I have, hopefully someone can tell me what I'm doing wrong.

    Code:
    int grid[320][240];    //my array
    int *grid_ptr[][240];  //my pointer
    
    int doSomethingWithGrid(int array[][240]); //function prototype
    
    int main()
    {
        grid_ptr = &grid;    //assign pointer to grid, compile error
    
        doSomethingWithGrid(grid_ptr);  //func. wont take pointer
    }
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    106
    What errors do you get?

  3. #3
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38
    Quote Originally Posted by Bigbio2002
    I'm trying to use a pointer to access an array, since I need to be able to switch between two identically-sized arrays. However, I get compiler errors when I try to reference an array to my pointer. Here is a sample of the code that I have, hopefully someone can tell me what I'm doing wrong.

    Code:
    int grid[320][240];    //my array
    int *grid_ptr[][240];  //my pointer
    
    int doSomethingWithGrid(int array[][240]); //function prototype
    
    int main()
    {
        //error C2440: '=' : cannot convert from 'int [320][240]' to 'int *[][240]'
        grid_ptr = grid;    //assign pointer to grid, compile error
    
        //error C2664: 'doSomethingWithGrid' : cannot convert parameter from 'int *[][240]' to 'int [][240]'
        doSomethingWithGrid(grid_ptr);  //func. wont take pointer
    }
    ive modified my code slightly and inserted the errors before the lines they appear on. hope this helps.
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    int grid[320][240];
    int (*grid_ptr)[240];
    
    int doSomethingWithGrid(int (*array)[240]);
    
    int main()
    {
       grid_ptr = grid;
       doSomethingWithGrid(grid_ptr);
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38
    aha, thank you!!
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  2. Pointer with Multi-dimensional Array
    By whichet in forum C Programming
    Replies: 7
    Last Post: 11-28-2007, 12:26 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM