I'm having a bit of trouble trying to use pointers as parameters to a function inside another function. My goal is to preserve the value for the variable 'counter' in every function, in other words, when the lowest function declares a 'counter++', it's value must be incremented for every other 'counter' variable in program.
My code looks like this:
int main(int argc, const char * argv[]) {
Hash tableHash;
char command[6];
int id = 0, counter = -1;
int flags[4] = {0, 0, 0, 0};
while(1) {
identifyCommand(command, id, &tableHash, flags, &counter);
}
return 0;
}
Inside my .h:
void identifyCommand(char* command, int id, Hash* tableHash, int* flag, int* counter){
scanf("%s", command);
/* ... */
if(strcmp(command, "INSERT") == 0){
scanf("%i", &id);
commandInsert(id, tableHash, counter, flag);
}
/* ... */
return;
}
void commandInsert(int id, Hash* tableHash, int* counter, int* flag){
Registry x;
x.key = id;
if(flag[MALLOCFLAG]){
tableHash->trees[*counter] = create_tree(x);
counter++;
flag[MALLOCFLAG] = 0;
}
else {
insert_element(tableHash->trees[*counter], x);
}
return;
}
My main question is: when I run the code, it keeps sending the '-1' value of counter even after 'counter++' command is run inside commandInsert() function. Why is that happening and how do I solve it?
I think maybe the problem is at the commandInsert(id, tableHash, counter, flag) call, because I didn't use the reference sign (&), but 'counter' is already a pointer when inside identifyCommand() because of it's parameters, so what am I missing here?