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. 2d arrays

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

2. char chArr[100][100];

3. 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");

4. >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"
*/```

5. 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???

6. 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. 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;
}```

8. what is this?
Code:
`const char sStrs[50][50]`

9. It is almost a declaration of an array of 50 arrays of 50 const chars.

It could be used as an array of 50 read-only strings that are at most 50 characters in length.
[/edit]

10. Dave isn't this a multi-dimensional array? well in my book it is, if isn't, sorry I didn't know.

11. 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????

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.

12. 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.