Pointers, Enum, and Structures

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Learners Support Publications Classes and Objects.
Chapter 9 Structured Data: Structs and ADTs (Data Base Programs with C++) Mr. Dave Clausen La Cañada High School.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
+ 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.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
CPS120: Introduction to Computer Science Lecture 15A Structures.
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.
CPS120: Introduction to Computer Science Data Structures.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
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++
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Constructors and Destructors
Pointers What is the data type of pointer variables?
EGR 2261 Unit 11 Pointers and Dynamic Variables
LESSON 06.
11 Chapter Structured Data
Introduction to Linked Lists
Pointers and Pointer-Based Strings
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Student Book An Introduction
Pointers and References
Pointers and References
Pointer Basics Psst… over there.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures Lesson xx In this module, we’ll introduce you to structures.
Using Arrays in C Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning.
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Derived types.
Built-In (a.k.a. Native) Types in C++
Lecture 18 Arrays and Pointer Arithmetic
Programming in C Pointer Basics.
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Constructors and Destructors
Classes and Objects.
Chapter 1: Introduction to Data Structures(8M)
Structures and Union.
CPS120: Introduction to Computer Science
Pointers and Pointer-Based Strings
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
C Programming Pointers
C++ Introduction - 3.
CPS120: Introduction to Computer Science
Standard Version of Starting Out with C++, 4th Edition
Pointer Basics Psst… over there.
Programming in C Pointer Basics.
C Language B. DHIVYA 17PCA140 II MCA.
Pointers and References
Variables and Constants
Structures Chapter 4.
Structures, Unions, and Enumerations
Presentation transcript:

Pointers, Enum, and Structures Chapter 11 Pointers, Enum, and Structures

Objectives Understand the basics of pointers. Declare pointers. Use the address-of and dereferencing operators. Use enum. Understand what structures are and how to use them.

What is a Pointer? A pointer is a variable or constant that holds a memory address. Think of a pointer as a variable or constant that points to another variable or data structure. Pointers can be used to work with multiple variables or advanced arrays.

Declaring Pointers Working with pointers requires the use of two new operators: the dereferencing operator (*) and the address-of operator (&). Pointers have types just like other variables. The pointer type must match the type of data you intend to point to.

Example int main() { int i; //declare integer int *iptr; //declare pointer iptr = &i; //initialize pointer i = 3; //initialize i return 0; }

Using the * and & Operators The dereferencing operator (*) is used for more than declaring pointers. It can also tell the compiler to return the value in the variable being pointed to, rather than the address of the variable. result = *int_ptr; The variable result is assigned the value of the integer pointed to by int_ptr.

Changing Values with * The dereferencing operator allows you to change the value of the variable the pointer points to. For example, the statement below assigns the value 5 to the integer to which int_ptr points. *int_ptr = 5;

Using enum The enum keyword allows you to create your own simple data types for special purposes in your program. To use enum, simply create a type, give it a name, and tell the compiler what values your new data type will accept. enum sizes {small, medium, large, jumbo};

How enum Works Internally, the compiler assigns an integer to each of the items in an enum list. By default, the compiler begins assigning integers with zero. For example, in the sizes type, small = 0, medium = 1, large = 2, and jumbo = 3.

How enum Works You can choose your own values for use in an enum list: enum quantity {Single=1, Dozen=12, Full_Case=48, Gross=144};

Using typedef Another C++ feature which is related to enum is typedef. You can use typedef to give a new name to an existing data type. For example, you can give the float data type an alias of real with typedef. typedef float real;

Structure Basics C++ structures allow variables to be grouped to form a new data type. The data elements in a structure are arranged in a manner that is similar to the way database programs arrange data. C++ allows you to create a record by grouping the variables and arrays necessary for the fields into a single structure.

Declaring a Structure struct inventory_item { apstring item_ID; apstring description; int quantity_on_hand; int reorder_point; double cost; double retail_price; };

Declaring a Structure Once you have declared a structure, you must declare an identifier, called a structure variable, that is of the structure’s type. The statement below creates a structure variable of a structure named todays_special that is of type inventory_item. inventory_item todays_special;

Accessing Members of a Structure To access a member of the structure, use the name of the variable, the dot operator, then the name of the member you need to access. todays_special.cost=47.80;

Nested Structures A structure can include enumerated data types and even other structures as members. Accessing the nested structure requires that two periods be used. An example based on Code List 11-10 might look like the following: current_donor.bp.diastolic = 74;

Summary Pointers are variables and constants that hold memory addresses. The dereferencing operator (*) is used to declare pointers and to access the value in the variable to which the pointer points. The address-of operator (&) returns the address of a variable, rather than the value in the variable.

Summary The enum keyword allows you to create custom data types. Structures are very useful data structures that allow variables and objects to be grouped to form a new data type. Structures are very similar to classes. The variables within a structure are called members.