# 05_pointers - CSCI-1200 Data Structures Fall 2010 Lecture 5...

This preview shows pages 1–3. Sign up to view the full content.

CSCI-1200 Data Structures — Fall 2010 Lecture 5 — Pointers, Arrays, Pointer Arithmetic Announcements: Test 1 Information Test 1 will be held Tuesday, September 21st, 2010 from 2-3:50pm in West Hall Auditorium . No make-ups will be given except for emergency situations or illness, and even then a written excuse from the Dean of Students or the Student Experience oﬃce or the RPI Health Center will be required. Coverage: Lectures 1-6, Labs 1-3, and Homeworks 1-2. Closed-book and closed-notes handwritten or printed . Computers, cell-phones, palm pilots, calculators, PDAs, music players, etc. are not permitted and must be turned oﬀ and placed under your desk. All students must bring their Rensselaer photo ID card. Practice problems from previous exams are available on the course website. Solutions to the problems will be posted later. The best way to prepare is to completely work through and write out your solution to each problem, before looking at the answers. Review from Last Week C++ class syntax, designing classes, classes vs. structs; Passing comparison functions to sort ; Non-member operators. Today’s Lecture — Pointers and Arrays Optional Reading: Ford & Topp pp. 219-228; Koenig and Moo, Section 10.1 Pointers store memory addresses. They can be used to access the values stored at their stored memory address. They can be incremented, decremented, added and subtracted. Dynamic memory is accessed through pointers. Pointers are also the primitive mechanism underlying vector iterators, which we have used with std::sort and will use more extensively throughout the semester. 5.1 Pointer Example Consider the following code segment: float x = 15.5; float *p; /* equiv: float* p; */ p = &x; *p = 72; if ( x > 20 ) cout << "Bigger\n"; else cout << "Smaller\n"; The output is Bigger because x == 72.0 . What’s going on? Computer memory Before *p=72 p x After *p=72 p x 72.0 15.5 5.2 Pointer Variables and Memory Access x is an ordinary integer, but p is a pointer that can hold the memory address of an integer variable. The diﬀerence is explained in the picture above. Every variable is attached to a location in memory. This is where the value of that variable is stored. Hence, we draw a picture with the variable name next to a box that represents the memory location.

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

View Full Document
Each memory location also has an address, which is itself just an index into the giant array that is the computer memory. The value stored in a pointer variable is an address in memory. The statement
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/21/2012 for the course CSCI 1200 taught by Professor Cutler during the Fall '08 term at Rensselaer Polytechnic Institute.

### Page1 / 5

05_pointers - CSCI-1200 Data Structures Fall 2010 Lecture 5...

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

View Full Document
Ask a homework question - tutors are online