-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.

7

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
**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 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 - 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.