CPS120: Introduction to Computer Science Lecture 15A Structures.

Slides:



Advertisements
Similar presentations
E LEMENTARY D ATA STRUCTURES. A RRAYS Sequence of elements of similar type. Elements are selected by integer subscripts. For example, signs: array[37:239]
Advertisements

Data Structures Using C++ 2E
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Structure of a C program
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CPS120: Introduction to Computer Science
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Data Types Declarations Expressions Data storage C++ Basics.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CPS120: Introduction to Computer Science Data Structures.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Chapter Structured Data 11. Combining Data into Structures 11.2.
Java Programming Language Lecture27- An Introduction.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Pointers What is the data type of pointer variables?
Pointers, Enum, and Structures
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
CPS120: Introduction to Computer Science
Chapter 2: Introduction to C++.
Data Structures and Algorithms Introduction to Pointers
C Programming Pointers
CPS120: Introduction to Computer Science
Standard Version of Starting Out with C++, 4th Edition
Variables and Constants
Presentation transcript:

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 iptr … i

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

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

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

Relationship between Pointers and Arrays state_code … M I \

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.

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".

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

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

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; };

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

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

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

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; };