C solution
\nint** matrixReshape(int** nums, int m, int n, int r, int c, int** columnSizes, int* returnSize) {\n    if (r * c != m * n) {\n        r = m;\n        c = n;\n
```\nint** matrixReshape(int** nums, int m, int n, int r, int c, int** columnSizes, int* returnSize) {\n    if (r * c != m * n) {\n        r = m;\n        c = n;\n    }\n\n    *returnSize = r;\n    int** result = (int**) malloc(r * sizeof(int*));\n    *columnSizes = (int*) malloc(r * sizeof(int));\n    for (int i = 0; i < r; ++i) {\n        result[i] = (int*) malloc(c * sizeof(int));\n        (*columnSizes)[i] = c;\n    }\n    \n    for (int i = 0; i < m * n; ++i)\n        result[i/c][i%c] = nums[i/n][i%n];\n\n    return result;\n}\n```\n\nMost of the code is for creating the data structure, not for writing the real contents into it. This must be fun for masochists :-). I just did this because of @leaf2's [question about C solutions](https://discuss.leetcode.com/topic/88089/anyone-has-c-solution-of-this-question) and similar questions I've seen occasionally. Was a little challenge.\n\nThings to notice:\n- If the requested shape is invalid, we can't just return `nums` (which is what the problem text somewhat sounds like). Think about what the judge is probably doing. Probably it uses and frees all the result data (the data pointed to by the `return` value as well as what we write in `returnSize` and `columnSizes`). So we should create an independent *copy* of the input matrix with all its own data. I do this by simply changing `r` and `c` to the input shape and then continuing as otherwise. Can also be done like this:\n\n       if (r * c != m * n)\n           return matrixReshape(nums, m, n, m, n, columnSizes, returnSize);\n\n- The "sizes" we must write to are the numbers of rows and columns, not the malloc sizes (i.e., not multiplied with `sizeof(...)`). I found that unclear in the specification, but on the other hand, it makes more sense if you think about what the judge code probably looks like.\n- I renamed `numsRowSize` and `numsColSize` to the standard matrix size names `m` and `n`. Can't stand those awfully long and nonstandard names.
[]