Você está na página 1de 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; struct date {
char *suit; .. .. ..
}; };
struct card oneCard, deck[ 52 ], struct date d1, d2, d3, d4, d5;
*cPtr;
method 2: A list of variables can be declared after the right brace and
use comma separated list:
struct card { struct date {
char *face; .. .. ..
char *suit; } d1, d2, d3;
} oneCard, deck[ 52 ], *cPtr; struct date d4, d5;

method 3: Declare only variables.


struct {
struct { .. .. ..
char *face; } d1, d2, d3, d4, d5;
char *suit;
} oneCard, deck[ 52 ], *cPtr;
Dale Roberts
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
struct date { Members in different structure
int day; can have the same name, since
int month; they are at different position.
int year;
int year_date;
char month_name[4]; struct s1 {
}; .. .. .. ..
char name[10];
struct person { .. .. .. ..
char name [NAME_LEN]; } d1;
char address[ADDR_LEN};
long zipcode; struct s2 {
long ss__number; .. .. .. ..
double salary; int name;
.. .. .. ..
struct date birthday; } d2;
};
struct s3 {
struct person emp; .. .. .. ..
int name;
emp.birthday.month = 6; struct s2 t3;
emp.birthday.year = 1776;
.. .. .. ..
} d3;
float name;
Dale Roberts
Memory Layout
Example: struct data1 {
int day1;
char month[9];
int year;
};
0
Word (2 bytes) alignment machine begins (aligns) integer
1
at even address, such as PC, SUN workstation 2 - 10 9 character
day1 int 2 bytes
month char array 9 bytes 11 (hole)
(hole) 1 bytes 12 integer
year int 2 bytes 13

Quad (4 bytes) address alignment begins (aligns)


at quad address, such as VAX 8200 0-3 integer
day1 int 4 bytes
4 - 12
month char array 9 bytes 9 character
(hole) 3 bytes 13 - 15 (hole)
year int 4 bytes 16-19 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; /* assume int is 2 bytes */
char s;
} t1, t2;
main()
{
printf(sizeof(struct test) = %d\n, sizeof (struct test));
printf(address of t1 = %d\n, &t1);
printf(address of t2 = %d\n, &t2);
printf(address of t1.name = %d\n, t1.name); t1 992 5 bytes
printf(address of t1.i = %d\n, &t1.i);
printf(address of t1.s = %d\n, &t1.s); 997 1 byte (hole)
} 998 2 bytes
1000 1 byte
output: 1001 1 byte (hole)
t2 1002 5 bytes
sizeof(struct test) = 10
address of t1 = 992 1 byte (hole)
address of t2 = 1002 2 bytes
address of t1.name = 992 1 byte
address of t1.i = 998
1 byte (hole)
address of t1.s = 1000
Dale Roberts

Você também pode gostar