Thread: Passing 2D arrays

  1. #1
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002

    Passing 2D arrays

    I have a 2D array of ints, which I wish to pass to a function. I define the function to receive an int**.

    When called, the array is cast.

    Inside the function, any attempt to access the array results in crashing, even though by displaying the literal address on the screen (in both the called and calling function) it's clear that the address is being recieved correctly.

    Accessing the array from main is OK, too.
    MSVC++ 6.0

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    int** and int (*)[N] are completely different. I like to stay upbeat and avoid being too negative when I describe stuff, but this is one situation where you just can't trick the compiler into being happy. To pass a 2D array, you pass a 2D array or an equivalent type. ie. T (*)[N]. If you want a 2D array of variable size for both dimensions, you need to either simulate a 2D array with a T**, or better yet, use a vector and save yourself a lot of frustration dealing with memory. But the methods are not interchangeable.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 2d Int Arrays
    By Voondebah in forum C Programming
    Replies: 2
    Last Post: 02-25-2009, 11:36 PM
  2. Help with 2d arrays
    By thamiz in forum C Programming
    Replies: 25
    Last Post: 05-25-2008, 05:06 AM
  3. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  4. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  5. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM