繁体   English   中英

为什么if语句未在函数中执行

[英]Why doesn't the if statement get executed in the function

我正在尝试做作业,但被卡住了。 他们要我取一个已经给定的数组并将其分成两个数组,其中一个保存偶数,另一个保存奇数。 我编写了一个void函数,该函数接收6个参数,如下所示。 该函数中的if语句:( if ((arr[j]%2) == 0) )由于某种原因未执行。 它只是跳过它。 我真的不明白为什么,非常感谢您的协助。

尝试调试,对指针Arr1和Arr2使用不同的语法。

#include <stdio.h>
#include <malloc.h>

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");

    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");
    system("pause");
    return 0;

}
void separate(int* arr, int n, int* size1, int* size2, int* arr1, int* arr2)
 {

    int i, j;
    for (i = 0; i < n; i++)
    {
        if (arr[i] % 2 == 0)
            (*size1)++;
        else
            (*size2)++;
    }

    printf("\n");
    printf("size1: %d size2: %d", (*size1),(*size2));
    arr1 = (int*)calloc((*size1), sizeof(int));
    arr2 = (int*)calloc((*size2), sizeof(int));
    for (j = 0; j < n; j++)
    {
        if ((arr[j]%2) == 0)
        arr1[j] == arr[j];

    }
    for (j = 0; j < n; j++)
    {
        if (arr[j] % 2 != 0)
            arr2[j]== arr[j];
    }

    return;
}

不编译

打开警告! 您尝试使用“ ==”进行分配-应该为“ =”。

gcc -std=c99 -Wall    omg.c   -o omg
omg.c: In function 'main':
omg.c:32:5: warning: implicit declaration of function 'system' [-Wimplicit-function-declaration]
     system("pause");
     ^
omg.c: In function 'separate':
omg.c:55:9: warning: statement with no effect [-Wunused-value]
         arr1[j] == arr[j];
         ^
omg.c:61:13: warning: statement with no effect [-Wunused-value]
             arr2[j]== arr[j];
             ^

这是错的

 for (j = 0; j < n; j++) { if ((arr[j]%2) == 0) arr1[j] == arr[j]; } 

想象j是最后一个( n - 1 )。 你会尝试设置arr1[n - 1]到什么,但大小arr1size1n

正如其他人指出的那样,您正在使用==尝试分配值。

您的数组超出范围,因为您在其他数组中仅分配了足够的内存来容纳要排序的数组中的偶数/奇数数量。 我给你留下了评论。 知道使用什么编译器或使用哪种编译器,但是我在Visual Studio上进行了此工作,并对代码进行了其他一些更改。 我也是学生!

void separate(int* arr, int n, int* size1, int* size2, int* arr1, int* arr2)
{

    int i, j;
    for (i = 0; i < n; i++)
    {
        if (arr[i] % 2 == 0)
            (*size1)++;
        else
            (*size2)++;
    }

    printf("\n");
    printf("size1: %d size2: %d", (*size1), (*size2));

    // Your assigning only enough space to hold the amount of even/odd numbers
    arr1 = (int*)calloc((*size1), sizeof(int));
    arr2 = (int*)calloc((*size2), sizeof(int));

    // If the index of the array is larger than what you allocated, crash..
    for (j = 0; j < n; j++)
    {
        if ((arr[j] % 2) == 0)
            arr1[j] == arr[j];

    }
    for (j = 0; j < n; j++)
    {
        if (arr[j] % 2 != 0)
            arr2[j] == arr[j]; // Use = to assign, not ==
    }

    return;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM