Performance Enhancement of TFRC in Wireless Networks

Performance Enhancement of TFRC in Wireless Networks

Structures Systems Programming Structures Structures Typedef Declarations Using Structures with Functions Systems Systems Programming: Programming: Structures 2 2 10.1 Introduction Structures A collection of related variables (aggregates) under one name. Can contain variables of different data types. Commonly used to define records to

be stored in files. When combined with pointers, structures can create linked lists, stacks, queues, and trees. 2007 Pearson Ed -All rights reserved. Systems Programming: Structures 3 Structures Example 1: struct player { char *name; int num; char *team; char *pos; } ; /* Dont forget this semicolon! */ Systems Programming: Structures 4 Typedef Example Example 2: struct card { const char *face; const char *suit; }; typedef struct card Card;

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. 2007 Pearson Ed -All rights reserved. Systems Programming: Structures 5 typedef Another way:: typedef struct { const char *face; const char *suit; } Card; Card deck[52]; Systems Programming: Structures 2007 Pearson Ed -All rights reserved. 6 10.6 typedef Example: typedef struct Card *CardPtr; or

Card *Cardptr; Defines a new type name CardPtr as a synonym for type struct Card * typedef does not create a new data type. It only creates an alias. Capitalize the first letter of typedef names to emphasize that they are synonyms for other type names. 2007 Pearson Ed -All rights reserved. Systems Programming: Structures 7 10.2 Structure Definitions struct information A struct cannot contain an instance of itself. It can contain a member that is a pointer to the same structure type. A structure definition does not reserve space in memory. Rather a struct creates a new data type used to define structure variables.

Definitions Defined like other variables: card oneCard, deck[ 52 ], *cPtr; Can use a comma separated list: struct card { char *face; char *suit; } oneCard, deck[ 52 ], *cPtr; 2007 Pearson Ed -All rights reserved. Systems Programming: Structures 8 10.2 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. 2007 Pearson Ed -All rights reserved. Systems Programming: Structures 9

10.3 Initializing Structures Initializer lists Example: card oneCard = { "Three", "Hearts" }; Assignment statements Example: card threeHearts = oneCard; Could also define and initialize threeHearts as follows: card threeHearts; threeHearts.face = Three; threeHearts.suit = Hearts; 2007 Pearson Ed -All rights reserved. Systems Programming: Structures 10 10.4 Accessing Members of Structures Accessing structure members The dot operator (.) {the structure member operator} is used to access a structure member via the structure variable name. card myCard;

printf( "%s", myCard.suit ); The arrow operator (->) {the structure pointer operator} accesses a structure member via a pointer to the structure. card *myCardPtr = &myCard; printf( "%s", myCardPtr->suit ); myCardPtr->suit is equivalent to ( *myCardPtr ).suit 2007 Pearson Ed -All rights reserved. Systems Programming: Structures 11 Structure member and pointer operators 1 /* Fig. 10.2: fig10_02.c 2 Using the structure member and 3 structure pointer operators */ 4 #include 5

6 /* card structure definition */ 7 struct card { Structure definition 8 char *face; /* define pointer face */ 9 char *suit; /* define pointer suit */ 10 }; /* end structure card */ 11 Structure definition must end with semicolon 12 int main( void ) 13 { 14 struct card aCard; /* define one struct card variable */ 15 struct card *cardPtr; /* define a pointer to a struct card */

16 17 /* place strings into aCard */ 18 aCard.face = "Ace"; 19 aCard.suit = "Spades"; Dot operator accesses members of a structure Systems Programming: Structures 2007 Pearson Ed -All rights reserved. 12 Structure member and pointer operators 20 21 cardPtr = &aCard; /* assign address of aCard to cardPtr */ 22 23 printf( "%s%s%s\n%s%s%s\n%s%s%s\n", aCard.face, " of ", aCard.suit, 24 cardPtr->face, " of ", cardPtr->suit, 25 ( *cardPtr ).face, " of ", ( *cardPtr ).suit ); 26

27 return 0; /* indicates successful termination */ 28 29 } /* end main */ Ace of Spades Ace of Spades Ace of Spades Arrow operator accesses members of a structure pointer 2007 Pearson Ed -All rights reserved. Systems Programming: Structures 13 10.5 Using Structures with Functions Passing structures to functions The entire structure can be passed. Individual members of the structure can be passed. For both cases, they are passed by value. To pass a structure by-reference Pass the address of the structure variable. To pass arrays by-value Create a structure with the array as a

