Why does int[][2]a become C as a parameter?

Asked 2 years ago, Updated 2 years ago, 86 views

Hello, I'm a second-year undergraduate student who is studying alone after being discharged from the military and before returning to school.

While solving the problem, C declared this structure.

int solution(int A[][2], size_t A_len) {
//Implementation Department

}

"Why did I set the column to 2?" So I looked it up.

** The pointer to the first element of the array is passed because the entire array cannot be passed as a parameter of the function. Arguments receiving a two-dimensional array must define the rightmost dimension. If you want to index an array correctly, you need the rightmost dimension because the compiler needs it to determine the length of each row. If the index to the far right is not mentioned, the compiler cannot determine where the next row begins. **

 // Java example void receiving_function (int a [] [10]) {. . .} 

(Source : https://ko.gadget-info.com/difference-between-one-dimensional)

.

I have three questions here.

I'll google it until I understand and find the data and get the answer. Even if you understand it, I think you might be mistaken. If you can answer, please answer!

c java 2d-array

2022-09-20 17:26

2 Answers

For example, a two-dimensional array, when coding, you think about a square with a height and a width, but the memory has only one line of memory with a height and width.

So if you don't have a right index at number 2, you don't know where to cut in that one line of memory to go to the next line of the array (square) you declare.

I don't know if your answer was okay


2022-09-20 17:26

@dh jung Thank you for your detailed reply to the long article. But there's something I still don't understand. The act of telling the compiler the size of the second index corresponding to the row in the matrix is acceptable in C, but why in Java?


2022-09-20 17:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.