Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    two dimensional arrays in C

    hey y'all!
    so i was working on this little program, and got to the point where i have to let the user enter integers from the keyboard to fill in a 3x3 matrix. I declared my 3x3 matrix as a a two dimensional array thus:

    int matrix[3][3];

    and also

    int i, j; //as counters

    to fill this array i did:

    for(i=0; i<3; i++) //rows of the matrix
    {
    for(j=0; j<3; j++) //columns of the matrix
    {
    printf("Enter a number to fill matrix [%d][%d]: ", i, j);
    scanf("%d", &matrix[i][j]);

    }

    }



    but the program won't compile!! I get an error message for the line with "scanf" saying "invalid binary operator"
    is that an improper way to fill a two dimensional array from the keyboard during run time?
    Please somebody help! I'm very confused here
    Thanks a lot!

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe:
    Code:
    scanf("%d", &(matrix[i][j]));
    shmoove

    Edit:
    Looking at the operator precedence chart it shouldn't matter since '&' has lower precedence that '[]', so that's probably not it.

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What compiler and OS are you using? It compiles just fine on MSVC.

  • #4
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here are two ways:

    printf("Enter a number to fill matrix [%d][%d]");
    scanf("%d",&i,&j);

    or
    printf("Enter a number to fill matrix [%d][%d]: ", i, j);

    i think now you won't be confused


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •