7

The problem is that I have a struct that is member of another (major) struct. I've written a function to clear the first struct (it takes a pointer to struct).

I would like to use that function to clear the struct inside the major structure, but I don't know exactly which is the correct way of doing that.

To explain it better, here is some code:

I have a structure, defined as:

typedef struct
{
    unsigned char next;
    unsigned char first;
    unsigned long data[TCP_RX_BUFFER+1]; 
}struct_circ_buff;

and a function to clear it:

void clearCircularBuffer(volatile struct_circ_buff *circular_buffer)
{
    int i=0;

    for(i=0;i<TCP_RX_BUFFER+1;i++)
        circular_buffer->data[i]=0;

    circular_buffer->first=0;
    circular_buffer->next=0;
}

Then, I have another struct which includes struct_circ_buff:

typedef struct 
{
    volatile unsigned char sensorType;
    volatile uint16_t  sensorFlag;
    volatile struct_circ_buff st_circular_buffer;
}struct_sens;

and I would like to write a function that would clean this struct, using the clearCircularBuffer function written above. How could I do that?

void clear_sensors_struc (volatile struct_sens *sensors_struct)
{

sensors_struct->sensorFlag=0;
sensors_struct->tipoSensor=0;

    //NOW, HOW CAN I USE clearCircularBuffer to clean sensors_struct->                      
    //st_circular_buffer??

    //this way compiles fine, but i don´t think it´s correct
    clearCircularBuffer(&(sensors_struct->st_circular_buffer));

    //this way wouldn´t even compile
    clearCircularBuffer(sensors_struct->st_circular_buffer));
} 

Finally, I have a variable declared as:

struct_sens struct_sensores[MAX_NUMBER_OF_SENSORS];

and I would like to write a function that would clean that array of structures...

So how could I use clear_sensors_struc function to do that?

void clear_sensors_struc_array(struct_sens *sensors_struct)
{
    struct_sens aux_str[MAX_NUMBER_OF_SENSORS];  
    int i=0;    

    for(i=0;i<MAX_NUMBER_OF_SENSORS;i++)
    {         
        clear_sensors_struc(&aux_str[i]);
        *(sensors_struct+i)=aux_str[i];
    }
}

Is there any way of doing that without defining an internal struct_sens aux_str?

4
  • 8
    //this way compiles fine, but i don´t think it´s correct <- Well, it is correct. Jul 29, 2013 at 15:35
  • This might help : stackoverflow.com/questions/6851848/…
    – R3D3vil
    Jul 29, 2013 at 15:36
  • 1
    Why you don't save a (strcut_circle_buf *) pointer in struct_sens instead of a struct_circle_buf . The things goes clear .
    – Lidong Guo
    Jul 29, 2013 at 15:39
  • @DanielFischer, thank you for reading and answering! Nisarg: thank you! I had already seen that post and i took some positive things from it but my case was slightly different. LidongGuo: thank you for the suggestion; I´m gonna analyze it.
    – Rodring
    Jul 29, 2013 at 18:15

3 Answers 3

4

Inside the function clear_sensors_struc, it is indeed correct to do:

//this way compiles fine, but i don´t think it´s correct
clearCircularBuffer(&(sensors_struct->st_circular_buffer));

It's right because (inside function clear_sensors_struc):

  • sensors_struct: is a pointer to a struct.
  • sensors_struct->st_circular_buffer: dereferences sensors_struct (using ->) and allows you to access its member st_circular_buffer.
  • &(sensors_struct->st_circular_buffer): is a pointer to the member st_circular_buffer of struct sensors_struct that happens to be a struct struct_circ_buff.

As the function clearCircularBuffer requires a pointer, it will compile and work right.

Regarding the function to clean the array of structs, what about this?:

void clear_sensors_struc_array(struct_sens *sensors_struct)
{
    int i=0;    

    for(i=0;i<MAX_NUMBER_OF_SENSORS;i++)
    {         
        clear_sensors_struc((sensors_struct+i));
    }
}
1
  • Thank you Nicolas so much for the explanation!
    – Rodring
    Jul 29, 2013 at 18:19
2

Per Nicolás' example:

clearCircularBuffer(&(sensors_struct->st_circular_buffer));

We can directly use

clearCircularBuffer(&sensors_struct->st_circular_buffer);

Because -> gets precedence over &.

0
  1. First of all you have not defined the structures correctly. Define the structures as below:

    typedef struct one
    {
        unsigned char next;
        unsigned char first;
        unsigned long data[11]; 
    }struct_circ_buff;
    
    typedef struct two
    {
        volatile unsigned char sensorType;
        volatile int  sensorFlag;
        volatile struct_circ_buff st_circular_buffer;
    }struct_sens;
    
  2. And you asked:

    I would like to write a function that would clean this struct, using the clearCircularBuffer function written above. How could I do that?`

    You can do it as below:

    void clear_sensors_struc (volatile struct_sens *sensors_struct)
    {
    
        int i;
    
        sensors_struct->sensorFlag=0;
        sensors_struct->sensorType=0;
        sensors_struct->st_circular_buffer.next=0;
        sensors_struct->st_circular_buffer.first=0;
    
        for(i=0;i<TCP_RX_BUFFER+1;i++)
        {
            sensors_struct->st_circular_buffer.data[i]=0;
        }    
    } 
    
1
  • Its a good way of cleaning the array of struct_sens, but in this case,I would like to make the most of 'clearCircularBuffer' function. Of course I could do it as you said, but the question was basically about structures and pointers handling.
    – Rodring
    Jul 29, 2013 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.