我的功能无法正常工作。程序运行,但当它进入 for 循环时,该函数不起作用并停止程序,即使它应该继续循环。如果您可以的话,请检查我的数组函数,并告诉我是否有任何我不理解或做得不正确的地方。
感谢您的宝贵时间。
我知道循环不是问题,因为当我删除该函数时它工作正常。我还尝试将“b”放在函数数组参数中,如下所示“int Array(int a[b], int b, int c);”
#include <stdio.h>
#include <stdlib.h>
/*Function*/
int Array(int a[], int b, int c);
/*Main Program*/
int main()
{
int S, C, *A, *B;
printf("How Many Numbers Would You Like in Array A and B? ");
scanf("%d\n", & S);
/*For Loop Asking The User to Enter a Value and using the Array function to calculate/store the B[] Value*/
for (C=0; C<=S; ++C){
printf("\nWhat is A[%d] ", C);
scanf("%d", & A[C]);
B[C] = Array(A, S, C);
}
}
/*Function*/
int Array(int a[], int b, int c)
{
if (a[c] < 0){
return a[c] * 10;
} else {
return a[c] * 2;
}
}
预期结果:
程序要求用户输入用于 *A 和 *B 的数组大小
程序使用 for 循环要求用户为数组 *A 中的每个位置输入一个值,使用该值计算每个匹配 B 位置的值
实际结果:
程序要求用户输入用于 *A 和 *B 的数组大小
程序使用 for 循环要求用户为数组 *A 中的每个位置输入一个值,程序要求用户输入一个值然后停止运行。
最佳答案
您没有为数组 A 分配任何内存。您只需将其声明为指向 int 的指针,然后开始向其中写入值,这些值将写入某个随机内存位置。在第一个 scanf 获取 S 之后,您需要在访问它之前分配 A = malloc(S * sizeof(int))
。
关于c - "Array"功能正在阻止程序正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55643191/