Thread: passing/pointing to a 2d array

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    66

    passing/pointing to a 2d array

    I have a 2d array as so:

    PHP Code:
    char array[num_of_files][100]; 
    and a pointer

    PHP Code:
    charp
    which points to the array as so

    PHP Code:
    = array; 
    I want to pass this pointer to another function, and access the array from that function.

    PHP Code:
    new_func(p); 
    New function is declared like so
    PHP Code:
    int new_func(charp); 
    In the new function, i cant cant seem to move along the 2d array.

    What am i doing wrong??

    Thanks

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    New function is not declared correctly. Declare like so:
    Code:
    int new_func(char p[][100])
    Otherwise the new_func code doesn't know how to calculate the offsets when you do p[1][5] (how does it know how to get to the next row without knowing the row size?)

    Have a good read of this FAQ:

    http://www.eskimo.com/~scs/C-faq/s6.html

    Pay particular attention to 6.18.

  3. #3
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    Code:
    char array[num_of_files][100]; 
    char **p = array; 
    
    int new_func(char **p)
    {
    }
    
    new_func(p);

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by rockytriton
    Code:
    char array[num_of_files][100]; 
    char **p = array; 
    
    int new_func(char **p)
    {
    }
    
    new_func(p);
    You too should read the exact same FAQ I mentioned above. A pointer to a pointer is not the same as a two dimensional array.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    Quote Originally Posted by cwr
    New function is not declared correctly. Declare like so:
    Code:
    int new_func(char p[][100])
    Otherwise the new_func code doesn't know how to calculate the offsets when you do p[1][5] (how does it know how to get to the next row without knowing the row size?)

    Have a good read of this FAQ:

    http://www.eskimo.com/~scs/C-faq/s6.html

    Pay particular attention to 6.18.

    Thankyou thankyou. Works a like a dream now. Good FAQ also.

    Cheers.

  6. #6
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    Quote Originally Posted by cwr
    You too should read the exact same FAQ I mentioned above. A pointer to a pointer is not the same as a two dimensional array.
    That's for clearing that up for me, I always thought it was the same, I've used it like that in the past a lot.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should listen to your compiler then.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by rockytriton
    That's for clearing that up for me
    You're welcome.

    I always thought it was the same, I've used it like that in the past a lot.
    No you haven't, or you'd get instant garbage or a memory access error when you tried to access the elements in the function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM