Question

I'm trying to solve a problem in C.

It requires me to register some chars and numbers in a matrix A[n][n] and then print them. It must be something like this: A[3][3]:

a D g 2
T 1 c v
G J k L
N 6 f Z

I checked the web and... no solution. I only find arrays of chars.

int n = 3;
char matrixA[n][n];

int i, j;
    for(i=0; i<n; i++){
        for(j=0; j<n; j++){
            scanf("%c ", &matrixA[i][j]);
        }
    }

what I get is an error:

subscripted value is neither array nor pointer nor vector

Was it helpful?

Solution

I like to do it this way :

int n = 4;
char matrixA[n][n];
int i,j;

for(i = 0;i < n ;i++){
  for(j = 0;j < n;j++){
    fflush(stdin);
    matrixA[i][j] = getche();
    printf(" ");
   }
  printf("\n");
} 

**Note that,you won't have to hit return after typing the character when you'll input.And the input will be row by row and with spaces between the characters.So,It'll give you a clear idea which value is going to which position of the matrix.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top