0

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.

2
  • What do you mean by 'it stops compiling'? Do you mean it stop executing and wait for an input ?do you provide an input ? What are your inputs ? Commented Nov 24, 2020 at 16:28
  • You must pass the address of the variable to scanf() like the first time. So scanf("%i", m); must be scanf("%i", &m); and similar for the arrays, I leave you to figure them out. Didn't the compiler tell you? Commented Nov 24, 2020 at 16:32

2 Answers 2

2
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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.