1 / 15

CPS120: Introduction to Computer Science

CPS120: Introduction to Computer Science. Lecture 15A Structures. Pointer Use in C++. A pointer is a variable or constant that holds a memory address a) Hexadecimal numbers are used for representing memory locations. 216793 216794 216801 iptr … 216801 3 i 216802 . Using Pointers.

Download Presentation

CPS120: Introduction to Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPS120: Introduction to Computer Science Lecture 15A Structures

  2. Pointer Use in C++. • A pointer is a variable or constant that holds a memory address • a) Hexadecimal numbers are used for representing memory locations 216793 216794 216801 iptr … 216801 3 i 216802

  3. Using Pointers • Pointers must be initialized • e.g. iptr =&I; • This reads iptr is assigned the address of i

  4. Intializing Pointers • Declare pointers before use, as with other variables. • Each variable being declared as a pointer must be preceded by an asterisk (*). • Initialize pointers before use to a 0, NULL or an address to prevent unexpected results

  5. Pointer Operators • & is the address operator which returns the address of its operand • * is the indirection operator or dereferencing operator and returns the value to which the operand (pointer) points. • sizeof - used to determine the size of an array during program compiliation

  6. Relationship between Pointers and Arrays 139996 139997 140003 state_code … 140003 M 140004 I 140005 \0 140006 140007

  7. C++ and Strings • C++ numbers the individual characters in a string beginning with index position 0. So after the declaration statement, • string stateName = "Michigan"; • is made, the 'M' is considered to be in the index position 0, the first 'i' in the index position 1, the 'c' in the index position 2, and so on. Note that index positions may contain a blank space, which is, of course, a valid character. A blank space has an ASCII value of 32.

  8. Changing Values in an Array • You may use subscript notation to change the value of one specific character within a string. The assignment statement, • stateName[1] = 'I'; • would change the lowercase e originally found in index position 1 of the example above to an uppercase I. So the string is now "MIchigan".

  9. Using enum • enum allows you to create your own simple data types for special purposes • Create a type • Give it a name • Specify values that are acceptable enum sizes {small, medium, large, jumbo}; • The compiler assigns an integer to each enum item

  10. typedef • typedef gives a new name to an existing data type typedef float real; • Confusing to the reader, should be used sparingly

  11. Structures • Structures group variables together in order to make one's programming task more efficient. • Any combination of variables can be combined into one structure. • This is a useful and efficient way to store data. struct Student { string socSecNum; string lastName; string firstName; int pointsEarned; double gpa; };

  12. Using Structures • Each of the different variables are called members of the structure • Structures allow us to keep related data referring to individual members together • Strings, integer, and floating-point variables may be grouped together into one structure. • In effect, we have created our own customized data type. • The semicolon after the closing curly brace is required

  13. Using the new data structure • The structure definition should be placed above the main function of a program but below the compiler directives • Declare an actual variable of this programmer-created data type within a function (such as main) in order to make use of this structureDone with a declaration statement like Student freshmen; • This reates a variable called freshmen of the data type Student

  14. Assigning values to the structure • To assign a grade point average (GPA) of 3.4 to the gpa member of the variable freshmen, use the statement: freshmen.gpa = 3.4; • The period (.) that is used between the variable name freshmen and the member gpa is called the dot operator. • The dot operator simply us to reference individual members of a structure

  15. Nested Structures • You can use a previously defined structure as a member of another structure • Address is nested inside of the Customer structure. • Since Address is used within Customer, the structure definition for Address must be placed above Customer in order to avoid compile errors struct Address{ string street; string city; string state; int zip;}; struct Customer{ string name; string phone; Address homeAddress; Address businessAddress; };

More Related