0

I was trying to pass array of arrays to a function, but I am not getting correct results. I looked at [this SO post][1] for this purpose. I am not getting any warnings and I am using TDM GCC 64 compiler. What could be wrong with this code?

#include <stdio.h>
#include<inttypes.h>

void printArrayOfArrays(uint8_t numRows,uint8_t numColumns,int arraOfArray[][numColumns]);


int main(void)
{
    int numRows, numColumns, rowCount, colCount, first[5][5];

    printf("Enter the number of rows and columns of matrix\n");
    scanf("%d%d", &numRows, &numColumns);

    printf("Enter the elements of first matrix\n");

    for (rowCount = 0; rowCount < numRows; rowCount++)
    {
        for (colCount = 0; colCount < numColumns; colCount++)
        {
            scanf("%d", &first[rowCount][colCount]);
        }
    }

    for (rowCount = 0; rowCount < numRows; rowCount++)
    {
        for (colCount = 0 ; colCount < numColumns; colCount++)
        {
            printf("%d\t",first[rowCount][colCount]);
        }
        printf("\n\n");
    }

    printf("\n");

    printArrayOfArrays(numRows,numColumns,first);

    return(0);
}

void printArrayOfArrays(uint8_t numRows,uint8_t numColumns,int arrayOfArray[numRows][numColumns])
{
    for (uint8_t rowCount = 0; rowCount < numRows; rowCount++)
    {
        for (uint8_t colCount = 0 ; colCount < numColumns; colCount++)
        {
            printf("%d\t", *(*(arrayOfArray+rowCount)+colCount));
        }
        printf("\n");
    }
}

The result I am getting for this is

Enter the number of rows and columns of matrix
2
2
Enter the elements of first matrix
4
3
1
6
4       3

1       6

4       3
0       0
M Oehm
  • 28,726
  • 3
  • 31
  • 42
Rajesh
  • 1,085
  • 1
  • 12
  • 25
  • Why are you using pointer arithmetic inside the function call? IMHO It's not a great practice to mix usages of the array access syntax and pointer arithmetic on the same structures. – Zefira Jun 21 '19 at 08:34
  • I agree, I am just a learner I was learning with various methods to see the behavior. – Rajesh Jun 21 '19 at 12:50

2 Answers2

3

Your array definitions of first in main and of arrayOfArrays in printArrayOfArrays are not consistent: first is a 5×5 array, even if you don't use or initialise all elements; arrayOfArrays is a variable length array, in your example 2×2.

So you should either make first a variable length array, too:

printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &numRows, &numColumns);

int first[numRows][numColumns];

(Make sure that you define it only after numRows and numColumns have meaningful values.)

Or you could make the parameter to the function a fixed size array of 5×5:

void printArrayOfArrays(uint8_t numRows, uint8_t numColumns,
    int arrayOfArray[5][5]);

(In this case, you must ensure that numRows and numColumns don't exceed 5.)

M Oehm
  • 28,726
  • 3
  • 31
  • 42
  • Beat me to it with this, but they'll also need to fix up the print statement in `printArrayOfArrays`... Should be just `printf("%d\t", arrayOfArray[rowCount][colCount]);` – idz Jun 21 '19 at 08:37
  • 1
    @idz: The `*(*(a + row) + col)` syntax is okay, but convoluted. (It still should be changed for readability's sake.) – M Oehm Jun 21 '19 at 08:41
  • Yup, perhaps "need" was a little too strong... "should" or "ought" would have been better. My bad! – idz Jun 21 '19 at 08:52
  • Why compiler does not warn? How can we write bug free code in C? Is this not a compiler issue? I am a beginner in C and I do not think I will be able to notice such dangerous issues. So when we pass array of arrays to a function, we may not be able to generalize the function and function should probably cater for maximum possible parameters by statically defining fixed length array inside the function? On the other hand, I may have a array of arrays of nxm dimension, but I may not use all the rows and columns. Same array I may use to store different number of row count and column count. – Rajesh Jun 21 '19 at 13:07
0

Did you try to access those value with an other syntax such as :

 printf("%d\t", arrayOfArray[numRow][numColumn]));

It is the obvious way for me, but maybe i didn't understood the question