-
help with arrays
help me with the following codes, I am doing an example out of a book, but I can't do it. I need your help to learn how to do it.
#include <stdio.h>
main()/* I am trying to print out all the arrays*/
{
char list_ch[][2]={'1','a',
'2','b',
'3', 'c',
'4', 'd',
'5', 'e',
'6', 'f'};
int x, i;
int j;
for (i=0; i<6; i++);
for ( j=0; j<1; j++);
printf("%6d\n", list_ch[i][j]);// should I leave list_ch empty
//meaning list_ch[]?
while (x!='e')// to exit program properly
x=getchar();
return 0;
}
// I want to print the arrays declared by list_ch, but I don't know how to do it, help...........
-
Code:
char multiarray[][2] =
{
{ 'a', 'b' },
{ 'c', 'd' },
{ 'e','f' },
{ 'g','h' }
};
This is how you declare a multidimensional array and assign it values at the time of creation.
Quzah.
-
printing
ok but how would I go about printing them?
-
How do you _think_ you'd go about printing them? Given that there are N number of rows, each containing 2 characters, you could just simply use a for loop.
for(x=0;x<N;x++) printf("%c %c", array[x][0],[array[x][1] );
If you have an array who's first member's value is not given:
array[][3]
It means there are an undetermined number of rows, each containing (in this case, 3) a set number of other 'cells'.
The only way you can use [] is when you define the number of entries (provide all values) at creation:
char array[][2] = { {'1','2'}, {'2','a'}, {5,3} };
Quzah.