1

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?

1 Answer 1

6

Since you want to change the value pointed to by counter, you should change the value. But you are incrementing the pointer.

counter++;

should be

(*counter)++;

As @szczurcio noted, the command array you pass from can hold only upto 5 chars (1 for the NUL terminator). So the size of command array needs to be at least 7 to read "INSERT". To prevent buffer overrun, you can use the width in scanf() such as:

char command[7];
scanf("%6s", command); 

Or you can use fgets().

char command[7];
fgets(command, sizeof command, stdin);

char *p = strchr(command, '\n');
if (p) *p = 0;

However, since you pass command to a function, you can't use sizeof command inside a function as that would return the sizoof(char*) (an array decays into a pointer to its first element when passed to a function). So you would have to pass the size information through another parameter:

From the caller:

   while(1) {
     identifyCommand(command, sizeof command, id, &tableHash, flags, &counter);
   }

And in the function:

void identifyCommand(char* command, size_t size, int id, Hash* tableHash,
 int* flag, int* counter){

   fgets(command, size, stdin);

   /* To remove the trailing newline, if any */
   char *p = strchr(command, '\n');
   if (p) *p = 0;

   ...
Sign up to request clarification or add additional context in comments.

5 Comments

A useful convention when programming is to use a 'p' or 'ptr' at the end or start of a variable name to remind yourself it is a pointer and not a value. So in the OP's case use int *counterp as the parameter. This makes it easy to connect the dots mentally.
WOW, I totally didn't see that. Silly mistake that took me hours haha. Thanks a lot!!
@StephenG Indeed. At my work, there's a convention to use _p suffix for a pointer and _pp for a pointer to pointer. This way, one doesn't have to look up the declaration everytime and helps in accidents like this.
It's worth mentioning that char command[6] cannot hold "INSERT", which appears to be one of the valid commands. And in general OP shouldn't use scanf without specifying the buffer width.
@szczurcio Thanks for pointing out. I have updated to cover that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.