190 likes | 286 Views
EEE 243B Applied Computer Programming. Structures (II), scope, extent and static variables. Review. What is the syntax for defining a type; The enumerated type is built on which other type? What is the value of WED ? enum days {SUN, MON, TUE, WED, THU, FRI, SAT};
E N D
EEE 243BApplied Computer Programming Structures (II), scope, extent and static variables
Review • What is the syntax for defining a type; • The enumerated type is built on which other type? • What is the value of WED? enum days {SUN, MON, TUE, WED, THU, FRI, SAT}; • How would I change the above enumerated type to define a new type called DAYS? Maj JGA Beaulieu & Capt MWP LeSauvage
Outline • Structures - initialisation • Structures – accessing the fields • Structures and pointers • Spatial limitation – Scope • Temporal limitation – Extent • Static storage class Maj JGA Beaulieu & Capt MWP LeSauvage
Structures • Yesterday we learned how to declare a structure • For most applications, you should use type-defined structures: typedef struct { char firstName[15]; char lastName[25]; unsigned long collegeNumber; float average; } STUDENT; //name of the type STUDENT aMilColStudent; //a declaration Maj JGA Beaulieu & Capt MWP LeSauvage
Structure - initialisation • Structures are initialised similarly to arrays. If we take the typedefSTUDENT and the variable aMilColStudent from the previous slide: STUDENT aMilColStudent = {"Joe", "Shmoe",45239,78.3}; You will have to comeback to meet this OCdt Maj JGA Beaulieu & Capt MWP LeSauvage
Structures and fields • Structures are constructed with fields. Every where you can use a variable you can use a structure field • Each field can be accessed individually with the structure member operator (.): strcpy(aMilColStudent.firstName, "Bob"); aMilColStudent.collegeNumber = 98876; You may meet this OCdt some day… Maj JGA Beaulieu & Capt MWP LeSauvage
Structures and fields • Structures are entities that can be treated as a whole, but ONLY during an assignment operation: STUDENT fStudent = {"Jane", "Doe", 23498, 33.2}; //poor Jane! aMilColStudent = fStudent; • You cannot compare two structures: if (aMilColStudent == fStudent) printf("Totally an error"); Maj JGA Beaulieu & Capt MWP LeSauvage
Structures and fields • In order to compare structures of the same type, you would need to write a function that compares each field in order: … //function returns 1 if all the field are equal int CompareStudents(STUDENT st1, STUDENT st2) { return (!strcmp (st1.firstName,st2.firstName) && !strcmp (st1.lastName,st2.lastName) && st1.collegeNumber == st2.collegeNumber && (st1.average - st2.average < 0.0001)); } Maj JGA Beaulieu & Capt MWP LeSauvage
Pointing to structures? Point! Maj JGA Beaulieu & Capt MWP LeSauvage
Structures and fields • Like any other type in C, pointers can be used to point to structures. The pointer points to the first byte of the structure. • You can also use pointers to access fields: STUDENT *pStudent = &aMilColStudent; aMilColStudent.collegeNumber = 12345; (*pStudent).collegeNumber = 12345; • Result is the same; but we need the brackets around the dereferencing due to precedence Maj JGA Beaulieu & Capt MWP LeSauvage
Structures and fields • Fortunately C provides another operator that allows us to dereference the pointer and access the field at the same time; the structure selection operator: pStudent->collegeNumber = 54321; Maj JGA Beaulieu & Capt MWP LeSauvage
Spatial limitation – scope • We have talked about global and local variables before • These two terms refer to the scope of a variable or its spatial limitation • The scope of a variable determines its visibility in space • Where you can see and use the variable • The scope of a variable should be kept to the smallest useful size: If a variable is not needed outside a bloc do not make it visible by declaring it outside that bloc Maj JGA Beaulieu & Capt MWP LeSauvage
Spatial limitation – scope • The main function contains a primary bloc. • If a variable is declared above the main function (outside the primary bloc) than it is in scope for the entire compilation unit. It is even available to any compilation unit that know about the variable!!! Do not do this. • Only type-defined structures and enumerated type definitions should be global to the compilation unit. Not variables. Maj JGA Beaulieu & Capt MWP LeSauvage
Temporal limitation - extent • So now we know about scope; the spatial limitation of a variable; its territory. • What about its lifetime? • The temporal limitation of a variable or its extent determines when a variable comes to life, can be used and when it dies. Maj JGA Beaulieu & Capt MWP LeSauvage
Temporal limitation - extent • By declaring a variable as static inside a function, the variable will save the value between executions of the function • The variable will only die at program termination • A static variable can be initialized where it is declared. It will only be initialized once. • If you do not initialize a variable its value is zero. Maj JGA Beaulieu & Capt MWP LeSauvage
Temporal limitation - extent • A static variable is declared like this: … void Fctn (void) { static int anInt = 0; //init only once return anInt++; //This will keep a count //of how many times the //function is called. } Maj JGA Beaulieu & Capt MWP LeSauvage
Temporal limitation - extent • Note that even if the extent of the variable changes by using the key word static, the scope does not change! • The variable can only be used inside the function. Maj JGA Beaulieu & Capt MWP LeSauvage
Scope and extent • static is one of the storage classes • So far all your variables that you used were from the auto class; you never used this as a keyword in front of your variables because it is the default • There two other storage classes (extern and register) but we will not study those in this course Maj JGA Beaulieu & Capt MWP LeSauvage
Quiz Time • If you declare a variable that is a type-defined structure what operator do you use to access its fields? • If you have a pointer that points to a structure, what operator do you use to access each field? • Can you assign a complete structure to another? • How would you compare structures? Maj JGA Beaulieu & Capt MWP LeSauvage