Thread: Array of Arrays (possible in C)?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    9

    Question Array of Arrays (possible in C)?

    Believe everything is possible in C.

    Want to do an array of arrays:
    each element in the top level array is same in size.

    For example:

    top_array[0] = char array_element[10];
    top_array[1] = char array_element[10];
    ...

    void top_array or char top_array?

    How to define and declare such data structure?
    Or what should a be a stand way of doing this?

    Thanks,

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    char my_array[x][y];
    You can do as many layers as you want. Just add another [z].
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    9
    Thanks a lot for prompt reply, I could actually get it going.

    Also wondering if there is a way to define such data structure by using pointers. So I don't have to actually deal with individual basic elements one by one.

    For example,
    char *array = "AA";

    I'd like to have a big memory region reserved for N arrays (each 2 bytes "AA")

    char *top_array
    array1: top_array = array
    array2: top_array+2 = array

    thanks,

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Look up memcpy() or memset().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can refer to a row or a depth, of a multi-dimensional array of arrays, as if it were a pointer, whether you created it to be a pointer or not.

    Arrays and pointers are practically joined at the hip, in C. Say I had a two D array of names[][], and I wanted to read or assign something a new name. I don't have to do it char by char, instead I just:

    Code:
    names[i] = "Solomon";
    and that's fine. Note that to work with strings, they MUST have the end of string char, in C: '\0'.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    YSay I had a two D array of names[][], and I wanted to read or assign something a new name. I don't have to do it char by char, instead I just:

    Code:
    names[i] = "Solomon";
    and that's fine. Note that to work with strings, they MUST have the end of string char, in C: '\0'.
    No you can't.
    Code:
    int main( void )
    {
        char names[ 10 ][ 10 ];
        int x;
        for( x = 0; x < 10; x++ )
            names[ x ] = "Solomon"; /* error */
    
        return 0;
    }
    Now if you meant this:
    Code:
    int main( void )
    {
        char names[ 10 ][ 10 ] =
        {
            "Solomon",
            "Some",
            "Names",
            "Here",
        };
        return 0;
    }
    The only other thing you could do would be to have an array of pointers to characters:
    Code:
    char *names[ 10 ];
    int x = 4;
    
    names[ x ] = "Solomon";
    That's fine, but no, you cannot do what I've illustrated in the first example.


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

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That was a bad example. This is a good one:

    Code:
    #include <stdio.h>
    
    int main() {
      int i; 
      char *names1[6] = {"Al", "Adrian", "Alice", "Beau", "Bob", "Betty"};
      char names[6][10] = {{'\0'}};
      printf("\n\n");
    
    
      for(i = 0;i<6;i++) {
        printf("\n %s", names1[i]);
        strcpy(names[i], "Solomon");
      }
    
      printf("\n\n");
      for(i=0;i<6;i++) {
        printf("\n %s", names[i]);
      }
      printf("\n\n\t\t\t     press enter when ready");
    
      i = getchar(); 
      return 0;
    }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You still are copying one character at a time. That's what strcpy does. It just hides it in a function call. (Just being pedantic.)


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

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Right, strcpy() *is* dealing with the string, one char at a time, but the programmer, doesn't have to.

    Depending of course, on what your definition of dealing, and possibly is, is. < Shades of Bill Clinton>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Array of Character Arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:07 PM