member and then pass the structure. 2007 Pearson Ed -All rights reserved. Systems Programming: Structures 14 A Structure Example 1 /* Fig. 10.3: fig10_03.c 2 3 The card shuffling and dealing program using structures */ #include 4 5 #include #include 6 7 /* card structure definition */ 8 struct card { 9 const char *face; /* define pointer face */ 10 const char *suit; /* define pointer suit */ 11 }; /* end structure card */

12 Each card has a face and a suit 13 typedef struct card Card; /* new type name for struct card */ 14 15 /* prototypes */ 16 void fillDeck( Card * const wDeck, const char * wFace[], 17 const char * wSuit[] ); Card is now an alias for struct card 18 void shuffle( Card * const wDeck ); 19 void deal( const Card * const wDeck ); 20 21 int main( void ) 22 { 23 Card deck[ 52 ]; /* define array of Cards */ 24 25 /* initialize array of pointers */ 26 27 const char *face[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",

28 29 2007 Pearson Ed -All rights reserved. "Jack", "Queen", "King"}; Systems Programming: Structures 15 A Structure Example 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

50 51 52 /* initialize array of pointers */ const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"}; srand( time( NULL ) ); /* randomize */ fillDeck( deck, face, suit ); /* load the deck with Cards */ shuffle( deck ); /* put Cards in random order */ deal( deck ); /* deal all 52 Cards */ return 0; /* indicates successful termination */ } /* end main */ /* place strings into Card structures */ void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[] ) Constant pointer to { int i; /* counter */ modifiable array /* loop through wDeck */ for ( i = 0; i <= 51; i++ ) { wDeck[ i ].face = wFace[ i % 13 ]; wDeck[ i ].suit = wSuit[ i / 13 ]; 53 } /* end for */ 54 55 } /* end function fillDeck */ 56 of Cards Fills the deck by giving each

Card a face and suit Systems Programming: Structures 2007 Pearson Ed -All rights reserved. 16 A Structure Example 57 /* shuffle cards */ 58 void shuffle( Card * const wDeck ) 59 { 60 61 62 int i; /* counter */ int j; /* variable to hold random value between 0 - 51 */ Card temp; /* define temporary structure for swapping Cards */ 63 64 65 66 67 /* loop through wDeck randomly swapping Cards */ for ( i = 0; i <= 51; i++ ) { j = rand() % 52; temp = wDeck[ i ];

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 wDeck[ i ] = wDeck[ j ]; wDeck[ j ] = temp; } /* end for */ Each card is swapped with another, random card, shuffling the deck } /* end function shuffle */ /* deal cards */ void deal( const Card * const wDeck ) { int i; /* counter */ /* loop through wDeck */ for ( i = 0; i <= 51; i++ ) {

printf( "%5s of %-8s%c", wDeck[ i ].face, wDeck[ i ].suit, ( i + 1 ) % 2 ? '\t' : '\n' ); } /* end for */ 2007 Pearson Ed -All rights reserved. } /* end function deal */ Systems Programming: Structures 17 A Structure Example Four of Clubs Three of Diamonds Four of Diamonds Nine of Hearts Three of Clubs Eight of Clubs Deuce of Clubs Seven of Clubs Ace of Clubs Ace of Spades Seven of Diamonds Eight of Spades Five of Spades Queen of Spades Queen of Diamonds Jack of Diamonds Eight of Hearts King of Spades Eight of Diamonds Ace of Hearts

Four of Spades Deuce of Hearts Deuce of Spades Seven of Spades King of Clubs Ten of Hearts Three of Hearts Three of Spades Ace of Diamonds Ten of Clubs Four of Hearts Nine of Diamonds Queen of Clubs Jack of Spades Five of Diamonds Five of Clubs Six of Spades Queen of Hearts Deuce of Diamonds Six of Hearts Seven of Hearts Nine of Spades Five of Hearts Six of Clubs Ten of Spades King of Hearts Jack of Hearts Jack of Clubs Ten of Diamonds Nine of Clubs Six of Diamonds King of Diamonds

Systems Programming: Structures 18 Summary Definition of structures in C Syntax details for declaring structs Initializing structs Typedef Structure member (.) and pointer -> operators Passing structures to functions A Structure Example Systems Programming: Structures 19

Recently Viewed Presentations

  • 2 Table of contents Certified Financial Planner Board

    2 Table of contents Certified Financial Planner Board

    Source (bottom chart): Barclays Capital, FactSet, Federal Reserve, Robert Shiller, Stategas/Ibbotson, J.P. Morgan Asset Management. Returns shown are based on calendar year returns from 1950 to 2016. Stocks represent the S&P 500 Shiller Composite and Bonds represent Stategas/Ibbotson for periods...
  • Description of the Region - E3 Alliance

    Description of the Region - E3 Alliance

    St Edward's University Huston-Tillotson University UT Austin Southwestern University 0.22764071395968963 0.73024167210973223 0.82835026608611517 0.8417706040596723 0.90712742980561556 0.92295955376572703 0.98957654723127031 Part-time Austin Community College Concordia University Texas State University St Edward's University Huston-Tillotson ...
  • Neolithic 3rd phase of Stone Ages - Rucker Stewart Middle School

    Neolithic 3rd phase of Stone Ages - Rucker Stewart Middle School

    Neolithic third phase of Stone Ages. They are the farmers! Specifically began 8,000 BC in Middle East. Bell Ringer September 12th:Use Textbook and composition book to define the following: ... Large animals in Neolithic Era were used for jobs on...
  • CSE1301 - Kennesaw State University

    CSE1301 - Kennesaw State University

    All of these algorithms (and the insertion sort) are considered to be to the order of n squared (i.e., O(n2)). O(n2) means that the runtime grows by the square of the size of the array. n == 2 means runtime...
  • Transfer Student Presentation

    Transfer Student Presentation

    CAAP. N=11. 34% or 109 College Portraits have published SLO results as of July 2011. SLO Published on CP by Test. CLA 2010-11 80 ETS PP 2010-11 18 CAAP 2010-11 11. VSA SLO Survey. Short survey to gauge progress, gather...
  • Inclined Plane - cpb-us-e1.wpmucdn.com

    Inclined Plane - cpb-us-e1.wpmucdn.com

    Apply Newton's three laws to everyday situations by explaining the following: 1) Inertia 2) Relationship between force, mass and acceleration 3) Equal and opposite forces . ... Types of pulleys include fixed pulleys, movable pulleys, and block and tackles.
  • Microsoft System Center 2012 Configuration Manager Overview

    Microsoft System Center 2012 Configuration Manager Overview

    System Centre Configuration Manager 2012. Unified Device Management. ... HP-UX, Red Hat Enterprise, Solaris, SUSE Enterprise. Hardware Inventory. Software Inventory. Software Deployment. ... Enable client management agent. Begin monitoring usage and activity. Power Management. Non-Peak & Peak ...
  • Topic #28: Titrations EQ: Why are titrations an

    Topic #28: Titrations EQ: Why are titrations an

    [H+] = [OH-] Buret Valve Titration Acid with Phenolpthalein End-Point Indicators Indicators are chosen, such that they change colors at the range of the pH of interest. The solution itself at the end-point may be: Basic, if the reaction involves...