Download as pps, pdf, or txt
Download as pps, pdf, or txt
You are on page 1of 9

Dale Roberts

Department of Computer and Information Science, Department of Computer and Information Science,
SchooI of Science, IUPUI SchooI of Science, IUPUI
CSCI 230
Structures
Declarations
Dale Roberts, Lecturer
Computer Science, IUPUI
E-mail: drobertscs.iupui.edu
Dale Roberts
Introduction Introduction
Structures Structures
A collection of one or more variables, possibly of different types, A collection of one or more variables, possibly of different types,
grouped together under a single name for convenient handling. grouped together under a single name for convenient handling.
Commonly used to define records to be stored in files Commonly used to define records to be stored in files
Combined with pointers, can create linked lists, stacks, queues, and Combined with pointers, can create linked lists, stacks, queues, and
trees trees
Example: Example:
struct card { struct card {
char *face; char *face;
char *suit; char *suit;
}; };
struct struct introduces the definition for structure card introduces the definition for structure card
card card is the structure name and is used to declare variables of the is the structure name and is used to declare variables of the
structure type structure type
card card contains two members of type contains two members of type char * char *
These members are These members are face face and and suit suit
Dale Roberts
Structure Definitions Structure Definitions
ExampIe ExampIe: :
A date consists of severaI parts, such as the day, month, and year, and the day of the A date consists of severaI parts, such as the day, month, and year, and the day of the
year, and the month name year, and the month name
struct date { struct date {
int day; int day;
int month; int month;
int year; int year;
int year_date; int year_date;
char month_name[4]; char month_name[4];
}; };
date date: the name of the structure, called : the name of the structure, called structure tag structure tag..
day day, , month month, .: the elements or variables mentioned in a structure , .: the elements or variables mentioned in a structure
are called are called 2e2-ers 2e2-ers..
897:.9 897:.9 information information
A A struct struct cannot contain an instance of itself cannot contain an instance of itself
Can contain a member that is a pointer to the same structure type Can contain a member that is a pointer to the same structure type
A structure definition does not reserve space in memory A structure definition does not reserve space in memory
nstead creates a new data type used to declare structure variables nstead creates a new data type used to declare structure variables
Dale Roberts
DecIaration of VariabIes of Structure DecIaration of VariabIes of Structure
DecIarations DecIarations
2ethod 1 2ethod 1:: declared like other variables: declare tag first, and then declared like other variables: declare tag first, and then
declare variable. declare variable.
struct card { struct card {
char *face; char *face;
char *suit; char *suit;
}; };
struct card oneCard, deck[ 52 ], struct card oneCard, deck[ 52 ],
*cPtr; *cPtr;
2ethod 2 2ethod 2:: A list of variables can be declared after the right brace and A list of variables can be declared after the right brace and
use comma separated list: use comma separated list:
struct card { struct card {
char *face; char *face;
char *suit; char *suit;
} oneCard, deck[ 52 ], *cPtr; } oneCard, deck[ 52 ], *cPtr;
2ethod 3 2ethod 3:: eclare only variables. eclare only variables.
struct { struct {
char *face; char *face;
char *suit; char *suit;
} oneCard, deck[ 52 ], *cPtr; } oneCard, deck[ 52 ], *cPtr;
struct date {
.. .. ..
};
struct date d1, d2, d3, d4, d5;
struct date {
.. .. ..
} d1, d2, d3;
struct date d4, d5;
struct {
.. .. ..
} d1, d2, d3, d4, d5;
Dale Roberts
Structure Definitions Structure Definitions
VaIid Operations VaIid Operations
Assigning a structure to a structure of the same type Assigning a structure to a structure of the same type
Taking the address ( Taking the address ( ) of a structure ) of a structure
Accessing the members of a structure Accessing the members of a structure
Using the Using the sizeof sizeof operator to determine the size of a structure operator to determine the size of a structure
InitiaIization of Structures InitiaIization of Structures
nitializer lists nitializer lists
Example Example::
struct card oneCard = { "Three", "Hearts" }; struct card oneCard = { "Three", "Hearts" };
Example Example::
struct date d1 = {4, 7, 1776, 186, Jul}; struct date d1 = {4, 7, 1776, 186, Jul};
struct date d2 = {4, 7, 1776, 186, {`J','u','l',' struct date d2 = {4, 7, 1776, 186, {`J','u','l','\ \0'}}; 0'}};
Assignment statements Assignment statements
Example Example::
struct card threeHearts = oneCard; struct card threeHearts = oneCard;
Dale Roberts
Accessing Members of Structures Accessing Members of Structures
Accessing structure members Accessing structure members
ot ( ot (. .) is a member operator used with structure ) is a member operator used with structure
variables variables
Syntax: Syntax: structure_name.member structure_name.member
struct card myCard; struct card myCard;
printf( "%s", myCard.suit ); printf( "%s", myCard.suit );
One could also declare and initialize One could also declare and initialize threeHearts threeHearts as follows: as follows:
struct card threeHearts; struct card threeHearts;
threeHearts.face = Three; threeHearts.face = Three;
threeHearts.suit = Hearts; threeHearts.suit = Hearts;
Arrow operator ( Arrow operator (- -> >) used with pointers to structure ) used with pointers to structure
variables variables
struct card *myCardPtr = myCard; struct card *myCardPtr = myCard;
printf( "%s", myCardPtr printf( "%s", myCardPtr- ->suit ); >suit );
myCardPtr myCardPtr- ->suit >suit is equivalent to is equivalent to (*myCardPtr).suit (*myCardPtr).suit
Dale Roberts
Structures Structures
Structure can be nested Structure can be nested
struct date { struct date {
int day; int day;
int month; int month;
int year; int year;
int year_date; int year_date;
char month_name[4]; char month_name[4];
}; };
struct person { struct person {
char name [NAME_LEN]; char name [NAME_LEN];
char address[ADDR_LEN}; char address[ADDR_LEN};
long zipcode; long zipcode;
long ss__number; long ss__number;
double salary; double salary;
struct date birthday; struct date birthday;
}; };
struct person emp; struct person emp;
emp.birthday.month = 6; emp.birthday.month = 6;
emp.birthday.year = 1776; emp.birthday.year = 1776;
W ame Rule
W embers in different structure
can have the same name, since
they are at different position.
struct s1 {
.. .. .. ..
char name[10];
.. .. .. ..
} d1;
struct s2 {
.. .. .. ..
int name;
.. .. .. ..
} d2;
struct s3 {
.. .. .. ..
int name;
struct s2 t3;
.. .. .. ..
} d3;
float name;
Dale Roberts
Memory Layout Memory Layout
Example Example:: struct data1 { struct data1 {
int day1; int day1;
char month[9]; char month[9];
int year; int year;
}; };
Word (2 bytes) alignment machine Word (2 bytes) alignment machine begins (aligns) begins (aligns)
at even address, such as PC, SU workstation at even address, such as PC, SU workstation
day1 day1 int int 2 bytes 2 bytes
month month char array char array 9 bytes 9 bytes
(hole) (hole) 1 bytes 1 bytes
year year int int 2 bytes 2 bytes
Quad (4 bytes) address alignment Quad (4 bytes) address alignment begins (aligns) begins (aligns)
at quad address, such as VAX 8200 at quad address, such as VAX 8200
day1 day1 int int 4 bytes 4 bytes
month month char array char array 9 bytes 9 bytes
(hole) (hole) 3 bytes 3 bytes
year year int int 4 bytes 4 bytes
You must take care of You must take care of hole hole, if you want to access data from very low level (i.e. , if you want to access data from very low level (i.e.
low low- -level /O, byte operations, etc.) level /O, byte operations, etc.)

