Function for printing 2D arrays of uknown size in C

passing 2d array to function in c
pass 2d array to function c without size
how to return 2d array in c
function to print 2d array c++
passing 2d array to function c++
passing 3d array to function in c
2d vector c++
c 2d array unknown size

I'm new to C programming and I've run into a problem when creating 2D array printing function. When I try to execute the code below I get:

points.c:13: error: unknown array element size

As I've checked there are very similar codes online, which are supposed to work. I've tried to initialize function as

int print2DArray( int arrayLen, int elementLen, int array[arrayLen][elementLen])

but it raises:

points.c:3: error: 'arrayLen' undeclared

Could somebody tell me what's wrong with this code and how to fix it? I also don't understand why very similar function for 1D arrays works just fine. It has to be in pure C.

#include <stdio.h>
//supposed to print 2D array:
int print2DArray(int array[][], int arrayLen, int elementLen)
{
    int i;
    int j;

    for (i = 0; i < arrayLen; i++)
    {
        for (j=0; j < elementLen; j++)
        {
            printf("%5d", array[i][j]);
        }
        printf("\n");
    }
}

//prints 1D array:

int printArray( int array[], int arrayLen)
{
    int i;
    for (i = 0; i < arrayLen; i++)
    {
        printf("%d", array[i]);
    }
}

--- edit ---

I undestand most of you pointed out that the function has to be called like that:

#include <stdio.h>

int print2DArray( int arrayLen, int elementLen, int array[arrayLen][elementLen])
{
    int i;
    int j;

    for (i = 0; i < arrayLen; i++)
    {
        for (j=0; j < elementLen; j++)
        {
            printf("%5d", array[i][j]);
        }
        printf("\n");
    }
}

This raises an error:

points.c:3: error: 'arrayLen' undeclared

I'm using tcc for windows and according to documentation it is supposed to support C99 VLA.

It appears OP's compiler (or the mode it is used) does not support variable length array (VLA) as a function parameter.

Below is a non-VLA approach.

void print2DArrayX(int arrayLen, int elementLen, const int *array) {
  int i;
  int j;

  for (i = 0; i < arrayLen; i++) {
    for (j = 0; j < elementLen; j++) {
      printf("%5d", array[i*elementLen + j]);
    }
    printf("\n");
  }
}

Call with address of first int, not the 2D array

#define ARRAY_LEN 3
#define ELEMENT_LEN 4
int array[ARRAY_LEN][ELEMENT_LEN] = { 0 };
...
print2DArrayX(ARRAY_LEN, ELEMENT_LEN, array[0]);

Best way to pass a 2d array to functions which size is unknown at , As long as the "effective type" is an int array of the specified size. and not a whole VLA passed by value - we can't pass arrays by value in C. void fill(size_t rows, size_t cols, int arr[rows][cols], int val); void print(size_t rows,� This post is an extension of How to dynamically allocate a 2D array in C? A one dimensional array can be easily passed as a pointer, but syntax for passing a 2D array to a function can be difficult to remember. One important thing for passing multidimensional arrays is, first array dimension does not have to be specified.

Ok, so thanks for all the answers - they were very helpful. I've just tried to use gcc in linux and as you've pointed out this approach works fine:

int print2DArray( int arrayLen, int elementLen, int array[arrayLen][elementLen])

I guess tcc (tiny c compiler, windows version 0.9.27) doesn't support VLA after all. A bit strange since documentation says it does.

c, can be variable length) and int **array (worth a try). Any idea's? Thanks. c 2D array arguments need a size for all but the first dimension. or the function has to take different sized 2D arrays, you can use a pointer to a pointer instead. void somefunction(int *array) { printf("%d\n", *(array+0)); /* will print� Passing two dimensional array to a C++ function. Specify the size of columns of 2D array. Previous Page Print Page. Next Page .

How about you try this solution.

#include <stdio.h>


int print2DArray(int* array, int arrayLen, int elementLen)
{
    int i;
    int j;

    for (i = 0; i < arrayLen; i++)
    {
        for (j=0; j < elementLen; j++)
        {
            printf("%5d ", *(array+j+elementLen*i));
        }
        printf("\n");
    }

}

int main(){
    int arr[2][6] = {   {9,258,9,96,-8,5},
                        {1,1212,-3,45,27,-6}
                      };
    print2DArray(*arr,2,6);
    return 0;
}

2D Array as Parameter w/ Unknown Size, though wouldn't you want this function to be a method of your matrix class, and have it print the internal array? Jan 24, 2012 at 5:08pm. C Programming - Passing a multi-dimensional array to a function Posted on March 27, 2019 by Paul . In this article I will show you how to pass a multi-dimensional array as a parameter to a function in C. For simplicity, we will present only the case of 2D arrays, but same considerations will apply to a general, multi-dimensional, array.

Unless you are using a C99 compiler,

int print2DArray( int arrayLen, int elementLen, int array[arrayLen][elementLen])

