Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8099279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:21:23+00:00 2026-06-05T22:21:23+00:00

Real noob question no doubt, but I can’t get my head around this behaviour.

  • 0

Real noob question no doubt, but I can’t get my head around this behaviour. I initialise a 2-d array, then call a function to print out certain values from it. The program and result are shown below:

int
main(void)
{
    int matrix[3][5] = 
    {
        {14,7,6,55,2},
        {8,33,12,88,24},
        {19,20,21,90,7}
    };

    printArray(3, 5, matrix);

    printArray(2, 3, matrix);

    return 0;
}

void
printArray(int rows, int columns, int matrix[rows][columns])
{
    int i, j;



    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < columns; j++)
            printf("%3i ", matrix[i][j]);

        printf("\n");
    }   



    printf("\n");       
}



 14   7   6  55   2 
 8  33  12  88  24 
 19  20  21  90   7 

 14   7   6 
 55   2   8 

It seems that the second time printArray() is called it continues printing values from the first row on a new line rather than actually accessing the next row. I don’t understand this as surely the index values are constant aren’t they? (ie matrix[1][2] should be (in this case) 12, not 8.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:21:24+00:00Added an answer on June 5, 2026 at 10:21 pm

    The following:

    void printArray(int rows, int columns, int matrix[rows][columns])
    

    promises the compiler that the third argument has the dimensions rowsxcolumns.

    At the same time, the following line breaks this promise:

    printArray(2, 3, matrix);
    

    since matrix is 3x5 and not 2x3.

    If you only wish to print a subset of rows and columns, you could do something like:

    void
    printArray(int rows, int columns, int matrix[rows][columns], int max_row, int max_col)
    {
        int i, j;
    
        for (i = 0; i < max_row; i++)
        {
            for (j = 0; j < max_col; j++)
                printf("%3i ", matrix[i][j]);
            printf("\n");
        }           
        printf("\n");       
    }
    
    int main(void)
    {
       ...
       printArray(3, 5, matrix, 3, 5);
       printArray(3, 5, matrix, 2, 3);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like a real noob posting this, but I can't seem to find
This surely is a noob question, but I can't find an answer in Doxygen
A real F# noob question, but what is |> called and what does it
Very noob question, I know, but I didn't manage to handle real-time slider move
This is a real noob question. I work for a company that makes 100's
This is a very general and noob question, but I am a noob with
I'm a real noob to encryption. After breaking my head over this for a
This is a real noob question. When I set up JAVA_HOME using the command
I feel like a real noob asking this, but here's my problem: I want
This is going to be a real noob question, so please have mercy. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.