2013-07-29 8 views
5

Prima di tutto, vorrei dire che ho esaurientemente cercato la risposta in questo forum prima di pubblicare questa domanda, ma non ho trovato alcuna risposta.Come passare un membro della struct come puntatore in una funzione

OK, il problema è che ho una struttura che è membro di un'altra (principale) struttura. Ho scritto una funzione per cancellare la prima struttura (ci vuole un puntatore a struct). Vorrei usare quella funzione per cancellare la struttura all'interno della struttura principale, ma non so esattamente quale sia il modo corretto di farlo.

Per spiegare meglio, ecco alcune code:

ho una struttura, definita come:

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

e una funzione per cancellarlo:

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; 
} 

Poi, ho avere un'altra struttura che include struct_circ_buff:

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

e vorrei scrivere una funzione che pulisse questa struttura, usando la funzione 'clearCircularBuffer' scritta sopra. Come potrei farlo?

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)); 
} 

Infine, ho una variabile dichiarata come:

struct_sens struct_sensores[MAX_NUMBER_OF_SENSORS]; 

e mi piacerebbe scrivere una funzione che avrebbe pulire tale matrice di strutture ... Così come potrei usare 'clear_sensors_struc' funzione per farlo?

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]; 
    } 
} 

C'è un modo per farlo senza definire una struct_sens aux_str interna?

OK, spero che qualcuno possa aiutarmi! Grazie in anticipo!

+7

'// questo modo compila bene, ma Non penso E'correct' <- Beh, _is_ corretto. –

+0

Questo potrebbe aiutare: http://stackoverflow.com/questions/6851848/passing-struct-pointer-to-functions-not-working – R3D3vil

+0

Perché non si salva un puntatore (strcut_circle_buf *) in struct_sens invece di struct_circle_buf. Le cose vanno bene. –

risposta

3

All'interno della funzione clear_sensors_struc, è infatti corretto per farlo:

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

E 'proprio a causa (funzione clear_sensors_struc all'interno):

  • sensors_struct: è un puntatore ad una struct.
  • sensors_struct->st_circular_buffer: dereferenziazioni sensors_struct (utilizzando ->) e ti permette di accedere ai suoi Stati st_circular_buffer.
  • &(sensors_struct->st_circular_buffer): è un puntatore all'elemento st_circular_buffer di struct sensors_struct che sembra essere una struttura struct_circ_buff.

Poiché la funzione clearCircularBuffer richiede un puntatore, verrà compilata e funzionerà correttamente.

Per quanto riguarda la funzione di pulire l'array di struct, che dire di questo ?:

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)); 
    } 
} 
+0

Grazie mille Nicolas per la spiegazione! – Rodring

+0

E la funzione 'clear_sensors_struc_array'? –

0

1) Prima di tutto non è stata definita correttamente le strutture. definire le strutture, come di seguito

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) E hai chiesto

I would like to write a function that would clean this struct, using the 'clearCircularBuffer' function written above. How could I do that? si può fare come qui sotto

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; 
    }  

} 
+0

È un ottimo modo per pulire la matrice di struct_sens, ma in questo caso, vorrei sfruttare al massimo la funzione 'clearCircularBuffer'. Ovviamente potrei farlo come hai detto tu, ma la domanda riguardava fondamentalmente le strutture e la gestione dei puntatori. – Rodring

0

Nel Nicolas es clearCircularBuffer (& (sensors_struct-> st_circular_buffer));

possiamo utilizzare direttamente

clearCircularBuffer (& sensors_struct-> st_circular_buffer);

-> ottiene la precedenza su &

Problemi correlati