First of all, I would like to say that I've exhaustively searched for the answer in this forum before post this question, but I didn´t find any answer.
OK, 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?
OK, hope someone can help me! Thanks in advance!
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));
}
}
In Nicolas eg clearCircularBuffer(&(sensors_struct->st_circular_buffer));
we can directly use
clearCircularBuffer(&sensors_struct->st_circular_buffer);
-> gets precedence over &
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.