我正在尝试编写一个程序,其中将有两个带有大块代码的函数。每个函数都将调用一些较小的函数。现在,在主程序体中,我需要调用两个更大的函数。我在程序中使用数组。
我做了一个样本,但是我收到了错误。请妥善我的代码。我是这个领域的新手。所以请帮忙。
# include <stdio.h>
unsigned char a[4] = {5, 3, 6, 2};
unsigned char b[4] = {3, 5, 2, 5};
unsigned char c[4] = {4, 2, 5, 7};
unsigned char x[4];
unsigned char y[4];
void add(unsigned char* num1, unsigned char* num2, unsigned char* num3)
{
int k;
for (k=0; k<3; k++)
{
num3[k] = num1[k] + num2[k]);
}
}
void mul(unsigned char* num1, unsigned char* num2, unsigned char* num3)
{
int k;
for (k=0; k<3; k++)
{
num3[k] = num1[k] * num2[k]);
}
}
void test1(unsigned char* num1, unsigned char* num2, unsigned char* num3)
{
add(num1, num2, num3);
mul(num1, num2, num3);
}
void test2(unsigned char* num1, unsigned char* num2)
{
add(num1, num2, num1);
}
void main()
{
int i,j ;
test1(a,b,x);
test2(c,y);
for (i=0; i<4; i++)
{
printf("%d ",(x[i]);
}
printf(" ");
for (i=0; i<4; i++)
{
printf("%d ", y[i]);
}
}
答案 0 :(得分:1)
有几个编译和逻辑错误:
}
)
)
正确的缩进在很容易识别错误匹配括号和大括号的问题方面有很长的路要走,以修复看似含糊不清的错误,例如“预期;之前......”
以下是您更正的代码,其中包含描述错误和修复的内联注释。
# include <stdio.h>
unsigned char a[4] = {5, 3, 6, 2};
unsigned char b[4] = {3, 5, 2, 5};
unsigned char c[4] = {4, 2, 5, 7};
unsigned char x[4];
unsigned char y[4];
void add(unsigned char* num1, unsigned char* num2, unsigned char* num3)
{
int k;
for (k=0; k<4; k++) {
/* removed extra closing bracket */
num3[k] = num1[k] + num2[k];
}
}
void mul(unsigned char* num1, unsigned char* num2, unsigned char* num3)
{
int k;
/* fixed value of array bounds check */
for (k=0; k<4; k++) {
num3[k] = num1[k] * num2[k];
}
}
void test1(unsigned char* num1, unsigned char* num2, unsigned char* num3)
{
add(num1, num2, num3);
mul(num1, num2, num3);
}
void test2(unsigned char* num1, unsigned char* num2)
{
add(num1, num2, num1);
}
int main()
{
int i,j ;
test1(a,b,x);
test2(c,y);
for (i=0; i<4; i++) {
/* added missing closing bracket */
printf("%d ",(x[i]));
/* added missing closing brace */
}
printf(" ");
for (i=0; i<4; i++) {
printf("%d ", y[i]);
}
}
答案 1 :(得分:0)
您的数组是4项,但您使用的是索引&lt; 3
for (k=0; k<3; k++)
应该是
for (k=0; k<4; k++)
在这里你有一个额外的)
num3[k] = num1[k] + num2[k]);
num3[k] = num1[k] * num2[k]);
答案 2 :(得分:0)
有一个括号,其中不应该在num2 [k]之后,并且改变它们所在的循环&lt; 3至&lt; = 3。你错过了(在printf(“%d”,(x [i]))
答案 3 :(得分:0)
似乎x和y未初始化,因此将y传递给test2会导致将垃圾值传递给add的第二个参数。
答案 4 :(得分:0)
num3[k] = (num1[k] + num2[k]); //fix bracket
printf("%d ",x[i]); //fix bracket