array of 2 dimensional arrays

This is a discussion on array of 2 dimensional arrays within the C Programming forums, part of the General Programming Boards category; Hi, I have a few 2 dimensional arrays that are defined as the following: Code: const char ONE[9][6] = { ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    7

    array of 2 dimensional arrays

    Hi,
    I have a few 2 dimensional arrays that are defined as the following:

    Code:
    const char ONE[9][6] = {
    {'_','_','_','8','_','_'},
    {'_','_','8','8','_','_'},
    {'_','8','_','8','_','_'},
    {'_','_','_','8','_','_'},
    {'_','_','_','8','_','_'},
    {'_','_','_','8','_','_'},
    {'_','_','_','8','_','_'},
    {'_','_','_','_','_','_'},
    {'_','_','_','_','_','_'}
    };
    I'm trying to have an array that will hold the address of each 2 dimensional arrays.
    It should like that:
    char WORD_TO_NUMBER[] = {ZERO,ONE,TWO,THREE,FOUR};
    so if I type WORD_TO_NUMBER[1] it will return the 2 dimensional array ONE.
    but I cant seem to sort it out.
    Id be grateful if someone could help me
    Last edited by toothpick; 10-22-2010 at 10:15 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Code:
    #include <stdio.h>
    
    const char ONE[9][6] = {
    {'_','_','_','8','_','_'},
    {'_','_','8','8','_','_'},
    {'_','8','_','8','_','_'},
    {'_','_','_','8','_','_'},
    {'_','_','_','8','_','_'},
    {'_','_','_','8','_','_'},
    {'_','_','_','8','_','_'},
    {'_','_','_','_','_','_'},
    {'_','_','_','_','_','_'}
    };
    const char TWO[9][6] = {
    {'2','2','_','8','_','_'},
    {'2','_','8','8','_','_'},
    {'_','8','_','8','_','_'},
    {'_','_','_','8','_','_'},
    {'_','_','_','8','_','_'},
    {'_','_','_','8','_','_'},
    {'_','_','_','8','_','_'},
    {'_','_','_','_','_','_'},
    {'_','_','_','_','_','_'}
    };
    typedef const char (*foo)[9][6];
    // foo is a type, which is a pointer to array[9][6] of const char
    
    void func ( foo arr ) {
      int   r, c;
      for ( r = 0 ; r < 9 ; r++ ) {
        for ( c = 0 ; c < 6 ; c++ ) {
          printf("%c", (*arr)[r][c]);
        }
        printf("\n");
      }
    }
    int main()
    {
      foo all[] = {
        &ONE,
        &TWO
      };
      func(all[1]);
      return 0;
    }
    The sanity way out is to create a typedef which is the pointer to your arrays.

    Without this, you end up with a mass of ()[]* every time you want to reference the type.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 12:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21