【发布时间】: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 是一个保留字!