I'm having trouble changing the address of a pointer inside a function. I declare the pointer inside the function, then step it by parameter to another function. This other function should cause the pointer to point to another address. The problem is that once this function finishes and returns to the one that called it, the pointer does not change.
//imports
#include <stdio.h>
#include<stdlib.h>
#define tam_max_bucket 2
int dir_prof=0;
typedef struct Buck{
int profundidade;
int contador;
int chaves[tam_max_bucket];
}bucket;
typedef struct dir{
struct Buck *bucket_ref;
}dir_cell;
dir_cell *diretorio;
int make_address(int key, int profundidade){
int retVal, mask, hashVal, lowBit;
retVal = 0;
mask = 1;
hashVal = key;
int j;
for(j=0; j<profundidade; j++){
retVal = retVal << 1;
lowBit = hashVal & mask;
retVal = retVal | lowBit;
hashVal = hashVal >> 1;
}
return retVal;
}
int op_find(int key, bucket *found_bucket){
int address = make_address(key, dir_prof);
found_bucket = diretorio[address].bucket_ref;
printf("\n%d\n", found_bucket->profundidade);
printf("\n%d\n", diretorio[address].bucket_ref->contador);
printf("\n%d\n", found_bucket->contador);
int i;
for(i=0; i<found_bucket->contador; i++){
if(key == found_bucket->chaves[i])
return 1;
}
return -1;
}
int op_add(int key){
bucket *found_bucket = (bucket *) malloc(sizeof(bucket));
printf("\nCont - %d\n", found_bucket->contador); //Imprime o valor do contador assim que found_bucket é alocada
if(op_find(key, found_bucket) == 1){
return -1;
}
printf("\nNovo valor%d\n", found_bucket->contador); //Imprime o valor de contador de found_bucket após a chamada da função. O valor não se altera, continua o mesmo
return 1;
}
void main(){
diretorio =(dir_cell *) malloc(1*sizeof(dir_cell));
diretorio[0].bucket_ref =(bucket *) malloc(sizeof(bucket));
diretorio[0].bucket_ref->profundidade = 0;
diretorio[0].bucket_ref->contador = 0;
FILE *chaves;
chaves = fopen("chaves.txt", "r+");
if(chaves!=NULL){
int chave;
while((fscanf(chaves, "%d\n", &chave))!=EOF){
printf("%d", chave);
op_add(chave);
}
}
}