Вы находитесь на странице: 1из 9

Department of Computer and Information Science,

School of Science, IUPUI

CSCI 230

Structures
Declarations
Dale Roberts, Lecturer
Computer Science, IUPUI
E-mail: droberts@cs.iupui.edu

Dale Roberts

Introduction
Structures
A collection of one or more variables, possibly of different types,
grouped together under a single name for convenient handling.
Commonly used to define records to be stored in files
Combined with pointers, can create linked lists, stacks, queues, and
trees
Example:

struct card {
char *face;
char *suit;
};

struct introduces the definition for structure card


card is the structure name and is used to declare variables of the
structure type
card contains two members of type char *
These members are face and suit
Dale Roberts

Structure Definitions
Example:
A date consists of several parts, such as the day, month, and year, and the day of the
year, and the month name
struct date {

int day;
int month;
int year;
int year_date;
char month_name[4];
};

date: the name of the structure, called structure tag.


day, month, : the elements or variables mentioned in a structure
are called members.

struct information
A struct cannot contain an instance of itself
Can contain a member that is a pointer to the same structure type
A structure definition does not reserve space in memory
Instead creates a new data type used to declare structure variables

Dale Roberts

Declaration of Variables of Structure


Declarations
method 1: declared like other variables: declare tag first, and then
declare variable.
struct card {
char *face;
char *suit;
};
struct card oneCard, deck[ 52 ],
*cPtr;

struct date {
.. .. ..
};
struct date d1, d2, d3, d4, d5;

method 2: A list of variables can be declared after the right brace and
use comma separated list:
struct card {
char *face;
char *suit;
} oneCard, deck[ 52 ], *cPtr;

struct date {
.. .. ..
} d1, d2, d3;
struct date d4, d5;

method 3: Declare only variables.


struct {
char *face;
char *suit;
} oneCard, deck[ 52 ], *cPtr;

Dale Roberts

struct {
.. .. ..
} d1, d2, d3, d4, d5;

Structure Definitions
Valid Operations
Assigning a structure to a structure of the same type
Taking the address (&) of a structure
Accessing the members of a structure
Using the sizeof operator to determine the size of a structure

Initialization of Structures
Initializer lists
Example:
struct card oneCard = { "Three", "Hearts" };
Example:
struct date d1 = {4, 7, 1776, 186, Jul};
struct date d2 = {4, 7, 1776, 186, {J,u,l,\0}};

Assignment statements
Example:
struct card threeHearts = oneCard;

Dale Roberts

Accessing Members of Structures


Accessing structure members
Dot (.) is a member operator used with structure
variables
Syntax: structure_name.member
struct card myCard;
printf( "%s", myCard.suit );
One could also declare and initialize threeHearts as follows:
struct card threeHearts;
threeHearts.face = Three;
threeHearts.suit = Hearts;

Arrow operator (->) used with pointers to structure


variables
struct card *myCardPtr = &myCard;
printf( "%s", myCardPtr->suit );
myCardPtr->suit is equivalent to (*myCardPtr).suit
Dale Roberts

Structures
Structure can be nested

Name Rule
Members in different structure
can have the same name, since
they are at different position.

struct date {
int day;
int month;
int year;
int year_date;
char month_name[4];
};
struct person {
char name [NAME_LEN];
char address[ADDR_LEN};
long zipcode;
long ss__number;
double salary;
};

struct date birthday;

struct s1 {
.. .. .. ..
char name[10];
.. .. .. ..
} d1;
struct s2 {
.. .. .. ..
int name;
.. .. .. ..
} d2;
struct s3 {
.. .. .. ..
int name;
struct s2 t3;
.. .. .. ..
} d3;
float name;

struct person emp;


emp.birthday.month = 6;
emp.birthday.year = 1776;

Dale Roberts

Memory Layout
Example:

struct data1 {
int day1;
char month[9];
int year;
};

Word (2 bytes) alignment machine begins (aligns)


at even address, such as PC, SUN workstation
day1
month
(hole)
year

int
char array
int

2
9
1
2

bytes
bytes
bytes
bytes

Quad (4 bytes) address alignment begins (aligns)


at quad address, such as VAX 8200
day1
month
(hole)
year

int
char array
int

4
9
3
4

bytes
bytes
bytes
bytes

0
1
2 - 10
11
12
13

0-3
4 - 12
13 - 15
16-19

integer
9 character
(hole)
integer

integer
9 character
(hole)
integer

You must take care of hole, if you want to access data from very low level (i.e.
low-level I/O, byte operations, etc.)

Dale Roberts

sizeof Operator
sizeof(struct tag)
struct test {
char name[5];
int i;
char s;
} t1, t2;

/* assume int is 2 bytes */

main()
{
printf(sizeof(struct test) = %d\n, sizeof (struct test));
printf(address of t1 = %d\n, &t1);
printf(address of t2 = %d\n, &t2);
t1 992
5 bytes
printf(address of t1.name = %d\n, t1.name);
printf(address of t1.i = %d\n, &t1.i);
1 byte (hole)
997
printf(address of t1.s = %d\n, &t1.s);
2 bytes
998
}

output:
sizeof(struct test) = 10
address of t1 = 992
address of t2 = 1002
address of t1.name = 992
address of t1.i = 998
address of t1.s = 1000

t2

1000
1001
1002

1 byte
1 byte (hole)
5 bytes
1 byte (hole)
2 bytes
1 byte
1 byte (hole)

Dale Roberts

Вам также может понравиться