c - 数据类型错误 - 求两个数组并集的程序 (c)

标签 c arrays

我正在尝试用 C 编写一个程序来创建两个数组之间的并集,然后输出新数组中元素的总数。编译我的代码 (gcc) 时出现以下错误。

test.c:44:11: 错误:在“(”标记之前需要“{” void union(int arrA[], int arrB[], int m, int n) ^ test.c:44:6: 错误:声明说明符中有两种或多种数据类型 void union(int arrA[], int arrB[], int m, int n) ^

我已经检查过是否缺少分号等。因此,除非我只是缺少它,否则我无法弄清楚问题出在哪里。任何帮助,将不胜感激。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main()
    {
        int n;
        int m;
        int i;
        int k;
        printf("Enter the size of array A: ");
        scanf("%d",&n);
        int arrA[n];
        printf("Enter the element(s) of array A: ");
        for(i=0;i<n;i++)
            {   
                scanf("%d",&arrA[i]);
            }
        for(i=0; i<n; i++)
            {
                printf("%d",arrA[i]);
            }
        printf("\n");

        printf("Enter the size of array B: ");
        scanf("%d",&m);
        int arrB[m];
        printf("Enter the element(s) of array B: ");
        for(i=0;i<m;i++)
            {
                scanf("%d",&arrB[i]);
            }
        for(i=0; i<m; i++)
            {
        printf("%d",arrB[i]);
            }
        printf("\n");

        printf("%d\n",k);
        return 0;
    }


    int union(int arrA[], int arrB[], int m, int n)
    {
        int i = 0;
        int j = 0;
        int k = 0;
        int l = 0;
        if(n > m)
            {
                n = l;
            }
        else
            {
                m = l;
            }
        int arrC[l];
        while ((i < n) && (j < m))
        {
            if (arrA[i] < arrB[j])
            {
                arrC[k] = arrA[i];
                i++;
                k++;
            }
            else if (arrA[i] > arbB[j])
            {
                arrC[k] = arrB[j];
                j++;
                k++;
            }
            else
            {
                arrC[k] = arrA[i];
                i++;
                j++;
                k++;
            }
        }
        if (i == n)
        {
            while (j < m)
            {
                arrC[k] = arrB[j];
                j++;
                k++;
            }
        }
        else
        {
            while (i < n)
            {
                arrC[k] = arrA[i];
                i++;
                k++;
            }
        }
        return(k);
    }

最佳答案

正如 sawims 在评论中指出的那样,union 是一个保留字,您在 else if (arrA[i] > arbB[j]) 上输入错误,更改函数名称并修复您的代码编译的拼写错误。

http://ideone.com/ubB1eG

关于c - 数据类型错误 - 求两个数组并集的程序 (c),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29336208/

相关文章:

arrays - Ruby 的数组是如何工作的?

c# - 为查看数组而创建的索引器看不到对数组所做的更改

c# - 在 C# 中一般访问多维数组

c 开放式简历;如何将二维数组转换为 IplImage?

c - 可疑指针转换警告

c - 使用 C 的嵌套结构程序

arrays - 复制列时如何保持真正的空白单元格

javascript - Vue war : Expected Array, 获得对象

从命令行执行时 CGI C 输出正确,但从 web 请求执行时不正确

c - 关闭堆栈保护