0

sorry for bothering you again, but I'm getting an error that I am not quite sure how to solve. I'm trying to make a function that prints a matrix with a set value for both ROW and COLUMN. However, when I use ROW or COLUMN inside the "for" to print the matrix, the compiler sends me this error:

error: lvalue required as left operand of assignment

I've tried reading other similar questions and trying to find out what exactly the error means in this case but I can't seem to do so.

So my question is, what does that error mean and how do I fix it?

Thank you

#include <stdio.h>
#include <stdlib.h>

#define ROW 3
#define COLUMN 4
#define SS1 4
#define SS2 1
#define SORTED1 8
#define SORTED2 1
#define SORTED3 5



void matrixPrint (int m[ROW][COLUMN]){
        for (ROW = 0; ROW < 3; ++ROW){
                for (COLUMN = 0; COLUMN < 4; ++COLUMN){
                    printf("%4d", m[ROW][COLUMN]);
        }
        printf ("\n");
    }
}

2 Answers2

5

Preprocessor's #define creates a textual substitution, not a variable. After preprocessing, your program looks like this:

for (3 = 0; 3 < 3; ++3){
    for (4 = 0; 4 < 4; ++4){
        printf("%4d", m[3][4]);
    }
    printf ("\n");
}

This is obviously invalid: you cannot assign values to constants. In C-speak, an expression to which you can assign is called lvalue (a value that can appear on the left in an assignment). That should explain the error that you see.

To fix the error, introduce two loop variables, call them row and column, and use them instead of ROW and COLUMN:

for (int row = 0; row < ROW ; ++row){
    for (int column = 0; column < COLUMN; ++column){
        printf("%4d", m[row][column]);
    }
    printf ("\n");
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

fix to like this

void matrixPrint (int m[ROW][COLUMN]){
    int row, column;
    for (row = 0; row < ROW; ++row){
        for (column = 0; column < COLUMN; ++column){
            printf("%4d", m[row][column]);
        }
        printf ("\n");
    }
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70