-1

I am solving problems on online judge like Leetcode and I wonder if it' possible to get size of a 2d array given int**A. Consider the function,

 int help(int** A){
       int rows = sizeof(A)/sizeof(A[0]);
       int columns = sizeof(A[0])/sizeof(A[0][0]);
}

But I am not getting the correct values of rows and columns. Is there a way to get sizes of a 2d array if I only have int** A. Same question for char** A. I know that the question is poorly framed but I am a beginner in C. Thank you.

user3386109
  • 34,287
  • 7
  • 49
  • 68
jd_sal
  • 11
  • 3
  • This is not possible in C. Do you want C or C++? – NathanOliver Oct 18 '19 at 21:00
  • Possible duplicate of [How to pass 2D array (matrix) in a function in C?](https://stackoverflow.com/questions/3911400/how-to-pass-2d-array-matrix-in-a-function-in-c) – dandan78 Oct 18 '19 at 21:02
  • 2
    Possible duplicate of [how to find 2d array size in c++](https://stackoverflow.com/questions/10274162/how-to-find-2d-array-size-in-c) – Ruzihm Oct 18 '19 at 21:03
  • Possible in C++? – jd_sal Oct 18 '19 at 21:03
  • For C++ see: https://stackoverflow.com/questions/6934776/c-getting-the-row-size-of-a-multidimensional-array-passed-to-a-function – NathanOliver Oct 18 '19 at 21:05
  • [A better way to handle a 2D rectangular array in C++](https://stackoverflow.com/a/2076668/4581301) – user4581301 Oct 18 '19 at 21:08
  • [Finding length of array inside a function (duplicate)](https://stackoverflow.com/q/17590226/995714), [How to get array size within function?](https://stackoverflow.com/q/39429730/995714), [Using sizeof() on an array passed to a function (duplicate)](https://stackoverflow.com/q/27096272/995714) – phuclv Oct 18 '19 at 23:39

1 Answers1

1

No, this is not possible.

There's nothing in the allocated memory that indicates where it starts and ends. For 2D arrays, there's not even a guarantee the memory is contiguous.

**A does not contain any data about itself - all of the info about the array must be kept track of by the programmer.

Nick Reed
  • 4,989
  • 4
  • 17
  • 37
  • 1
    Actually `**A` isn't a pointer to the first element in an array. It's a pointer to another pointer that points to an int. There may or may not be ints following. It might also be a pointer to an array of pointers and the first element of that array points to an int. But that's all speculation. The only thing known for sure is that it's a pointer to a pointer to an int. – doug Oct 18 '19 at 22:12
  • `**A` is not "the address of a pointer to a pointer of an int". `**A` is actually an `int`. `*A` is a pointer to an `int`, and its value is the address of an `int`. `A` is a pointer to a pointer to an int - so its value may be the address of a pointer to an `int`. – Peter Oct 18 '19 at 23:40
  • @Peter I should have referred to the declaration `int **A` which declares a pointer to a pointer to an int. You are, of course, correct that the stand alone expression `**A` is just an int. But I think the context is clear given that `**A` only appears in a declaration. – doug Oct 19 '19 at 01:25
  • 1
    @doug - I was not responding to your comment. I responded to content of the answer at that time - which said that `**A` was "the address of a pointer to a pointer of an int". It has since been edited to remove that text. – Peter Oct 19 '19 at 02:59