函数返回类型不兼容错误排查:Matrix结构体函数与int**返回值匹配问题
Let's walk through the issues in your code and fix them one by one:
1. Critical Undefined Behavior in infoM
Your infoM function creates a local Matrix variable on the stack, then returns a pointer to this local variable. When the function exits, the stack frame is destroyed, and the pointer you return becomes a dangling pointer (points to invalid memory). This leads to unpredictable behavior in your program.
Fix for infoM:
Instead of using a local variable, allocate the Matrix struct on the heap with malloc:
Matrix* infoM(int n_rows, int n_cols) { // Allocate memory for the Matrix struct itself Matrix* matrix = (Matrix*)malloc(sizeof(Matrix)); if (matrix == NULL) { fprintf(stderr, "Failed to allocate Matrix struct\n"); exit(EXIT_FAILURE); } matrix->rows = n_rows; matrix->cols = n_cols; // Allocate row pointers matrix->data = (int**)malloc(sizeof(int*) * n_rows); if (matrix->data == NULL) { fprintf(stderr, "Failed to allocate row pointers\n"); free(matrix); // Clean up already allocated memory exit(EXIT_FAILURE); } // Allocate each row's data for (int i = 0; i < n_rows; i++) { matrix->data[i] = (int*)malloc(sizeof(int) * n_cols); if (matrix->data[i] == NULL) { fprintf(stderr, "Failed to allocate row %d\n", i); // Clean up previously allocated rows and struct for (int j = 0; j < i; j++) { free(matrix->data[j]); } free(matrix->data); free(matrix); exit(EXIT_FAILURE); } } return matrix; }
This ensures the Matrix struct and its data live on the heap, so the pointer remains valid after the function returns.
2. Return Type Mismatch in readM
The compiler error Returning from a function with incompatible return type int ** happens because:
- Your
readMfunction is declared to returnint** - But you're returning a
Matrix*pointer (matrix)
According to your problem statement, readM should return an int** (the 2D array of elements). So you need to return matrix->data instead of the entire Matrix struct pointer.
Fix for readM:
int** readM(int n_rows, int n_cols, FILE *in) { Matrix* matrix = infoM(n_rows, n_cols); for (int i = 0; i < n_rows; i++) { for (int j = 0; j < n_cols; j++) { // Use clearer syntax for the address of the element fscanf(in, "%d", &matrix->data[i][j]); } } // Return the 2D data array as required int** data = matrix->data; // Optional: If you don't need the Matrix struct anymore, free it // free(matrix); // Note: If you free the Matrix struct here, make sure your caller knows they own the data pointer return data; }
Note: If you free the Matrix struct after extracting data, ensure the caller of readM knows they are responsible for freeing the 2D array later (first free each row, then the row pointer array).
Additional Notes
- Always use
NULLinstead of0when checking malloc results (it's more idiomatic in C). - Add proper cleanup for partial allocations (like freeing rows that were successfully allocated if one fails) to avoid memory leaks.
- Use array syntax (
matrix->data[i]) instead of pointer arithmetic (*(matrix->data+i)) for better readability.
内容的提问来源于stack exchange,提问作者Florin Sebastian

