Thread: 2 dimentional array

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You create a 2-dimensional array using the knowledge you already have about creating a 1-dimensional array. As you've started doing, you create a 1D array of 1D arrays.
    First you make a variable to hole the array of arrays: done.
    Next you create the array to hold the other arrays and assign that to the variable: done.
    Next you create all those other arrays and assign them to the positions in the array: NOT done.
    It helps a lot if you typedef the kind of pointer that goes in the first array.

    So you're just missing the loop that creates all those actual data arrays. You already have the array to hold them all. Think you can finish it now?

    btw this kind of array even allows for the secondary arrays to be of differing lengths.

    Now the other way to create a 2-D array of m*n items is to simply create an array that is m*n big. Then you step across in multiples of m to go down by one n. That's how bitmaps do it too. This method is usually preferred because it is more efficient. However, unlike the other method, this way you can't use the array[m][n] syntax. Instead it's better to make a function to to the lookups, and then you get the even better syntax of: lookup(m, n).
    Last edited by iMalc; 11-11-2007 at 12:24 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. passing 3 dimentional array into function
    By 182blink in forum C Programming
    Replies: 3
    Last Post: 10-28-2006, 05:20 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM