简体   繁体   English

在C函数内部具有函数的参数和指针

[英]Parameters and pointers with function inside function in C

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. 我的目标是在每个函数中保留变量“ counter”的值,换句话说,当最低的函数声明“ counter ++”时,程序中每个其他“ counter”变量的值都必须增加。

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: 在我的.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. 我的主要问题是:运行代码时,即使在commandInsert()函数中运行了“ counter ++”命令后,它仍会继续发送counter的“ -1”值。 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? 我认为问题可能出在commandInsert(id,tableHash,counter,flag)调用上,因为我没有使用参考符号(&),但是由于它是参数,'counter'已经在identifyCommand()中时已经是一个指针,那么我在这里想念什么?

Since you want to change the value pointed to by counter , you should change the value. 由于要更改counter指向的 ,因此应更改该值。 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). 如@szczurcio所述,您从中传递的command数组最多只能容纳5个字符(NUL终止符为1个字符)。 So the size of command array needs to be at least 7 to read "INSERT" . 因此, command数组的大小至少应为7才能读取"INSERT" To prevent buffer overrun, you can use the width in scanf() such as: 为了防止缓冲区溢出,可以在scanf()使用宽度,例如:

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

Or you can use fgets() . 或者,您可以使用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). 但是,由于将command传递给函数,因此不能在函数内部使用sizeof command因为那样会返回sizoof(char*) (将数组传递给函数时,它会变成指向其第一个元素的指针)。 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;

   ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM