信号:分割故障(核心倾倒)错误
编写了一个将两个数组相乘的程序,如下所示:
uv = u1v1 + u2v2 + u3v3 + ... un*vn
从用户处获取两个数组时,我收到“信号:分段错误(核心转储)”错误。
这是代码:
#include <stdio.h>
int scalar_product(int vectorU[], int vectorV[], int vectorLength) {
int i, sum = 0;
for (i = 0; i < vectorLength; i++)
sum += (vectorU[i] * vectorV[i]);
return sum;
}
void userInterface() {
int vectorLength = 0, i;
printf("Please enter the length of the vectors: ");
scanf("%d", &vectorLength);
int vectorU[vectorLength], vectorV[vectorLength];
printf("\nVector U:");
for (i = 0; i < vectorLength; i++) {
printf("\n%d) ", (i + 1));
scanf("%d", &vectorU[i]);
}
printf("\nVector V:");
for (i = 0; i < vectorLength; i++) {
printf("\n%d) ", (i + 1));
scanf("%d", &vectorV[i]);
}
printf(scalar_product(vectorU, vectorV, vectorLength));
}
main(void) {
userInterface();
}
Wrote a program that multiply two arrays like this:
uv = u1v1 + u2v2 + u3v3 + ... un*vn
after getting from the user both of the arrays, i get a "signal: segmentation fault (core dumped)" error.
here's the code:
#include <stdio.h>
int scalar_product(int vectorU[], int vectorV[], int vectorLength) {
int i, sum = 0;
for (i = 0; i < vectorLength; i++)
sum += (vectorU[i] * vectorV[i]);
return sum;
}
void userInterface() {
int vectorLength = 0, i;
printf("Please enter the length of the vectors: ");
scanf("%d", &vectorLength);
int vectorU[vectorLength], vectorV[vectorLength];
printf("\nVector U:");
for (i = 0; i < vectorLength; i++) {
printf("\n%d) ", (i + 1));
scanf("%d", &vectorU[i]);
}
printf("\nVector V:");
for (i = 0; i < vectorLength; i++) {
printf("\n%d) ", (i + 1));
scanf("%d", &vectorV[i]);
}
printf(scalar_product(vectorU, vectorV, vectorLength));
}
main(void) {
userInterface();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
printf 的这种调用是不正确的
您至少需要编写
此外,最好声明并定义函数,如
要输出结果,您需要使用格式字符串
“%lld \ n”..
long long int
类型用于避免溢出。另一种方法是将函数返回类型声明为 double。
您还忘记指定函数 main 的返回类型
int
。This call of printf is incorrect
You need to write at least
Also it would be better to declare and define the function like
To output the result you need to use the format string
"%lld\n"
..The type
long long int
is used to avoid an overflow.Another way is to declare the function return type as
double
.Also you forgot to specify the return type
int
of the function main.这里:
...您未能指定
printf
的格式。因此,它尝试将 scalar_product() 的结果解释为指向格式字符串的指针。未定义的行为结果。如果您的编译器没有发出有关此问题的警告,那么您应该学习如何调高警告级别以使其发出警告,或者获得更好的编译器。如果您的编译器正在发出有关它的警告,那么请将此作为一个教训,忽略编译器警告是不安全的。
也许您想要类似这样的东西:
作为一个小附加问题,您忘记了
main()
的返回类型。您的编译器可能将其视为返回int
,事实证明这是正确的做法,但这并不意味着您的代码正确。您想要:通过这两项更改,您的程序可以在没有任何诊断的情况下为我编译,并且运行时没有错误,产生我期望的结果。
至少对于小向量长度来说是这样。如果您尝试非常大的向量,那么您可能会超出堆栈上分配向量的可用空间。
Here:
... you have failed to specify a format for
printf
. As a result, it attempts to interpret the result ofscalar_product()
as a pointer to the format string. Undefined behavior results.If your compiler is not emitting a warning about that then you should either learn how to turn up the warning level so that it does, or else get a better compiler. If your compiler is emitting a warning about it, then take this as a lesson that it is not safe to ignore compiler warnings.
Probably you wanted somethign more like this:
As a minor additional issue, you have forgotten the return type for
main()
. Your compiler is probably treating it as returningint
, which turns out to be the right thing to do, but that doesn't make your code correct. You want:With those two changes, your program compiles for me without any diagnostics, and runs without error, producing the result I expect.
At least, for small vector lengths. If you try really large vectors then you might exceed the available space for allocating your vectors on the stack.