Structures and Unions in C: Slides Taken From: Alan L. Cox Computer Science Department Rice University Texas

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 11

Structures and Unions in C

Slides taken from: Alan L. Cox


Computer Science Department
Rice University
Texas
Structures
Compound data:

A date is
an int month and
an int day and
an int year
struct ADate {
int month;
int day;
int year;
};

struct ADate date;

date.month = 9;
date.day = 1;
date.year = 2005;

Structure Representation & Size
sizeof(struct ) =
sum of sizeof(field)
+ alignment padding
Processor- and compiler-specific
62 61 EF BE AD DE
c1 c2 i
padding
struct CharCharInt {
char c1;
char c2;
int i;
} foo;

foo.c1 = a;
foo.c2 = b;
foo.i = 0xDEADBEEF;
Typedef
Mechanism for creating new type names
New names are an alias for some other type
Improves clarity of the program
typedef long int64_t;
typedef struct ADate {
int month;
int day;
int year;
} Date;

int64_t i = 100000000000;
Date d = {9, 1, 2005};
Overload existing type
names for clarity and
portability
Simplify complex type names
Constants
Allow consistent use of the same constant
throughout the program
Improves clarity of the program
Reduces likelihood of simple errors
Easier to update constants in the program


int array[10];

for (i=0; i<10; i++) {

}
#define SIZE 10

int array[SIZE];

for (i=0; i<SIZE; i++) {

}
Preprocessor directive
Constant names are
capitalized by convention
Define once,
use throughout
the program
Arrays of Structures
Date birthdays[NFRIENDS];

bool
check_birthday(Date today)
{
int i;

for (i = 0; i < NFRIENDS; i++) {
if ((today.month == birthdays[i].month) &&
(today.day == birthdays[i].day))
return (true);
}

return (false);
}
Constant
Array declaration
Array index, then
structure field
Pointers to Structures
Date
create_date1(int month,
int day,
int year)
{
Date d;

d.month = month;
d.day = day;
d.year = year;

return (d);
}
void
create_date2(Date *d,
int month,
int day,
int year)
{
d->month = month;
d->day = day;
d->year = year;
}
Copies date
Pass-by-reference
Date today;

today = create_date1(9, 4, 2008);
create_date2(&today, 9, 4, 2008);
Pointers to Structures (cont.)
void
create_date2(Date *d,
int month,
int day,
int year)
{
d->month = month;
d->day = day;
d->year = year;
}

void
foo(void)
{
Date today;
create_date2(&today, 9, 4, 2008);
}
today.month:
today.day:
today.year:
0x1000
0x1004
0x1008
month: 9
day: 4
year: 2008
0x30A0
0x30A4
0x30A8
d: 0x1000 0x3098
9
4
2008
Pointers to Structures (cont.)
Date *
create_date3(int month,
int day,
int year)
{
Date *d;

d->month = month;
d->day = day;
d->year = year;

return (d);
}
What is d pointing to?!?!
Unions
Choices:

An element is
an int i or
a char c

sizeof(union ) =
maximum of sizeof(field)



EF BE AD DE
c
i
padding
union AnElt {
int i;
char c;
} elt1, elt2;

elt1.i = 4;
elt2.c = a;
elt2.i = 0xDEADBEEF;
Unions
A union value doesnt know which case it
contains
union AnElt {
int i;
char c;
} elt1, elt2;

elt1.i = 4;
elt2.c = a;
elt2.i = 0xDEADBEEF;

if (elt1 currently has a char)
How should your program keep track
whether elt1, elt2 hold an int or
a char?
?
?
Basic answer: Another variable holds
that info

You might also like