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

3 回答 3

4

在函数内部clear_sensors_struc,这样做确实是正确的:

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

这是正确的,因为(在函数内部clear_sensors_struc):

  • sensors_struct: 是指向结构的指针。
  • sensors_struct->st_circular_buffer: 取消引用sensors_struct(使用->)并允许您访问其成员st_circular_buffer
  • &(sensors_struct->st_circular_buffer): 是指向恰好是 structst_circular_buffer的 struct 成员的指针。sensors_structstruct_circ_buff

由于该函数clearCircularBuffer需要一个指针,因此它将编译并正常工作。

关于清理结构数组的函数,这个呢?:

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));
    }
}
于 2013-07-29T15:52:36.453 回答
2

Per Nicolás 的例子:

clearCircularBuffer(&(sensors_struct->st_circular_buffer));

我们可以直接使用

clearCircularBuffer(&sensors_struct->st_circular_buffer);

因为->优先于&.

于 2016-03-07T23:22:24.447 回答
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;
        }    
    } 
    
于 2013-07-29T16:18:59.347 回答