Many developers find themselves in a situation where they need to convert a 2D array to a pointer-to-pointer in C . This conversion is not as straightforward as it may seem, and there is no direct way to accomplish it without introducing an intermediate step.
Given a 2D array of objects:
Activity solution[a][b];
The goal is to convert it into a pointer-to-pointer representation:
Activity **mother = solution;
A simple conversion from a 2D array to a pointer-to-pointer will not work due to type incompatibility. To bridge this gap, an additional "row index" array is required as an intermediate step:
Activity *solution_rows[a] = { solution[0], solution[1], /* etc. */ }; Activity **mother = solution_rows;
Now, accessing mother[i][j] will grant access to solution[i][j]. This approach maintains the semantics of the 2D array while providing the pointer-to-pointer representation required.
The above is the detailed content of How do you Convert a 2D Array to a Pointer-to-Pointer in C ?. For more information, please follow other related articles on the PHP Chinese website!