Initialize 2-D array of unknown size

前端 未结 6 1936
我在风中等你
我在风中等你 2021-01-16 00:42

I have a 2-D array of characters e.g. char aList[numStrings][maxLength]. ideally, during program execution I want to be able to modify the contents of aList i.

6条回答
  •  执笔经年
    2021-01-16 01:28

    If you really want to be able to remove items from the middle of the grid (your questions isn't clear on this), you'll need some kind of multiply linked structure. These are often used to implement sparse arrays, so you can probably find one pre-made.

    I'm talking about something like this:

    +---+  
    | A |  
    +-|\+  
      | \  
      |  \  
      |   \  
      |    \
      |     +----+----+----+  
      |     | C0 | C1 | C2 | ...  
      |     +--|-+----+--|-+  
      |        |         |
      |        |         |  
    +-V--+  +--V-+       | +----+
    | R0 |->|a0,0|-------+>|a0,3|--> ...
    +----+  +--|-+    +--V-+----+
    | R1 |-----+----->|a1,2|--> ...
    +----+     |      +--|-+
     ...       V         |
              ...        V
                        ...  
    

    Where A is the root node of the object, C is an array of column pointers, R is an array of row pointers, and each cell points to it next neighbor along both its row and column. All cells not explicitly represented are assumed to have some default value (usually NULL or 0).

    It is a simple idea, but a fairly picky implementation, with lots of chances to mess up, so use a debugged library if you can.

提交回复
热议问题