introduction to pointers lesson xx

18
Introduction to Pointers Lesson xx

Upload: gusty

Post on 22-Feb-2016

28 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Pointers Lesson xx. Objectives. Memory setup Pointer declaration Address operator Indirection Printing addresses or pointers. Memory Setup. 10. a. (1ab). int a = 10; float x = 17.24; char c = ‘q’;. 17.24. (2fe). x. ‘q’. c. (3cd). Address . 10. a. (1ab). - PowerPoint PPT Presentation

TRANSCRIPT

Slide 1

Introduction to PointersLesson xx

In this presentation, we introduce you to the basics of pointers.

1ObjectivesMemory setupPointer declarationAddress operatorIndirectionPrinting addresses or pointers

Our goal is to introduce you to these basic elements of pointers: memory setup, pointer declaration, address operator, indirection and printing addresses.2Memory Setupint a = 10;float x = 17.24;char c = q;1017.24qaxc(1ab)(2fe)(3cd)

Think of the memory of the computer as a series of boxes in which you can store 1 number or character. The statement: int a = 10; sets up a memory location called a and stores the #10 in it. char c = q sets up another memory location named c and stores the character q in it. Associated with every location is an address. In our picture, a is at address 1aB, x is at address 2fe and c is located at 3cd.3Address int a = 10;float x = 17.24;char c = q;1017.24q5fbaxc(1ab)(2fe)(3cd)

So far, we have put a number or a character in each memory location. In C++, you can also put an address in a memory location. In our diagram, we have the address 5fb stored in one of the boxes. Lets see how we get an address into a memory location.4Pointer Declarationint a = 10;float x = 17.24;char c = q;

int *ptr;1017.24qaxc(1ab)(2fe)(3cd)ptr

When you want to set up an integer memory location in C++, you write the statement: int a; This statement is called a variable declaration. When you want to set up a location that holds an address, you write the statement: int * ptr; This is called a pointer declaration. You are telling the computer that ptr is a variable that will contain a pointer or an address. Think of pointer and address as the same thing in C++.5Address Operator &int a = 10;float x = 17.24;char c = q;

cout