150 likes | 286 Views
Types. The type of a variable represents a set of values that may be assigned to the variable. For example, an integer variable is one that may take the values –2 31 to 2 31 – 1 What if we want a variable that may take values from a non-numerical set?
E N D
Types • The type of a variable represents a set of values that may be assigned to the variable. • For example, an integer variable is one that may take the values –231 to 231 – 1 • What if we want a variable that may take values from a non-numerical set? • For example, variable day may take values from the set {Mon, Tue, Wed, Thu, Fri, Sat, Sun}
Enumerations • An enumerated type is a set of values defined by the programmer. • Each element of an enumerated type is given a unique name by the programmer. • Example: enum{Mon, Tue, Wed, Thu, Fri, Sat, Sun} • Each element of an enumerated type is internally represented by an integer. • Unless the programmer explicitly specifies the integers, the values are consecutive starting at 0. • This allows us to perform iterate from one variable to another.
Enumerations • In order to be able to declare enumerated variables, an enumeration must be given a name. • Example: enum dayT {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; int main () { dayT today, tomorrow; today = Mon; cout << today; // this will print 0 tomorrow = (today + 1) % 7; if (tomorrow == Sat) cout << “It’s the weekend!” << endl; ...
Enumerations • enum dayT {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; • Mon, Tue, etc. are called “enumeration constants” • CAUTION! When you declare dayT today; , today is essentially an integer. There are no restrictions on its value. • Example: today = 135; is allowed (but meaningless) 0 1 2 3 4 5 6
Structures • We have seen that we can use arrays when we want several similar variables of the same type. • But what if we need to store several pieces of information that have different types but refer to the same entity? • Example: a student roster may contain the name and id number of each student. We’d like one variable to somehow contain two components, the name and the id. • The answer is a structure: a collection of named components (fields) that may have different types. • (Sneak preview: C++ classes are very similar to C structures)
Structures • Example: struct studentInfoT { char name[30]; int id_number; }; int main () { studentInfoT student1; // a variable of type studentInfoT studentInfoT roster[15]; // an array of 15 elements of type // studentInfoT.
Structure • To access the individual fields of a structure we use the dot operator. • Example 1: struct pointT { double xcoord; double ycoord; }; int main () { pointT origin; origin.xcoord = 0.0; origin.ycoord = 0.0; ... }
Structure • To access the individual fields of a structure we use the dot operator. • Example 2: struct studentInfoT { int id_number; char grade; }; int main () { studentInfoT classlist[15]; classlist[0].id = 1234567; classlist[0].grade = ‘A’; ... }
Pointers • A pointer variable is a variable whose value is an address. • Some programmers use the terms pointer and address interchangeably. • Pointers allow us to manipulate data through the address • This makes pointers very useful but also very dangerous.
Pointers • Example: address variable name, value px is a pointer variable that contains the address of an integer variable, x. The value of px is address 0xbfffc294 The value of x is integer -10 0xbfffc290: px ,0xbfffc294 0xbfffc294: x , -10 0xbfffc298: We say that px points to x Because x is an int, we say that the base type of px is int
Pointers • A pointer variable is declared as follows: base_type*variable_name ; the name of the pointer variable. It follows the same rules as regular variables, and usually starts with a p the type of the object pointed to by the pointer the asterisk signifies that this is a declaration of a pointer variable
Pointers • Examples • All of these variables are currently unitilialized int *pnum; // pnum will contain the address of an // integer float *ptemp; // ptemp will contain the address of a // floating point number char *pword, letter; // CAUTION! pword is a pointer // to char but letter is a char.
Pointers • A pointer variable is initialized by assigning an address to it. • Example 1: int list[5] = {10, 20, 30, 40, 50}; int *pnum; pnum = list; /* pnum now contains the address where the list begins. In other words, it contains the address of the first element of the list. */
Pointers • A pointer variable is initialized by assigning an address to it. • We can access the address of a variable by using the & operator. int *pnum, number; pnum = &number; /* pnum now points to number */
Pointers char *pletter, *pgrade; char grades[4] = {'A', 'B', 'C', 'D'}; char ch; pgrade = grades;/* pgrade points to the beginning of grades */ pletter = &ch; /* now, pletter points to variable ch */ pletter = pgrade; /* now, pletter points to the beginning of grades */ grades = pletter; /*ILLEGAL! The address of an array is constant! You cannot change its value */ pletter = 100; /* ILLEGAL! 100 is an integer, pletter's value should be an address */ pletter = &pgrade /* ILLEGAL! pletter should be the address of a char, but &pgrade is the address of a pointer (or, to be more specific, it's the address of the address of a char). The types do not match. */