CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
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.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Pointers Applications
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
Pointers CSE 2451 Rong Shi.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
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++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
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.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
POINTERS.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
Pointers *, &, array similarities, functions, sizeof.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
CE-2810 Dr. Mark L. Hornick 1 Mixing C and assembly Safety goggles on!
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Variables and memory addresses
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
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.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Lecture 5 Pointers 1. Variable, memory location, address, value
Intro to Pointers in C CSSE 332 Operating Systems
UNIT 5 C Pointers.
Pointers.
Pointers.
Pointers and Pointer-Based Strings
Student Book An Introduction
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Lecture 6 C++ Programming
Homework Starting K&R Chapter 5 Good tutorial on pointers
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Class Circle { Float xc, yc, radius;
Presentation transcript:

CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!

CS-1030 Dr. Mark L. Hornick 2 Pointers are… …used to store the address of an object An address is legitimate data by itself in C Every memory location has an address …a data type Ex: int is a datatype “pointer to int” is also a (separate) datatype Since they hold memory addresses, all pointers are the same size in bytes (unlike the data they point to)

CS-1030 Dr. Mark L. Hornick 3 Pointer declaration confusion Declaration of a pointer uses the * prefix int *pValue; // pointer to an integer int* pValue; // another way of declaring the same Be careful with multiple declarations on one line: int *pValue1, *pValue2; int* pValue1, pValue2; // No!!! int* pValue1; // OK int* pValue2; // OK

CS-1030 Dr. Mark L. Hornick 4 Pointers – unary operators New unary operators: & prefix – “address of” (can use on pointers or regular variables) int aValue = 3; int *pValue = &aValue; // sets pValue to the addr of aValue * prefix - dereference (can only use on pointers) int someOtherValue; someOtherValue = *pValue; // sets someOtherValue = aValue

CS-1030 Dr. Mark L. Hornick 5 Pointers – unary operators New unary operators: ++ prefix or postfix – increment the address stored in the pointer int aValue = 3; int *pValue = &aValue; // addr might be 0x1234 pValue++; // new addr is 0x1236; why not 0x1235???? // what does (*pValue)++ do??? -- prefix or postfix – decrement the address stored in the pointer

CS-1030 Dr. Mark L. Hornick 6 Confusing syntax explained int aValue = 1; // an int int anotherValue = 2; int* pValue = &aValue; // ptr to aValue // above line is same as: // int* pValue; // ptr, but not init’d // pValue = &aValue; // now init’d pValue = &anotherValue // points to anotherValue // what does pValue = anotherValue mean??? // whatever pValue points to now equals aValue: *pValue = aValue;// same as anotherValue=aValue; A common C convention: pointer variable names are prefixed with “p”

CS-1030 Dr. Mark L. Hornick 7 More Confusing syntax int aValue = 1; // an int int anotherValue = 2; int* p1 = &aValue; // ptr to aValue int* p2 = &anotherValue; *p1 = *p2; // same as aValue=anotherValue p1 = p2; // now both point to anotherValue

CS-1030 Dr. Mark L. Hornick 8 The NULL pointer Pointers should only be made to point at valid addresses The exception is the NULL pointer, where int* pValue = NULL; // points to address 0 // or int* pValue = 0; This is a convention that allows you to check to see if a pointer is valid

CS-1030 Dr. Mark L. Hornick 9 Pointing a pointer at a valid address Since a pointer holds an address, the pointer can be made to point anywhere in (data) memory uint8_t* pValue; // 16-bit addr of 8-bit value pValue = 0x0038; // what is at 0x38??? Now explain what this does: *pValue = 0xFF;

CS-1030 Dr. Mark L. Hornick 10 Pointers and arrays Say you define an array of characters: char s[]=“ABCD”; The string variable is actually a pointer to the beginning of the array, so we can write: char* ps = s; // or ps = &s[0]; Dereferencing the pointer gives the value of the character at that address: char c = *ps; // same as c = s[0]; Incrementing the pointer advances the pointer address to the next character in the array: ps++; // same as ps = &s[1];

Pointer Arithmetic Incrementing a character pointer advances the pointer address by one byte, to the next character in the array: ps++; // value of ps increases by 1; Say you define an array of longs: long larray[]= {1L, 2L, 3L, 4L}; long* pl = larray; Incrementing a long pointer advances the pointer address by four bytes, to the next long in the array: pl++; // value of pl increases by 4; CS-1030 Dr. Mark L. Hornick 11 Pointer arithmetic is dependent on the size of the element the pointer is declared to reference.

CE-2810 Dr. Mark L. Hornick 12 Pointers as function arguments Consider a function: int16_t add( uint8_t* px, uint16_t* py); uint8_t* means “pointer to unsigned 8-bit int” uint16_t* means “pointer to unsigned 16-bit int” Since Atmega32 uses 2-byte addressing, all pointers are 2 byte variables Since Atmega32 uses 2-byte addressing, all pointers are 2 byte variables GNU GCC passes arguments left to right using registers r25 to r8 Above, value of px is placed in r24:r25 (low, high bytes) Px’s value is the address of some 8-bit uint8_t value somewhere in memory Value of py (2 bytes) is placed in r22:r23 Calling such a function: uint8_t x=3; uint16_t y=4; uint8_t* px = &x; uint16_t* py = &y; int16_t sum = add(&x, &y); sum = add(px, py); px(high) px(low) x=3...

CE-2810 Dr. Mark L. Hornick 13 Within the function, the arguments must be dereferenced to manipulate the values Implementing such a function: uint16_t add(uint8_t* px,uint16_t* py ) { uint8_t tempx = *px; uint16_t tempy = *py; uint16_t sum = tempx + tempy; //or uint16_t sum = *px + *py; return sum; }

CS-1030 Dr. Mark L. Hornick 14 The const specifier Prevents objects passed by address from being changed within a function Here’s the modified declaration of the method that passes by address: void printValue(const int* pValue); Usage: printName( &value );

CS-1030 Dr. Mark L. Hornick 15 Notation for const and pointers is tricky int x; const int* ptr1 = &x; // x is constant // Can’t use ptr1 to change x, but can change ptr1 to point to another object int* const ptr2 = &x; // ptr2 constant // Can’t change ptr2 to another object, but can change value of x const int* const ptr2 = &x; // Can’t do either