2d arrays

This is a discussion on 2d arrays within the C Programming forums, part of the General Programming Boards category; how do i delcare a 2 dimensional string array????...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    2d arrays

    how do i delcare a 2 dimensional string array????

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    char chArr[100][100];

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    In addition, this *might* help

    #define MAXCHARS 80 /* the chars per line */
    #define MAXROWS 3 /* the index elements 0-2*/

    fgets(chArr[index], MAXCHARS, stdin);

    don't forget to strip out the '\n' when using fgets

    length = strlen(chArr[index])-1;
    chArr[index][length] = '\0';

    or use strcpy

    strcpy(chArr[index], "some string");
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >how do i delcare a 2 dimensional string array????
    Maybe I'm just reading this wrong, but if a string is an array of char that is terminated by a null character '\0', then wouldn't a two-dimensional array of strings would be a three-dimensional array of char?
    Code:
    #include <stdio.h>
    
    #define ROWS    2
    #define COLS    4
    #define LENGTH 50
    
    /* one way */
    char a [ LENGTH ];
    char b [ COLS ] [ LENGTH ];
    char c [ ROWS ] [ COLS ] [ LENGTH ];
    
    /* another way */
    char d [ ] = "text";
    char e [ ] [ LENGTH ] =
    {
        "an", "array", "of", "text",
    };
    char f [ ] [ COLS ] [ LENGTH ] =
    {
        { "First",  "array", "of", "text" },
        { "Second", "array", "of", "text" },
    };
    
    /* yet another way */
    char g [ LENGTH ] = "text";
    char h [ COLS ] [ LENGTH ] =
    {
        "an", "array", "of", "text",
    };
    char i [ ROWS ] [ COLS ] [ LENGTH ] =
    {
        { "First",  "array", "of", "text" },
        { "Second", "array", "of", "text" },
    };
    
    #define ARRAYSIZE(x)    (sizeof(x)/sizeof(*(x)))
    
    int main(void)
    {
        int j,k;
        for(j = 0; j < ARRAYSIZE(i); ++j)
        {
            for(k = 0; k < ARRAYSIZE(*i); ++k)
            {
                printf("i[%d][%d] = \"%s\"\n", j, k, i [ j ] [ k ]);
            }
        }
        return 0;
    }
    
    /* my output
    i[0][0] = "First"
    i[0][1] = "array"
    i[0][2] = "of"
    i[0][3] = "text"
    i[1][0] = "Second"
    i[1][1] = "array"
    i[1][2] = "of"
    i[1][3] = "text"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    When I first read "array of strings" I was thinking of the string class in c++, but this is the c board... perhaps your response is what TOP wanted to know???
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    To be more exactly, a simple example:
    Code:
    const char sStrs[50][50] = {
        "Hello there, string 1",
        "Wtf, string 2 here?",
        "Multidemensional array, doh",
        "I love this sould",
        "C/C++ r0x",
    };

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by Vber
    To be more exactly, a simple example:
    Code:
    const char sStrs[50][50] = {
        "Hello there, string 1",
        "Wtf, string 2 here?",
        "Multidemensional array, doh",
        "I love this sould",
        "C/C++ r0x",
    };
    Hmmm. I see a two-dimensional array of chars; a one-dimensional array of strings.
    Code:
    #include <stdio.h>
    int main(void)
    {
       int i;
       for ( i = 0; i < 5; ++i )
       {
          puts(sStrs[i] /* where's the other dimension? */ );
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    what is this?
    Code:
    const char sStrs[50][50]

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    It is almost a declaration of an array of 50 arrays of 50 const chars.

    [edit]
    It could be used as an array of 50 read-only strings that are at most 50 characters in length.
    [/edit]
    Last edited by Dave_Sinkula; 03-15-2003 at 01:09 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Dave isn't this a multi-dimensional array? well in my book it is, if isn't, sorry I didn't know.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    char sStrs[50][50];
    >Dave isn't this a multi-dimensional array?

    Of characters, yes; of strings, no. Remember the original question was...

    >how do i delcare a 2 dimensional string array????


    All righty, one last try. Let's start with ints.
    Code:
    int x; /* a single int, not an array */
    int one[10]; /* one-dimensional array */
    int two[3][4]; /* two-dimensional array */
    Okay now let's take a look at a string.
    Code:
    char text[20]; /* a single string (or an array of 20 characters) */
    See how x differs from text? To be a string, you already need to be an array of char (and end with a '\0').

    So in order to be an array of strings, we have to add a second dimension.
    Code:
    char one[4][20]; /* array of 4 strings (or a 4x20 array of characters) */
    Then we would need to have a three-dimesional char array in order to have a two-dimensional (the first multi-dimensional) array of strings.
    Code:
    char two[2][4][20]; /* a 2x4 array of strings (or a 2x4x20 array of characters) */
    Would a typedef help?
    Code:
    typedef char str[20];
    
    str one[5] = /* array of 5 strings (up to 20 characters each) */
    { "one", "two", "three", "four", "five" };
    
    str two[2][4] = /* 2x4 array of strings (up to 20 characters each) */
    {
       { "one", "two", "three", "four" },
       { "five", "six", "seven", "eight" },
    };
    But then again, maybe the one-dimensional array of strings is what cnewbee wanted all along.
    Last edited by Dave_Sinkula; 03-15-2003 at 02:43 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Well I didn't mean to write that I'm using a multi-dimensional of strings, I was fooling with the text when I gave the example, I wrote multi-dimensional, not multi-dimensional of strings

    Btw, thanks for the explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 2d arrays
    By thamiz in forum C Programming
    Replies: 25
    Last Post: 05-25-2008, 05:06 AM
  2. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  3. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  4. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 08:05 PM
  5. how can i pass 2d arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2001, 06:33 AM

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