开发者

pass the address to a function and assign it to another variable

   if ((pos < n) && (key == ptr->keys[pos].value))
        function(&ptr->keys[pos]);

here i need to pass the address of ptr->keys[pos] to function, and i have done it as shown above, i am calling this "function" inside another function

    inline struct return_values *function(struct classifier *fun_ptr)
    {
        struct return_values *r; 
        r = malloc(sizeof(struct return_values));
        r->value1 = Duplicate;
        r->value2 = (void *)get;
        return r;
    }

the definition of function is as shown above i have to assign the address of ptr->keys[pos] to r->value2

the structure return_values is as shown below

    struct return_values
  开发者_C百科  {
        KeyStatus_t value1;
        void* value2;
    }

how to assign the address of &ptr->keys[pos] to r->value2, i am able to pass the address to function form there how to assign that address to r->value2 and i dont want to change structure return_value in any case as it will have effect my entire code please help.


Passing the address to fields of a struct can cause trouble depending on your runtime (and is bad style, but that's an opinion). Instead of:

function(&ptr->keys[pos]);
  • You should either pass the pointer to the struct:

    function(ptr);

    and let the function de-reference it (not encapsulated but flexible).

  • or just call the function with the value stored inside that field, and assign to that field when the function returned like so:

    ptr->keys[pos] = function(ptr->keys[pos]);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