0
#include<stdio.h>   
#include<stdlib.h>
#include<string.h>

void find_col_num(int matrix[][5])
{
    printf("size of matrix = %d\n",sizeof(matrix));
    printf("size of one element = %d\n", sizeof(matrix[0][0]));
    printf("number of columns = %d\n", sizeof(matrix)/(sizeof(matrix[0][0])*5));
}

int main()
{
    int a[6][5] = { {0,0,0,0,0},
                    {0,1,1,1,1},
                    {1,0,1,0,0},
                    {1,0,1,0,0},
                    {1,0,1,0,0},
                    {0,1,1,1,1} };

    find_col_num(a);

    printf("\n\n\n");

    printf("size of a = %d\n",sizeof(a));
    printf("size of one element = %d\n", sizeof(a[0][0]));
    printf("number of columns = %d\n", sizeof(a)/(sizeof(a[0][0])*5));

    return 0;
}

Output:

size of matrix = 8
size of one element = 4
number of columns = 0



size of a = 120
size of one element = 4
number of columns = 6

I want to use sizeof in the void or int function for determine number of the matrix's column. I know that I have to specify number of the matrix's line in the function. How can I handle that?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
  • 2
    The function `find_col_num` has *no way* to know the size of its argument. You have to pass that as another argument unless it is known globally. In fact it can be *any size* provided you don't exceed the array bounds. There is no size for the outer dimension given, and even if there were, that is not restrictive. The only reason for the one dimension given is so that the compiler knows *how* to index the 2D array. – Weather Vane May 10 '20 at 12:57
  • well... How can I handle that? Sorry, I am new in the C. – Ömer Faruk VERGİSİZ May 10 '20 at 13:03
  • An array passed to a function decays to a pointer, and that is what the compiler knows. If it were a 1D array that is enough information. Here it is a 2D array so the compiler needs to know the width of each row. That's why the inner dimension is given. Note that for a 1D array, even if you state the dimension, the compiler will *ignore it*. Similarly, the 2D array here has no outer dimension, because the size of the array is unlimited. – Weather Vane May 10 '20 at 13:03
  • 1
    You have to pass the number of elements in the outer dimension to the function. BTW you *know* the size of the array, because you are the one who defined it. Suppose you allocate memory with `malloc`. People ask here "What is the size of the memory?" and the only possible answer is "the size you told it to allocate". – Weather Vane May 10 '20 at 13:07
  • well.. How can I allocate memory again in the void function? – Ömer Faruk VERGİSİZ May 10 '20 at 13:14

1 Answers1

1

The argument, int matrix[][5] when passed to a function, decays to a pointer to int. This is the reason why its size is 8 (the size of a pointer in your system).

As a consequence, the calculated number of columns is not what you expect.

I'm afraid there's no chance to obtain what you want without changing find_col_num () signature. The best way is probably passing the total size of the matrix to the function:

void find_col_num(int matrix[][5], size_t tot_size)
{
    /* ... */
}

And you could call it in this way

find_col_num(a, sizeof(a));
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39