Writing an Address to a Pointer in a struct with a function fails

Viewed 69

I have two structs pbuf and netif, and assigned two variables (local_pbuf, local_netif) with them. These variables hold some data. There is another struct called wrapper_p_n, which holds two pointers of the type pbuf and netif. My goal is to write a function which hand over the variables local_pbuf and local_netif by call by reference and then wraps the two pointers in a single struct called wrapper_p_n. Then I want to use call by reference to give wrapper_p_n to another function. Unfortunately I get the Error message: [Error] cannot convert 'pbuf**' to 'pbuf*' in assignment

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

struct pbuf{
    int a;
    int b;
};

struct netif{
    int c;
    int d;
};

struct wrapper_p_n{ // wrapper for pbuf- and netif-struct pointer
    struct pbuf *wp_val_p;
    struct netif *wp_val_n;
};

void rx_local_p_n(struct pbuf *rx_pbuf, struct netif *rx_netif)
{
    // wrap the received pointer
    
    struct wrapper_p_n *local_w_p_n;
    
    local_w_p_n->wp_val_p = &rx_pbuf;
    local_w_p_n->wp_val_n = &rx_netif;
    
    /*Passing *local_w_p_n pointer to another function: Example: */
    /*ex_function(&local_w_p_n)*/
}

int main(int argc, char** argv) {
    
    // give values to local_pbuf and netif
    struct pbuf local_pbuf;
    local_pbuf.a = 1;
    local_pbuf.b = 2;
    
    struct netif local_netif;
    local_netif.c = 3;
    local_netif.d = 4;
    
    //passing pbuf- and netif-stuct to function
    rx_local_p_n(&local_pbuf, &local_netif);
        
    return 0;
}

3 Answers

In the function void rx_local_p_n you pass in pointers to the pbuf and netif struct. These are already pointers and do not need to be assigned to your wrapper struct using the address of operator (&): by doing so you are getting the memory location of the pointer itself. That is why it is complaining about not being able to convert pbuf** to pbuf*.

Solution

  1. local_w_p_n->wp_val_p = &rx_pbuf; to local_w_p_n->wp_val_p = rx_pbuf;
  2. local_w_p_n->wp_val_n = &rx_netif; to local_w_p_n->wp_val_n = rx_netif;

Here in the function params, struct pbuf *rx_pbuf, struct netif *rx_netif

Are already pointers, you do not need to get the address of the pointers, the & is useful when your variables are allocated on the stack and the function call needs a pointer.

As a result, this is the code change

local_w_p_n->wp_val_p = rx_pbuf;
local_w_p_n->wp_val_n = rx_netif;

In the original posted code local_w_p_n was defined as a pointer but not initialized; the attempts to assign members should crash since local_w_p_n would be leftover stack data, not a valid address. Try this example:

void rx_local_p_n(struct pbuf *rx_pbuf, struct netif *rx_netif)
{
    // define as a struct instead of a struct *
    struct wrapper_p_n local_w_p_n;

    // assign from func args without &
    // switch from -> to .
    local_w_p_n.wp_val_p = rx_pbuf;
    local_w_p_n.wp_val_n = rx_netif;

    x_function(&local_w_p_n);
}
Related