0
該程序應該採用2個數組並對數組中的每個元素執行點乘積。C程序 - 指針數組乘法
如果n的索引小於5,那麼我的程序很好;然而,一旦數組的索引大於5,只有第一個數組中的第一個元素是錯誤的(我通過在函數中添加printf語句來檢查)。我不知道如何解決這個錯誤。
#include <stdio.h>
void multi_vec(int *v1, int *v2, int *v3, int n);
int main(void)
{
int n, i;
int v1[n];
int v2[n];
int v3[n];
printf("Enter the length of the two vectors\n");
scanf("%d", &n);
printf("Enter numbers for the first array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
}
printf("Enter numbers for the second array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v2[i]);
}
multi_vec(v1, v2, v3, n);
for (i = 0; i < n; i++) {
printf("%d", v3[i]);
}
printf("\n");
return 0;
}
void multi_vec(int *v1, int *v2, int *v3, int n)
{
int i;
for (i = 0; i < n; i++) {
*(v3+i) = *(v1+i) * *(v2+i);
}
}
'N'是unintilaized http://rextester.com/AJDX1741 – P0W
詮釋V1 [N],如果n初始化 –
@ machine_1是illegal.even:你更多的C++人,是不是?在C中,VLA是可以的。 – EOF