简体   繁体   中英

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:

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.

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

This will crash because scanf needs a pointer-to-int , not an 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. As is not using your compiler's maximum warning level and turning all warnings into errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM