Structures
Structures
Structures
Structures and Unions
struct motor p, q, r;
• Declares and sets aside storage for three variables –
p, q, and r – each of type struct motor
struct motor M[25];
• Declares a 25-element array of struct motor;
allocates 25 units of storage, each one big enough to
hold the data of one motor
struct motor *m;
• Declares a pointer to an object of type struct
motor
Structures
struct ADate {
int month;
int day;
int year;
};
date.month = 9;
date.day = 1;
date.year = 2005;
date1.month = 9;
date1.day = 1;
date1.year = 2005;
date2 = date1 ;
date2.month = date1.month;
date2.day = date1.day;
date2.year = date1.year;
More Examples
• struct SSN { • struct time {
int first_three; int hours;
char dash1; int minutes;
int second_two; int seconds;
char dash2; } time_of_birth = { 8, 45, 0 };
int last_four;
};
struct SSN customer_ssn ;
• struct date {
char month[2];
char day[2];
char year[4];
} current_date ;
Structure Representation & Size
•sizeof(struct …) =
• sum of sizeof(field) struct CharCharInt {
•+ alignment padding char c1;
Processor- and compiler-specific char c2;
int i;
} foo;
foo.c1 = ’a’;
foo.c2 = ’b’;
foo.i = 0xDEADBEEF;
c1 c2 padding i
61 62 EF BE AD DE
x86 uses “little-endian” representation
Accessing Members of a struct
Repeat
struct motor {
float volts;
• Let
float amps;
struct motor p; int phases; otor
struct motor q[10]; float rpm;
• Then };
p.volts — is the voltage
p.amps — is the amperage
p.phases — is the number of phases
p.rpm — is the rotational speed
• Let
s e s ?
struct motor *p;nthe
pa re
• Then Why the
(*p).volts — is the voltage of the motor pointed
to by p
(*p).phases — is the number of phases of the
motor pointed to by p
Accessing Members of a struct (continued)
• Let e r a t or
o p ce
struct motor ' . '
*p;eced e n
a us e r
Be c e r p
• g
Then has h ary '*'
i h
n u n
tha — is the voltage of the motor pointed
(*p).volts
to by p
(*p).phases — is the number of phases of the
motor pointed to by p
Accessing Members of a struct (continued)
• Let
struct motor *p;
• Then
(*p).volts — is the voltage
Reason:– of the
you really motor
want pointed
the expression
to by p
m.volt * m.amps
(*p).phases — is the number of phases of the
to mean what you think it should mean!
motor pointed to by p
Accessing Members of a struct (continued)
• Let
struct motor *p;
• Then
p -> volts — is the voltage of the motor pointed
to by p
p -> phases — is the number of phases of the
motor pointed to by p
Operations on struct
• Copy/assign
struct motor p, q;
p = q;
• Get address
struct motor p;
struct motor *s
s = &p;
• Access members
p.volts;
s -> amps;
Initialization of a struct
• Let struct motor {
float volts;
float amps;
int phases;
float rpm;
}; //struct motor
• Then
struct motor m = {208, 20, 3,
1800};
initializes the struct
Why structs AGAIN???
struct Line {
struct Point pt1;
struct Point pt2;
};
struct Line l1;
Nesting Structures
struct Point {
l1
char name[30];
pt1 pt2
int x;
int y;
};
x y Name x y
struct Line { Name
10
struct Point pt1;
struct Point pt2;
}; To Access the Elements
struct Line l1;
l1.pt1.x=10;
struct Point {
char name[30];
Array of Structures int x;
int y;
};
• Array of Structures act like any other array.
struct Point pt[3];
pt.namePtr=(char *) malloc(20*sizeof(char));
*(pt.namePtr)=“lastPoint”;
pt
namePtr x y
lastPoint \0
Pointer to Structures
n1 n2
value nextPtr value nextPtr
10 0x2000 20 NULL
0x1000 0x2000
Typedef
• Use typedef for creating new data type names
• typedef int length;
this the name length a synonym (alias) for int. Afterwards, you can
do: length x = 4;
• In context of structs, you can do:
union AnElt {
int i;
char c;
} elt1, elt2;
elt1.i = 4;
elt2.c = ’a’;
elt2.i = 0xDEADBEEF;
c padding
EF BE AD DE
i
Unions
• Storage
– size of union is the size of its largest member
– avoid unions with widely varying member
sizes;
for the larger data types, consider using
pointers instead
• Initialization
– Union may only be initialized to a value
appropriate for the type of its first member
Bit-fields
… /* In another file */
struct _item {
char *info;
Item *nextItem;
};
Questions?