how to set a int value passed by parameter to a function and assign it to global so I can use outside of the function?
Example:
int assignValues(int valor_assign1, valor_assign2){
valor_assign1 = 7;
valor_assign2 = 3;
}
main (){
int valor1 = 0;
int valor2 = 0;
assignValues(valor1,valor2);
printf("%d,%d",valor1, valor2);
}
The output is actually 0,0 I want it to be 7,3 how do I do this? I know its a simple question but I've been trying and searching but I can't find anything like that =/ Thanks in advance.
Pass the pointers:
You pass pointers to the variables:
But, in your snippet,
valor1
andvalor2
are not global. They are local tomain
. If they were global, you could just assign to themNote: the usage of globals is frowned upon. Your usage, with local variables, is much better.
You could pass a pointer to your integers into the function, instead of passing the values. E.g.:
You might want to read a pointer tutorial, however.