-1

I'm trying to find a way to find the size of 2D array. Here is the function I use to create a matrix:

float** createMatrix(int rows, int colums,char populate){

float** matrix = malloc(sizeof(float*)*rows); // allocates the memory for all the pointers of the 2D array

for(int i = 0;i<rows;i++){ // allocates the memory for each pointer in the 2D array
    matrix[i] = malloc(sizeof(float)*colums); // allocates the memory for all the colums in each row
}

if(populate=='Y'){
    for(int i = 0;i<rows;i++){ // prompts the user for values to create the array
        for(int j = 0;j<colums;j++){
            printf("Value for row %d colum %d: ",i+1,j+1);
            scanf("%f",&matrix[i][j]);
        }
    }

}else if(populate=='N'){
    return matrix;
}

return matrix; // returns the matrix created
}

Testing it out:

float** matrix = createMatrix(100,100,'N');  

int rows = sizeof(matrix)/sizeof(float*);
int colums = sizeof(matrix[0])/sizeof(float) - 1;

printf("Rows: %d and Cols: %d",rows,colums);

I get "Rows: 1 and Cols: 1" as the output. I'm not sure what I'm doing wrong?

user7839375
  • 31
  • 2
  • 7
  • 7
    `matrix` is a pointer, not an array. `sizeof(matrix)` does not tell you the size of the contents. – Tibrogargan Apr 29 '17 at 05:24
  • 5
    In short, no. You need to store it by yourself – Tatsuyuki Ishi Apr 29 '17 at 05:25
  • You can not get the size of the entire reserved area from the pointer. – BLUEPIXY Apr 29 '17 at 05:26
  • 3
    Also, your first `malloc()` uses `sizeof (int *)`. If you value correctness of your code at all, better to use `sizeof(float *)` or `sizeof(*matrix)`. – Peter Apr 29 '17 at 05:31
  • I fixed it to 'float *' and I still get the same problem, I'm using it as an array because C wont let me return arrays, and I'm not sure what you mean by you need to store it by yourself? – user7839375 Apr 29 '17 at 05:55
  • I'm just trying to get the rows and the columns from the matrix returned by the function – user7839375 Apr 29 '17 at 05:57
  • 1
    You can't get "the rows and columns from the matrix returned by the function" because nothing stores that information. That's what the first couple of comments say too. You're on a hiding to nothing. You're trying to square the circle; trying to find a perpetual motion machine; … and many other impossible tasks. If the information is to be made available, you must either use a different language (not C), or you have to explicitly arrange for the information to be made available yourself, by any suitable mechanism you choose. Or rethink the issue; have the calling code dictate the size. – Jonathan Leffler Apr 29 '17 at 06:06
  • The best way to keep track of the size would be to just use rows and columns in your initialization: `int rows = 100, columns = 100; createMatrix(rows,columns, 'N');` and just keep these around. Alternatively, a struct: `struct matrix { int rows, columns; float **table }` and load it up to pass everything around together. – Kenneth B. Jensen Apr 29 '17 at 06:11
  • Theoretically (but not recommended), you could check the size of the `malloc`ed block and calculate the array size by dividing the block size by `sizeof(float*)` resp.`sizeof(float)`, see http://stackoverflow.com/questions/5451104/how-to-get-memory-block-length-after-malloc – Erich Kitzmueller Apr 29 '17 at 06:47

1 Answers1

1

The answer is in the parameter values in the function call:

    createMatrix(100,100,'N')

So the answer should be 100 rows and 100 columns.

The variable matrix is the memory address of [0][0]th index matrix which is a 2D array. From this memory address, there is no way to find out the dimension of 2D using sizeof() C function. There may be a way to back trace it logically.

Here is a reason why you are getting the outputs as you have indicated above:

   int rows = sizeof(matrix)/sizeof(float*);
   int colums = sizeof(matrix[0])/sizeof(float) - 1;

matrix is a double pointer to float. The size of double pointer to float is 8. matrix[0] is a single pointer to float. The size of single pointer to float is 8. sizeof float is 4. (most cases)

So we have

    sizeof(matrix)/sizeof(float *) = 1    //since 8/8 = 1.
    sizeof(matrix[0])/sizeof(float) = 2   //since 8/4 = 2
Nguai al
  • 958
  • 5
  • 15