LearnFrenzy
Sign in Take a free test

Arrays · Find the Error · Question 6

Error: 2D array parameter without the column size Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

The program below has a problem. Which statement correctly describes it?

main.c
#include <stdio.h>

int corner(int m[][], int rows) {
    return m[rows - 1][0];
}

int main(void) {
    int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
    printf("%d\n", corner(m, 2));
    return 0;
}
  • A rows must be the first parameter
  • B int m[][] – the number of columns must be given, e.g. int m[][3] Correct
  • C There is no error; it prints 4
  • D m must be passed as &m
Correct Answer: Option B

To find m[i][j] the compiler needs the row length, so only the first dimension can be left empty: int corner(int m[][3], int rows), or as a VLA parameter int corner(int rows, int cols, int m[rows][cols]).

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Popular