The problem I have is:
- I call the function scanf in the method copyValue(int num).
- I type an integer
- and that value is not assigned to the local variable that I then return in the main.
(I guess you won't have time to read it)
However, if I uncomment the call to the method coparReference(int *num)
- won't let me type.
- e Prints the number I wrote earlier in the copyVarlor() method.
#include <stdio.h>
#include <stdlib.h>
int main() {
int num = 50;
//Obtengo dirección de memoria de variable num
printf("Direccion de num en memoria: %p\n", &num);
printf("Valor de variable num inicial: %i\n", num);
num = copiaValor(num); //llamo a función copiaValor
printf("Direccion memo: %p\n", &num);
printf("Valor de variable num despues copiaValor: %i\n", num);
//copiaReferencia(&num);
//printf("numero despues de referencia: %d\n", num);
}
int copiaValor(int num){
printf("valor de dirección memoria de num local: %p\n", &num);
//Solicita valor para introducir
printf("Ingrese el número copia\n");
scanf("&d", &num);
printf("valor de num copia: %i\n", num);
printf("Valor de direccion memo de num local: %p\n", &num);
return num;
}
void copiaReferencia(int *num){
//Solicita valor para introducir
printf("Ingrese numero referencia\n");
scanf(" %d", num);
printf("valor de num refe: %p\n", &num);
printf("VAlor de num refe dentro del método: %i\n", *num);
}
And I don't understand why this happens. However, when I remove "scanf" and make a loop where I increment the value by 1, the value is saved in the local variable and is returned.
Thank you very much
It is always good to read and interpret the warning messages that the compiler throws at you:
Sure enough, the method
copiarValor
is wrong, since the call toscanf
is not configured correctly. that"&d"
should be"%d"
On the other hand, this method should not receive any integers, since the value of the variable passed as argument is not used at any time. For this there are local variables: