Professional Documents
Culture Documents
Alan L. Cox
alc@rice.edu
Structures can be created and accessed using
pointers. A pointer variable of a structure can
be created as below: struct name
{ member1; member2; . . }; int main()
{ struct name *ptr; } Here, the pointer
variable of type struct name is created.
date.month = 2;
date.day = 4;
date.year = 2021;
foo.c1 = ’a’;
foo.c2 = ’b’;
foo.i = 0xDEADBEEF;
c1 c2 padding i
61 62 EF BE AD DE
x86 uses “little-endian” representation
int64_t i = 100000000000;
Date d = { 2, 4, 2021 };
#define SIZE 10
Define once,
int array[10]; int array[SIZE]; use throughout
the program
Date birthdays[NFRIENDS];
bool
check_birthday(Date today)
{
int i; Array index, then
structure field
for (i = 0; i < NFRIENDS; i++) {
if ((today.month == birthdays[i].month) &&
(today.day == birthdays[i].day))
return (true);
return (false);
}
Padded to be an integral
number of words f1 f2 f3
Placement is compiler- 1 1 0 1 1 0 … …
specific.
c padding
EF BE AD DE
i
?
union AnElt {
int i;
char c;
How should your program keep track
} elt1, elt2;
whether elt1, elt2 hold an int or
elt1.i = 4;
a char?
elt2.c = ’a’;
elt2.i = 0xDEADBEEF;
?
Basic answer: Another variable holds
if (elt1 currently has a char) … that info
Date void
create_date1(int month, create_date2(Date *d,
int day, int month,
int year) Pass-by-reference int day,
{ int year)
Date d; {
d->month = month;
d.month = month; d->day = day;
d.day = day; d->year = year;
d.year = year; }
return (d);
Date today;
}
enum boolean
{
false;
true;
};
enum boolean check;
Here, a variable check is declared which is of
type enum boolean.