How does multi dimensional array in C work -
i creating c program tables in using multi dimensional arrays used store 2 letters , number follows:
int main() { char x[5][3]; int y[5]; (i=0; i<5; i++) { printf('enter 2 characters x'); scanf('%s', x[i]); printf('enter number y'); scanf('%d', &y[i]); } }
what don't understand here in for
loop. example, iteration i=0
, if user enters 2 characters a
,b
these letters stored in memory location of x
? mean @ position x[0][0]
or x[0][1]
or other location?
i know might silly question confused here. thank in advance.
the thing here have two-dimensional array of char meaning in 1 memory location cannot store more 1 single character. stored in position 0 , 1. think approach not correct.
in case either want store char* array like:
char* tab[5] ;
and you'll need allocate memory each line with:
tab[i] = (char *) malloc ( 2 * sizeof(char));
or can keep array , instead (if want sure of what's happening in memory):
for (i=0; i<5; i++) { printf("enter first character x"); scanf("%c", &x[i][0]); printf('enter second character x'); scanf("%c", &x[i][1]); }
hope helps
Comments
Post a Comment