Download presentation
Presentation is loading. Please wait.
1
Structures Declarations CSCI 230
Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Declarations Dale Roberts, Lecturer Computer Science, IUPUI
2
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
3
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
4
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; method 2: A list of variables can be declared after the right brace and use comma separated list: } oneCard, deck[ 52 ], *cPtr; method 3: Declare only variables. struct { struct date { }; struct date d1, d2, d3, d4, d5; struct date { } d1, d2, d3; struct date d4, d5; struct { } d1, d2, d3, d4, d5;
5
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" }; struct date d1 = {4, 7, 1776, 186, “Jul”}; struct date d2 = {4, 7, 1776, 186, {‘J’,’u’,’l’,’\0’}}; Assignment statements struct card threeHearts = oneCard;
6
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
7
Structures Structure can be nested Name Rule
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 person emp; emp.birthday.month = 6; emp.birthday.year = 1776; Name Rule Members 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 { struct s2 t3; } d3; float name;
8
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 int 2 bytes month char array 9 bytes (hole) 1 bytes year int 2 bytes Quad (4 bytes) address alignment – begins (aligns) at quad address, such as VAX 8200 day1 int 4 bytes (hole) 3 bytes year int 4 bytes 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.) 1 2 - 10 11 12 13 integer 9 character (hole) 0 - 3 4 - 12 16-19 integer 9 character (hole)
9
sizeof Operator sizeof(struct tag) output: sizeof(struct test) = 10 t1
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); printf(“address of t1.i = %d\n”, &t1.i); printf(“address of t1.s = %d\n”, &t1.s); } 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 2 bytes 5 bytes 1 byte (hole) 1 byte 992 998 1000 1002 1001 997 t2 t1
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.