integer
integer
9 character
(hoIe)
-
-
-
-9
integer
integer
9 character
(hoIe)
Dale Roberts
sizeof sizeof Operator Operator
sizeof(struct tag) sizeof(struct tag)
struct test { struct test {
char name[5]; char name[5];
int i; int i; /* assume int is 2 bytes */ /* assume int is 2 bytes */
char s; char s;
} t1, t2; } t1, t2;
main() main()
{ {
printf(sizeof(struct test) = %d printf(sizeof(struct test) = %d\ \n, sizeof (struct test)); n, sizeof (struct test));
printf(address of t1 = %d printf(address of t1 = %d\ \n, t1); n, t1);
printf(address of t2 = %d printf(address of t2 = %d\ \n, t2); n, t2);
printf(address of t1.name = %d printf(address of t1.name = %d\ \n, t1.name); n, t1.name);
printf(address of t1.i = %d printf(address of t1.i = %d\ \n, t1.i); n, t1.i);
printf(address of t1.s = %d printf(address of t1.s = %d\ \n, t1.s); n, t1.s);
} }
output: output:
sizeof(struct test) = 10 sizeof(struct test) = 10
address of t1 = 992 address of t1 = 992
address of t2 = 1002 address of t2 = 1002
address of t1.name = 992 address of t1.name = 992
address of t1.i = 998 address of t1.i = 998
address of t1.s = 1000 address of t1.s = 1000
bytes
bytes
byte (hoIe)
byte
byte (hoIe)
99
998

bytes
bytes
byte (hoIe)
byte
byte (hoIe)

997
t
t

You might also like