how do i delcare a 2 dimensional string array????
how do i delcare a 2 dimensional string array????
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
>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.*
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
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.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", };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.*
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 02: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.*
Dave isn't this a multi-dimensional array? well in my book it is, if isn't, sorry I didn't know.
>Dave isn't this a multi-dimensional array?Code:char sStrs[50][50];
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.Okay now let's take a look at a string.Code:int x; /* a single int, not an array */ int one[10]; /* one-dimensional array */ int two[3][4]; /* two-dimensional array */See how x differs from text? To be a string, you already need to be an array of char (and end with a '\0').Code:char text[20]; /* a single string (or an array of 20 characters) */
So in order to be an array of strings, we have to add a second dimension.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 one[4][20]; /* array of 4 strings (or a 4x20 array of characters) */Would a typedef help?Code:char two[2][4][20]; /* a 2x4 array of strings (or a 2x4x20 array of characters) */But then again, maybe the one-dimensional array of strings is what cnewbee wanted all along.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" }, };
Last edited by Dave_Sinkula; 03-15-2003 at 03: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.*
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.