1

I have:

void displayMatrix(int a[][]){

    int howManyRows = sizeof(a)/sizeof(a[0]);
    int howManyColumns = sizeof(a)/sizeof(int);

    int r, c;

    for (r = 0; r < howManyRows; r++){
            if (r >= 1){
                printf("\n");
            }
        for (c = 0; c < howManyColumns; c++){
            printf("%d ",a[r][c]);
        }
    }


}

but when I create an array

int samp[4][5] = {
    {1,2,3,4,5},
    {6,7,8,9,10},
    {11,12,13,14,15},
    {16,17,18,19,20}
    };

and pass it to the function in main, nothing shows up on the screen. What is going on?

AviG
  • 362
  • 2
  • 14
  • Why not just print the "\n" unconditionally after the column loop instead of before? Then verify that howManyRows and howMany columns actually contains the correct values – visibleman Sep 20 '18 at 02:06
  • You can't use sizeof like that on an array passed to a function. – Retired Ninja Sep 20 '18 at 02:08
  • Possible duplicate of [How to find the 'sizeof' (a pointer pointing to an array)?](https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array) – Retired Ninja Sep 20 '18 at 02:09
  • @RetiredNinja why can't I? I did just edit the function and now it does work. – AviG Sep 20 '18 at 02:10
  • If it somehow works it is only because the compiler may be inclining the function call or something similar. This should never work. – Roflcopter4 Sep 20 '18 at 02:14
  • You'd have to show how you "fixed" it. https://ideone.com/7WufmX – Retired Ninja Sep 20 '18 at 02:14

2 Answers2

2

This happens because in function parameters, arrays decay to pointers on the top level. So your function

void displayMatrix(int a[4][5])

is no different from

void displayMatrix(int (*a)[5])

Regarding N1570, Section 6.7.6.3, Paragraph 7:

A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, ...

This results in an unexpected behavior:

int howManyRows = sizeof(a)/sizeof(a[0]);

Where sizeof(a) == sizeof(void*) and sizeof(a[0]) == 5*sizeof(int)

Usually the length of 5 integers is longer than a pointer, so you get howManyRows = 0 after all.

Try hard-coding # of rows or pass it by an extra parameter:

void displayMatrix(int a[][5], int rows)
iBug
  • 35,554
  • 7
  • 89
  • 134
  • is there anyway to make this function work with just the parameters I've speicifed? – AviG Sep 20 '18 at 02:14
  • No. You must send the size of the array separately. – Roflcopter4 Sep 20 '18 at 02:15
  • @AviG According to the standard, not quite simple. The top level array will always decay to a pointer, so you have to either pass the length of the top-level array, or make it non-top-level like `int (*a)[4][5]`. – iBug Sep 20 '18 at 02:15
  • Well with @AviG , considering your original function definition int a[4][5] why would you need to calculate row and col? They are already hardcoded in your function def, so you might as well hardcode them in the loop as well.? – visibleman Sep 20 '18 at 02:16
1

This is a classic gotcha of C programming. You have to understand what an array in C really is and how it works to understand why this code will not work.

When you create an "array" as such of size x, the compiler sets aside enough memory to hold x elements of some some object. This is really just a lump of memory with nothing otherwise special about it. If the array is one of 4 integers, each 4 bytes in size, then the compiler sets aside 4 * 4 bytes (16). The variable that contains the array is in fact nothing more than a pointer to the start of the array. Accessing the array is a simple matter of going to the start, and moving forward over the required number of bytes to find the object specified.

That said, arrays do have a special property within the function they are declared. They are known as arrays and their total size is stored, and can be accessed by the sizeof operator. This property is not valid anywhere outside of that function. If you pass the array as a parameter, all that is passed is the pointer and not any information about the array. There is no way to get its size. Your code here will never work and cannot be made to work.

Roflcopter4
  • 679
  • 6
  • 16