is not possible.

Even if you are using C99 compiler, your code has a problem. You need to pass one of the dimension first.

int print2DArray(int arrayLen, int elementLen, int arr[][elementLen]);

So,

int print2DArray(int arrayLen, int elementLen, int arr[][elementLen]) 
{
    // Your code
    int i;
    int j;

    for (i = 0; i < arrayLen; i++)
    {
        for (j=0; j < elementLen; j++)
        {
            printf("%5d", array[i][j]);
        }
        printf("\n");
    }

    return 0;
}

This can be used as

int main(void) 
{ 
    int i32Array[3][3] = {{-15, 4, 36}, {45, 55, 12}, {-89, 568, -44568}}; 
    int m = 3, n = 3; 
    // I am not sure why 'print2DArray' would return an int 
    // (or anything at all for that matter). 
    // If you can establish a case for it, 
    // modify the function and the value it is supposed to return,
    // And catch it below.

    print2DArray(m, n, i32Array); 

    return 0; 
}

I am not sure how you are calling print2DArray function. Unless you post that piece of code, it is difficult to resolve your problem. Confirm that you are calling the function correctly as shown above.

How to pass a 2D array as a parameter in C?, One important thing for passing multidimensional arrays is, first array dimension does int main(). {. int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};. print(arr); From C99, C language supports variable sized arrays to be passed simply by In this method, we must typecast the 2D array when passing to function. In the previous post, we have discussed how to dynamically allocate memory for 2D array. In this post, we will see how we can pass 2D array to a function in C programming language. 1. For Static Array. If we know the array bounds at compile time, we can pass a static 2D array to a function in C as shown below.

declaring a multidimensional array with unknown size, declaring a multidimensional array with unknown size - c++. C++ passing Dynamically-sized 2D Array to function an array and then initialize them and get updated their value and get them printed but not getting the expected output. You cannot safely pass a dynamically allocated 2D array in C++ into a function because you always have to know at least one dimension at compile time. I could point over at Passing a 2D array to a C++ function because that looks like a good duplicate. I won't because it's referring to statically allocated arrays.

Passing a 2D array to a C++ function using reference, #include<bits/stdc++.h> using namespace std; int r, c=0; void print(int a[][4]) { for( size_t i=0 How to pass two dimensional array of an unknown size to a function. functions allowed and should be included in the code: string functions - strlen(),strcpy(), strcat(), strchr(), strcmp(),strstr() must use 2d array. must use fgets for words. Out put must match the exact format.

[PDF] Two-Dimensional Arrays, spreadsheet, which need a two-dimensional array. parameter, how do you determine the size of the array? 7. Hint: • Consider a variable words, a 1D array of� In this post, we will see how to pass 2D array as a function parameter in C++ programming language. If we know the array dimensions at compile time, we can pass the array by reference using a function template in C++. The advantage of using this approach is that there is no need to specify the array dimensions.

Comments
  • You say you have written the function as int print2DArray( int arrayLen, int elementLen, int array[arrayLen][elementLen]). But you haven't. Instead, you have done int print2DArray(int array[][], int arrayLen, int elementLen) which is something completely different (and invalid).
  • int array[][] isn't a valid argument, so forget that. And before this goes on, you've already determined your compiler does support VLAs (variable-length-arrays) ? It must, or your int print2DArray( int arrayLen, int elementLen, int array[arrayLen][elementLen]) stands no chance of working.
  • Post the code that causes "I've tried to initialize function as int print2DArray( int arrayLen, int elementLen, int array[arrayLen][elementLen])". I suspect you have a transcription error.
  • @Peter I know, I've meant that I've tried both methods.
  • ChrisM "I'm using tcc for windows and according to documentation it is supposed to support C99 VLA" --> The compiler appears to be in a mode that simply does not accept VLA. Maybe the compiler options are set for pre-C89, no VLA or "support C99" is only "support most of C99".
  • Doesn't this approach cause undefined behavior of the type mentioned in Annex J.2: "An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a[1][7] given the declaration int a[4][5]) (6.5.6)" and discussed here?
  • @exnihilo Interesting: I would say it is not UB given const int *array does not carry limitations of the 2 dimensions of int array[ARRAY_LEN][ELEMENT_LEN], only that the overall indexing is limited by ARRAY_LEN*ELEMENT_LEN and no aliasing in print2DArrayX().
  • "A bit strange since documentation says it does." --> best to post the errant tcc quote and the version of tcc.
  • Do you mean this doc: bellard.org/tcc/tcc-doc.html#Clang ? I haven't found any way to specify a particular standard, though.
  • @Bob__ yes, this little document. Since tcc 'help' doesn't show any options for specifying a standard I'd assume that VLA is 'on' by default. To be honest at least I found out that VLA isn't part of ANSI C standard. I have to stay within ANSI C, so it's not a real problem for me.
  • Not to open that can of worms, but even if it's common to refer to C89 as ANSI C, the current ANSI C standard is C18.