The problem is kind of subtle due to working with vectors and pointers, but it’s something simple.
Imagine this scenario:
void altera(int x){
x = 10;
}
Calling it that:
int var1 = 50;
altera(var1);
printf("%d", var1); //50
Look at the ideone
It turns out that in C the parameters are always passed by copying, so changing the value of a parameter in a function does not change the original variable. Looking at this example I gave, the parameter x
is a copy of the value that var1
had, therefore alter x
function does not change the value of var1
.
You did exactly the same but through a pointer, now look:
void carrega (int *n){
...
n = (int *) malloc(3*sizeof(int));
...
}
int main(int argc,char *argv[]){
int *n=NULL;
carrega (n);
printf("%d\n", n[0]);
}
See how it is exactly like the example I gave but now with a int*
, a whole pointer. You create a pointer, pass it to the function in the hope that the function changes it, but remember that what is passed is a copy of the pointer. Then do n = (int *) malloc(3*sizeof(int));
does not change the n
that has in the main
but the copy of that n
that has in the function.
Here are two solutions to solve
1) Return the value you wanted to change:
int* carrega (int *n);
//^-- agora retorno de int*
int main(int argc,char *argv[]) {
int *n = NULL;
n = carrega (n); //coloca o valor devolvido de novo em n
printf("%d\n", n[0]);
}
int* carrega (int *n) {
//^--- agora retorno de int*
int i = 0;
FILE *f = fopen ("arquivo.txt", "r");
n = (int *) malloc(3*sizeof(int));
for(i=0; i<3; i++) {
fscanf (f, "%d", &n[i]);
printf("%d\n", n[i]);
}
return n; //retorna o novo array alocado
}
2) Pass the address of the variable you want to change:
void carrega (int **endereco_n);
// ^-- agora recebe o endereço do array
int main(int argc,char *argv[]) {
int *n = NULL;
carrega (&n); //passa o endereço do array através do &
printf("%d\n", n[0]);
}
void carrega (int **endereco_n) {
// ^-- agora recebe o endereço do array
int i = 0;
FILE *f = fopen ("arquivo.txt", "r");
int *n = (int *) malloc(3*sizeof(int));
for(i=0; i<3; i++) {
fscanf (f, "%d", &n[i]);
printf("%d\n", n[i]);
}
*endereco_n = n; //altera o array do main através do endereço recebido
}
Opa Maniero. Sorry about the weird code. I cut several things to make it simpler for you to understand what was my doubt, I did not see the malloc, I did not use the free, etc ... In fact I need to perform all the allocation within the "charge" function and then play with it in main. The Isac down here reminded me that I was passing the copy of the pointer and not the pointer itself.
– user120462
@Eduardooliveira only that is not how people usually make me C and is a code prone to errors. In general, all libraries work with this idea of allocating and passing the pointer, its departure from the standard. It’s your right to do so, but you’re doing it in a way that’s considered bad.
– Maniero
I get it, I’m gonna follow your lead. This code that I am doing is a very specific exercise for learning, where I have a file that the 1st whole element informs the amount of integer numbers that the file has. For example: 3 90 100 87 <<< Informs that I have three integers (90,100,87) After discovering the quantity, I had to put these three in an array, find the lowest value and print the inverse order of these elements. Then the teacher asked to leave nothing on main, including the opening of the file. As I have to take the amount of elements I had to put the aperture in the charge function.
– user120462