【问题标题】:data types error - program to find the union of two arrays (c)数据类型错误 - 查找两个数组并集的程序 (c)
【发布时间】:2015-06-02 21:04:29
【问题描述】:

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

test.c:44:11: 错误:在“(”标记之前应为“{” 无效联合(int arrA[],int arrB[],int m,int n) ^ test.c:44:6:错误:声明说明符中有两种或多种数据类型 无效联合(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);
    }

【问题讨论】:

  • 我现在无法检查,但问题可能是函数名称引起的。联合是保留字。尝试将您的函数重命名为,例如,array_union。
  • 你不能调用函数union,因为它是C语言的保留字。
  • 谢谢 - 没有意识到 union 是一个保留字!

标签: c arrays


【解决方案1】:

正如 cmets 中的 sawims 所指出的,union 是一个保留字,您在 else if (arrA[i] &gt; arbB[j]) 上有一个拼写错误,更改了函数的名称并修复了您的代码编译时的拼写错误。

http://ideone.com/ubB1eG

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多