t15AStructuresDeclarations

t15AStructuresDeclarations - Structures Declarations Dale...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Structures Declarations Dale Roberts www.kite-legends.blogspot.com Introduction Structures A collection of one or more variables, possibly of different types, collection grouped together under a single name for convenient handling. grouped Commonly used to define records to be stored in files Combined with pointers, can create linked lists, stacks, queues, and Combined trees trees Example: struct card { char *face; char char *suit; char }; struct introduces the definition for structure card struct card is the structure name and is used to declare variables of the card structure type card contains two members of type char * card char These members are face and suit These face suit Dale Roberts www.kite-legends.blogspot.com 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 year, struct date { int day; int month; int year; int year_date; char month_name[4]; }; date: the name of the structure, called structure tag. date the structure day, month, …: the elements or variables mentioned in a structure day month …: are called members. members struct information A struct cannot contain an instance of itself struct Can contain a member that is a pointer to the same structure type A structure definition does not reserve space in memory structure Instead creates a new data type used to declare structure variables Instead Dale Roberts www.kite-legends.blogspot.com Declaration of Variables of Structure Declarations method 1: declared like other variables: declare tag first, and declared then declare variable. declare struct card { char *face; char *suit; }; struct card oneCard, deck[ 52 ], *cPtr; *cPtr; struct date { .. .. .. }; struct date d1, d2, d3, d4, d5; method 2: A list of variables can be declared after the right brace list and use comma separated list: use 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; struct { .. .. .. } d1, d2, d3, d4, d5; Dale Roberts www.kite-legends.blogspot.com Structure Definitions Valid Operations Assigning a structure to a structure of the same type Assigning Taking the address (&) of a structure Taking of Accessing the members of a structure Using the sizeof operator to determine the size of a structure sizeof Initialization of Structures Initializer lists Example: struct card oneCard = { "Three", "Hearts" }; struct 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 www.kite-legends.blogspot.com Accessing Members of Structures Accessing structure members Dot (.) iis a member operator used with structure s variables variables Syntax: structure_name.member Syntax: structure_name.member struct card myCard; struct printf( "%s", myCard.suit ); printf( One could also declare and initialize threeHearts as follows: One threeHearts struct card threeHearts; threeHearts.face = “Three”; threeHearts.suit = “Hearts”; Arrow operator (->) used with pointers to structure used variables variables struct card *myCardPtr = &myCard; Dale myCardPtr->suit ); printf( "%s",Roberts www.kite-legends.blogspot.com Structures Structure can be nested 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; • 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 { .. .. .. .. int name; struct s2 t3; .. .. .. .. } d3; float name; struct person emp; emp.birthday.month = 6; emp.birthday.year = 1776; Dale Roberts www.kite-legends.blogspot.com Memory Layout Example: struct data1 { struct int day1; char month[9]; int year; }; Word (2 bytes) alignment machine – begins (aligns) at even address, such as PC, SUN workstation day1 int month char array (hole) year int int 2 9 1 2 bytes bytes bytes bytes bytes 0 1 2 - 10 11 12 13 integer 9 character (hole) integer Quad (4 bytes) address alignment – begins (aligns) at quad address, such as VAX 8200 day1 int month char array (hole) year int int 4 9 3 4 bytes bytes bytes bytes bytes 0-3 4 - 12 13 - 15 16-19 integer 9 character (hole) integer You must take care of hole, iif you want to access data from very low level (i.e. You hole f low-level I/O, byte operations, etc.) low-level Dale Roberts www.kite-legends.blogspot.com sizeof Operator 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 www.kite-legends.blogspot.com ...
View Full Document

This note was uploaded on 12/08/2010 for the course CSE 2017 taught by Professor Kittu during the Spring '10 term at APIIT.

Ask a homework question - tutors are online