Pass a 2D-array to a function
suggest changePassing a 2D array to a function seems simple and obvious, and we happily write:
#include <stdio.h>
#include <stdlib.h>
#define ROWS 3
#define COLS 2
void fun1(int **, int, int);
int main(void)
{
int array_2D[ROWS][COLS] = { {1, 2}, {3, 4}, {5, 6} };
int n = ROWS;
int m = COLS;
fun1(array_2D, n, m);
return EXIT_SUCCESS;
}
void fun1(int **a, int n, int m)
{
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("array[%d][%d]=%d\n", i, j, a[i][j]);
}
}
}
But the compiler, here GCC in version 4.9.4 , does not appreciate it well.
$ gcc-4.9 -O3 -g3 -W -Wall -Wextra -std=c11 passarr.c -o passarr
passarr.c: In function ‘main’:
passarr.c:16:8: warning: passing argument 1 of ‘fun1’ from incompatible pointer type
fun1(array_2D, n, m);
^
passarr.c:8:6: note: expected ‘int **’ but argument is of type ‘int (*)[2]’
void fun1(int **, int, int);
The reasons for this are twofold: the main problem is that arrays are not pointers and the second inconvenience is the so called pointer decay. Passing an array to a function will decay the array to a pointer to the first element of the array–in the case of a 2D array it decays to a pointer to the first row, because C stores arrays in row-major order.
#include <stdio.h>
#include <stdlib.h>
#define ROWS 3
#define COLS 2
void fun1(int (*)[COLS], int, int);
int main(void)
{
int array_2D[ROWS][COLS] = { {1, 2}, {3, 4}, {5, 6} };
int n = ROWS;
int m = COLS;
fun1(array_2D, n, m);
return EXIT_SUCCESS;
}
void fun1(int (*a)[COLS], int n, int m)
{
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("array[%d][%d]=%d\n", i, j, a[i][j]);
}
}
}
It is necessary to pass the number of rows, they cannot be computed.
#include <stdio.h>
#include <stdlib.h>
#define ROWS 3
#define COLS 2
void fun1(int (*)[COLS], int);
int main(void)
{
int array_2D[ROWS][COLS] = { {1, 2}, {3, 4}, {5, 6} };
int rows = ROWS;
/* works here because array_2d is still in scope and still an array */
printf("MAIN: %zu\n",sizeof(array_2D)/sizeof(array_2D[0]));
fun1(array_2D, rows);
return EXIT_SUCCESS;
}
void fun1(int (*a)[COLS], int rows)
{
int i, j;
int n, m;
n = rows;
/* Works, because that information is passed (as "COLS").
It is also redundant because that value is known at compile time (in "COLS"). */
m = (int) (sizeof(a[0])/sizeof(a[0][0]));
/* Does not work here because the "decay" in "pointer decay" is meant
literally--information is lost. */
printf("FUN1: %zu\n",sizeof(a)/sizeof(a[0]));
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("array[%d][%d]=%d\n", i, j, a[i][j]);
}
}
}
The number of columns is predefined and hence fixed at compile time. C99 added variable-length arrays. C11 made them optional, and later standards keep that choice. See Multidimensional arrays of variable size. Support is common but not universal; a zero or negative bound from atoi is not a valid array size, and these examples omit that check.
#include <stdio.h>
#include <stdlib.h>
/* ALL CHECKS OMITTED! */
void fun1(int (*)[], int rows, int cols);
int main(int argc, char **argv)
{
int rows, cols, i, j;
if(argc != 3){
fprintf(stderr, "Usage: %s rows cols\n", argc > 0 ? argv[0] : "program");
exit(EXIT_FAILURE);
}
rows = atoi(argv[1]);
cols = atoi(argv[2]);
int array_2D[rows][cols];
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
array_2D[i][j] = (i + 1) * (j + 1);
printf("array[%d][%d]=%d\n", i, j, array_2D[i][j]);
}
}
fun1(array_2D, rows, cols);
exit(EXIT_SUCCESS);
}
void fun1(int (*a)[], int rows, int cols)
{
int i, j;
int n, m;
n = rows;
/* Does not work anymore, no sizes are specified anymore
m = (int) (sizeof(a[0])/sizeof(a[0][0])); */
m = cols;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("array[%d][%d]=%d\n", i, j, a[i][j]);
}
}
}
This does not work, the compiler complains:
$ gcc-4.9 -O3 -g3 -W -Wall -Wextra -std=c99 passarr.c -o passarr
passarr.c: In function ‘fun1’:
passarr.c:168:7: error: invalid use of array with unspecified bounds
printf("array[%d][%d]=%d\n", i, j, a[i][j]);
It becomes a bit clearer if we intentionally make an error in the call of the function by changing the declaration to void fun1(int **a, int rows, int cols). That causes the compiler to complain in a different, but equally nebulous way
$ gcc-4.9 -O3 -g3 -W -Wall -Wextra -std=c99 passarr.c -o passarr
passarr.c: In function ‘main’:
passarr.c:208:8: warning: passing argument 1 of ‘fun1’ from incompatible pointer type
fun1(array_2D, rows, cols);
^
passarr.c:185:6: note: expected ‘int **’ but argument is of type ‘int (*)[(sizetype)(cols)]’
void fun1(int **, int rows, int cols);
We can react in several ways. One of them is to ignore the diagnostics and do some illegible pointer juggling:
#include <stdio.h>
#include <stdlib.h>
/* ALL CHECKS OMITTED! */
void fun1(int (*)[], int rows, int cols);
int main(int argc, char **argv)
{
int rows, cols, i, j;
if(argc != 3){
fprintf(stderr, "Usage: %s rows cols\n", argc > 0 ? argv[0] : "program");
exit(EXIT_FAILURE);
}
rows = atoi(argv[1]);
cols = atoi(argv[2]);
int array_2D[rows][cols];
printf("Make array with %d rows and %d columns\n", rows, cols);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
array_2D[i][j] = i * cols + j;
printf("array[%d][%d]=%d\n", i, j, array_2D[i][j]);
}
}
fun1(array_2D, rows, cols);
exit(EXIT_SUCCESS);
}
void fun1(int (*a)[], int rows, int cols)
{
int i, j;
int n, m;
n = rows;
m = cols;
printf("\nPrint array with %d rows and %d columns\n", rows, cols);
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("array[%d][%d]=%d\n", i, j, *( (*a) + (i * cols + j)));
}
}
}
Or we do it right and pass the needed information to fun1. To do so we need to rearrange the arguments to fun1: the size of the column must come before the declaration of the array. To keep it more readable the variable holding the number of rows has changed its place, too, and is first now.
#include <stdio.h>
#include <stdlib.h>
/* ALL CHECKS OMITTED! */
void fun1(int rows, int cols, int (*a)[cols]);
int main(int argc, char **argv)
{
int rows, cols, i, j;
if(argc != 3){
fprintf(stderr, "Usage: %s rows cols\n", argc > 0 ? argv[0] : "program");
exit(EXIT_FAILURE);
}
rows = atoi(argv[1]);
cols = atoi(argv[2]);
int array_2D[rows][cols];
printf("Make array with %d rows and %d columns\n", rows, cols);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
array_2D[i][j] = i * cols + j;
printf("array[%d][%d]=%d\n", i, j, array_2D[i][j]);
}
}
fun1(rows, cols, array_2D);
exit(EXIT_SUCCESS);
}
void fun1(int rows, int cols, int (*a)[cols])
{
int i, j;
int n, m;
n = rows;
m = cols;
printf("\nPrint array with %d rows and %d columns\n", rows, cols);
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("array[%d][%d]=%d\n", i, j, a[i][j]);
}
}
}
This looks awkward to some people, who hold the opinion that the order of variables should not matter. That is not much of a problem, just declare a pointer and let it point to the array.
#include <stdio.h>
#include <stdlib.h>
/* ALL CHECKS OMITTED! */
void fun1(int rows, int cols, int **);
int main(int argc, char **argv)
{
int rows, cols, i, j;
if(argc != 3){
fprintf(stderr, "Usage: %s rows cols\n", argc > 0 ? argv[0] : "program");
exit(EXIT_FAILURE);
}
rows = atoi(argv[1]);
cols = atoi(argv[2]);
int array_2D[rows][cols];
printf("Make array with %d rows and %d columns\n", rows, cols);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
array_2D[i][j] = i * cols + j;
printf("array[%d][%d]=%d\n", i, j, array_2D[i][j]);
}
}
// a "rows" number of pointers to "int". Again a VLA
int *a[rows];
// initialize them to point to the individual rows
for (i = 0; i < rows; i++) {
a[i] = array_2D[i];
}
fun1(rows, cols, a);
exit(EXIT_SUCCESS);
}
void fun1(int rows, int cols, int **a)
{
int i, j;
int n, m;
n = rows;
m = cols;
printf("\nPrint array with %d rows and %d columns\n", rows, cols);
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("array[%d][%d]=%d\n", i, j, a[i][j]);
}
}
}