Structures
UNIONS et champs de bits
Une
structure est un tableau dans lequel les variables peuvent être de types
différents
#include <stdio.h>
#include <string.h>

struct
identite { char nom[30] ;
char prenom[30] ;
int age ;
unsigned int tel;
} classe[10] ;
char i;
int
main()
{
strcpy(classe[0].nom,"dupont") ;
strcpy(classe[0].prenom,"pierre") ;
classe[0].age=40 ;
classe[0].tel=4458;
strcpy(classe[1].nom,"durand") ;
strcpy(classe[1].prenom,"paul") ;
classe[1].age=30 ;
classe[1].tel=4454;
printf("/n/nprenom /tnom /tage /ttel/n");
for(i=0;i<2;i++) { printf("%s/t%s/t%d/t%d/n",classe[i].prenom,classe[i].nom,classe[i].age,classe[i].tel);
}
return 0;
}
On
peut créer une structure « champ de bits ». Le premier élément est le bit 0. Le
nom de l’élément est suivi du nombre de bits utilisés.
struct
{
unsigned RB0:1;
unsigned RB1:1;
unsigned RB2:1;
unsigned RB3:1;
unsigned GROUPE:3;
unsigned RB7:1;
} PORTBbits ;
Bit 7 |
Bit 6 |
Bit 5 |
Bit 4 |
Bit 3 |
Bit 2 |
Bit 1 |
Bit 0 |
RB7 |
GROUPE |
RB3 |
RB2 |
RB1 |
RBO |
2.
Union
Dans une UNION les
champs partagent les mêmes adresses.
volatile
near union {
struct {
unsigned RE0:1;
unsigned RE1:1;
unsigned RE2:1;
unsigned RE3:1;
unsigned RE4:1;
unsigned RE5:1;
unsigned RE6:1;
unsigned RE7:1;
} ;
struct {
unsigned ALE:1;
unsigned OE:1;
unsigned WRL:1;
unsigned WRH:1;
unsigned :3;
unsigned CCP2:1;
} ;
struct {
unsigned AN5:1;
} ;
} PORTEbits ;
|