C语言作业问题:separate函数if语句未执行且代码无法编译
Hey there, let's break down the problems in your code and get it working properly. I see a few key issues that are causing compilation failures and unexpected behavior:
1. Wrong Operator (Immediate Compilation Error)
You're using the equality comparator == instead of the assignment operator = when trying to copy values into your even/odd arrays. That's why your if blocks aren't doing anything useful, and it's triggering compiler warnings or errors:
// ❌ This is a comparison, not an assignment—does nothing useful if ((arr[j]%2) == 0) arr1[j] == arr[j]; // ✅ Correct assignment to copy values into the new array if ((arr[j]%2) == 0) arr1[idx1++] = arr[j];
The same mistake applies to the odd array filling code—fix those first to resolve the immediate compilation issues.
2. Pointer Passing Logic Flaw
C uses pass-by-value, so when you assign arr1 = calloc(...) inside your separate function, you're only modifying the local copy of the pointer, not the newArr1/newArr2 variables in main. To update the pointers in the main function, you need to pass pointers to pointers (double pointers) instead.
3. Incorrect Array Indexing
Using the original array's index j to fill your new even/odd arrays is wrong. For example, if the 0th and 2nd elements are even, your even array should store them at indices 0 and 1—not 0 and 2. You need separate counters to track the current position in each new array.
4. Minor Header & Memory Cleanup Issues
system("pause")requires<stdlib.h>; without it, you'll get a compiler warning.<malloc.h>isn't a standard C header—use<stdlib.h>instead (it includescalloc).- Don't forget to free dynamically allocated memory to avoid memory leaks.
Fixed Full Code
#include <stdio.h> #include <stdlib.h> // Updated to use double pointers for arr1/arr2 to modify main's pointers void separate(int* arr, int n, int* size1, int* size2, int** arr1, int** arr2); int main() { int size1 = 0, size2 = 0; int* newArr1 = NULL; int* newArr2 = NULL; int arr[] = {6, 57, 14, 21, 11, 3, 22, 42, 9, 15}; printf("The array before change:\n"); for (int i = 0; i < 10; i++) { printf(" %d", arr[i]); } printf("\n"); // Pass addresses of the pointers to modify them in the separate function separate(arr, 10, &size1, &size2, &newArr1, &newArr2); printf("The even array is:\n"); for (int i = 0; i < size1; i++) { printf(" %d", newArr1[i]); } printf("\n"); printf("The odd array is:\n"); for (int i = 0; i < size2; i++) { printf(" %d", newArr2[i]); } printf("\n"); // Clean up dynamically allocated memory to avoid leaks free(newArr1); free(newArr2); system("pause"); return 0; } void separate(int* arr, int n, int* size1, int* size2, int** arr1, int** arr2) { int i, idx1 = 0, idx2 = 0; // Reset sizes before counting to avoid unexpected values *size1 = 0; *size2 = 0; for (i = 0; i < n; i++) { if (arr[i] % 2 == 0) { (*size1)++; } else { (*size2)++; } } printf("size1: %d size2: %d\n", *size1, *size2); // Allocate memory and assign directly to the pointers from main *arr1 = (int*)calloc(*size1, sizeof(int)); *arr2 = (int*)calloc(*size2, sizeof(int)); // Fill even array with correct indexing using a dedicated counter for (i = 0; i < n; i++) { if (arr[i] % 2 == 0) { (*arr1)[idx1++] = arr[i]; } } // Fill odd array with correct indexing using a dedicated counter for (i = 0; i < n; i++) { if (arr[i] % 2 != 0) { (*arr2)[idx2++] = arr[i]; } } }
Key Fixes Explained
- Double Pointers: By changing
int* arr1toint** arr1, we can modify the actual pointer variables inmaininstead of just local copies. The*arr1 = calloc(...)line updates the pointer in main directly. - Assignment Operator & Index Counters: Replaced
==with=to actually copy values into the new arrays, and addedidx1/idx2counters to keep track of where to place each element in the even/odd arrays. - Memory Management: Added
free()calls in main to clean up the dynamically allocated memory, preventing leaks. - Header Fixes: Swapped
<malloc.h>for standard<stdlib.h>and ensured all required headers are included.
内容的提问来源于stack exchange,提问作者David Bu

