0

I write generic application for handle as many modbus devices as defined in JSON config file. mbus_servers struct used in my_modbus_init() contents ip of device and port.

Initialization of one modbus_t in my_modbus_init() looks like this:

modbus_t * my_modbus_init(const char * ip_s, int port, int slave_id)
{
    modbus_t *ctx;


    ctx = modbus_new_tcp(ip_s, port);
    if(ctx == NULL)
    {
      printf("Error");
    }

    modbus_set_debug(ctx, 0);

    if (modbus_connect(ctx) == -1)
    {
        fprintf(stderr, "Connection failed: %s\n",modbus_strerror(errno));
        modbus_free(ctx);
        return NULL;
    }

    modbus_set_slave(ctx, slave_id);

    return ctx;
}

Now I try to initialize dynamiclly allocated number of modbus_t:

modbus_t * my_modbus;
int quantity_of_connections_modbus = 3;

my_modbus = (modbus_t *) malloc (quantity_of_connections_modbus * sizeof(modbus_t));
                                                                  ^here I get invalid application of ‘sizeof’ to incomplete type ‘modbus_t' (I know that is because i dont have acces to definition of modbus_t [it is defined in modbus.h as typedef struct _modbus modbus_t; ])

for(i=0; i< quantity_of_connections_modbus; i++)
{
  // |> my_modbus[i] = my_modbus_init(mbus_servers[i]->host,mbus_servers[i]->port,MBUS_SLAVE); 
  // |- And here error: dereferencing pointer to incomplete type ‘modbus_t {aka struct _modbus}’ 
}

Is there any way to create as many modbus_t as needed using malloc?

pzydziak
  • 89
  • 1
  • 2
  • 10

1 Answers1

0

Instead of having an array of modbus_t objects, have an array of pointers to modbus_t objects. This makes sense since the my_modbus_init function (and the modbus_new_tcp function it calls) returns pointers.

In other words change sizeof(modbus_t) to sizeof(modbus_t *). And change the variable my_modbus to represent this array of pointers (i.e. be a modbus_t **).


The code from the question modified accordingly:

modbus_t ** my_modbus;
int quantity_of_connections_modbus = 3;

my_modbus = malloc (quantity_of_connections_modbus * sizeof(modbus_t*));

for(i=0; i< quantity_of_connections_modbus; i++)
{
  my_modbus[i] = my_modbus_init(mbus_servers[i]->host,mbus_servers[i]->port,MBUS_SLAVE); 
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    This implies `modbus_t **my_modbus;` – LPs Jun 13 '17 at 06:47
  • No, as seen from code he wants to allocate memory for 3 modbuses in a row and then using for loop to address it. – unalignedmemoryaccess Jun 13 '17 at 06:47
  • 1
    @tilz0R Since the `modbus_t` is an opaque type (like `FILE`), it's not possible any other way. Besides, the functions used to create the `modbus_t` object actually returns *pointers* so having an array of pointers just makes sense. Looping over an array of pointers to objects is no different than looping over an array of objects. – Some programmer dude Jun 13 '17 at 06:50
  • @Someprogrammerdude Now the corrected code will work. – unalignedmemoryaccess Jun 13 '17 at 06:52