lecture20 Pointers

lecture20 Pointers - 1 Janice Regan, CMPT 128, February....

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Janice Regan, CMPT 128, February. 2007 CMPT 128: Introduction to Computing Science for Engineering Students Pointers Janice Regan, CMPT 128, February 2007 2 What is a variable? A variable is stored in a particular location in memory A variable is given an identifier (name) when it is declared. We refer to the variable using that identifier A variable has a type int myvariable1; //type int float myarray[10]; //type float[10] Janice Regan, CMPT 128, February 2007 3 Types of Constants and Variables A Data Type is A set of values plus a set of operations on those values A crucial concept on modern programming The data type of a variable or constant also determines how the variables value will be represented in memory Variables of several types can have numerical values Variables of other types have values that are characters, or logical values. Janice Regan, CMPT 128, February 2007 4 What is a pointer? Each variable is stored at some memory address, Count successive locations in memory Assume the first memory location counted has address 0, the second address 1, and so on Each location in memory has an address, that address can be represented as an integer A pointer (or reference) is a special type of variable that holds a memory address A pointer containing the address of a variable points to or references that variable Janice Regan, CMPT 128, February 2007 5 Types for pointers Include a set of values legal memory addresses Include a set of operations on those values +, -, --, ++ (meanings of operator somewhat different from simple arithmetic definitions) Include a method to represent the values within the computer: Addresses are represented by integers. IMPORTANT: Addresses are not integers, they have different properties and applications than integers. Janice Regan, CMPT 128, February 2007 6 Data Types, pointers and integers Data type int includes a set of objects, the integers ( -10, -9,-8, ,123, 124, ) Operations that can be done on those objects (+, -, *, /, % ) Data type pointer to an integer includes A set of objects, all legal addresses for integers A set of operations +, - ,++, -- Data type pointer to an double includes A set of objects, all legal addresses for doubles A set of operations +, - ,++, -- Janice Regan, CMPT 128, February 2007 7 Declaring pointer variables Pointer variables can point to only one type of variable int *v1p; // pointer to an integer double *v2p; // pointer to a double char *v3p; // pointer to a char myStruct *v5p; //pointer to a structure of type myStruct Janice Regan, CMPT 128, February 2007 8 Pointers and Style It is useful to easily be able to see which variables in your function are pointers C++ does not enforce any particular structure on your variable and pointer identifiers To distinguish pointers from other variables a number of conventions are used in different coding standards...
View Full Document

Page1 / 40

lecture20 Pointers - 1 Janice Regan, CMPT 128, February....

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online