【问题标题】:Why doesn't the if statement get executed in the function为什么if语句没有在函数中执行
【发布时间】:2019-07-19 12:38:40
【问题描述】:

我正在尝试做作业,但我卡住了。他们希望我取一个已经给出的数组并将其分成两个数组,其中一个保存偶数,另一个保存奇数。我写了一个 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;
}

不编译

【问题讨论】:

  • 你好......如果你说它在运行时做一件事并且它不编译......通常当你遇到这样的情况时,你似乎有两个问题注意你的编译器警告/错误是很好的,一旦你修复了这些,有时事情:just work™...
  • @David Bu “为什么if语句不执行”和“不编译”是什么关系?
  • 您不能从 void main() 发送 return 0;。你应该使用int main(void)
  • @VladfromMoscow 好吧,“编译”“执行”的先决条件。 ;-)
  • 当你调用separate时,newarr1newarr2在调用方不会被修改。这是经典的,仔细阅读this SO article

标签: c arrays byref


【解决方案1】:

开启警告!您正在尝试使用 '==' 进行分配 - 应该是 '='。

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];
             ^

【讨论】:

  • 是的,我的编译器也是这么说的。
  • 无需解释 - 它不言自明,而且 OP 显然没有打开警告。即使是初学者,稍微有点主动性,也应该能够找出===之间的区别。
【解决方案2】:

这是错误的

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

想象j 是最后一个 (n - 1)。您将尝试将arr1[n - 1] 设置为任何值,但arr1 的大小是size1 而不是n

【讨论】:

    【解决方案3】:

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

    您的数组超出了界限,因为您在其他数组中仅分配了足够的内存来容纳正在排序的数组中的偶数/奇数数量。我给你留下了cmets。我知道您使用的是什么编译器或 ide,但我在 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多