[英]Why does the "for" loop not work and program stops?
When I compile the program it seems to work normally but, when it asks for the "cuadrados", the program just stops:当我编译程序时,它似乎正常工作,但是当它要求“cuadrados”时,程序就停止了:
int main(){
int *ptrs, suma=0, n, i, m;
printf("cuantos numeros al cuadrado quiere?: ");
scanf("%i", &n);
int numero[n], cubo[n];
*ptrs=numero
Here is when the program stops:这是程序停止的时间:
for(i=0;i<n;i++){
printf("escriba los cuadrados: ");
scanf("%i", numero[i]);
}
printf("Cuantos numero al cubo quiere?: ");
scanf("%i", m);
for(i=0;i<n;i++){
printf("escriba los cubos: ");
scanf("%i", cubo[i]);
}
I had this issue before but I can't understand why it does not keep running;我以前遇到过这个问题,但我不明白为什么它不继续运行; it just asks for 1 number and then stops with no error or warning.它只要求输入 1 个数字,然后停止,没有错误或警告。
for(i=0;i<n;i++){
printf("escriba los cuadrados: ");
scanf("%i", &numero[i]);
}
printf("Cuantos numero al cubo quiere?: ");
scanf("%i", &m);
for(i=0;i<n;i++){
printf("escriba los cubos: ");
scanf("%i", &cubo[i]);
}
You need to give address of the variable in scanf
using &
operator您需要使用&
运算符在scanf
给出变量的地址
This will crash because scanf
needs a pointer-to-int , not an int :这会崩溃,因为scanf
需要一个指向 int的指针,而不是一个int :
scanf("%i", numero[i]);
This will not:这不会:
if (scanf("%i", &numero[i]) == 1) {
/* do something */
}
else {
/* scanf failed */
}
Note that not testing the scanf
return value is always asking for trouble.请注意,不测试scanf
返回值总是自找麻烦。 As is not using your compiler's maximum warning level and turning all warnings into errors.因为没有使用编译器的最大警告级别并将所有警告转换为错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.