Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a struct member as a pointer in a function?

Tags:

c

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?

like image 976
Rodring Avatar asked Jul 29 '13 15:07

Rodring


People also ask

How do you pass a struct pointer to a function?

A structure can be transferred to a function either using call by value or call by reference scheme. Remember that C only implements call by value scheme of parameter transmission. Call by reference is indirectly implemented by passing address of variable.

How can a structure member access a pointer?

To access members of a structure using pointers, we use the -> operator. In this example, the address of person1 is stored in the personPtr pointer using personPtr = &person1; . Now, you can access the members of person1 using the personPtr pointer.

Can you have a function pointer in a struct?

Function pointers can be stored in variables, structs, unions, and arrays and passed to and from functions just like any other pointer type. They can also be called: a variable of type function pointer can be used in place of a function name.

Can you pass a struct into a function in C?

To pass a structure variable to a function all we have to do is write the name of the variable and it will pass a copy of the structure variable.


2 Answers

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));
    }
}
like image 118
Nicolás Ozimica Avatar answered Oct 16 '22 06:10

Nicolás Ozimica


Per Nicolás' example:

clearCircularBuffer(&(sensors_struct->st_circular_buffer));

We can directly use

clearCircularBuffer(&sensors_struct->st_circular_buffer);

Because -> gets precedence over &.

like image 40
Majnu Avatar answered Oct 16 '22 06:10

Majnu