Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and 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.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
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.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Pointers Applications
Pointers CSE 2451 Rong Shi.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
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.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Object-Oriented Programming in C++
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.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
Copyright © – Curt Hill Pointers A Light Introduction.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Week 5 - Monday.  What did we talk about last time?  Processes  Lab 4.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
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.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
Protecting Memory What is there to protect in memory?
Chapter 8 Arrays, Strings and Pointers
CSE 220 – C Programming Pointers.
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Pointers.
Introduction to Pointers
Introduction to Pointers
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Object Oriented Programming COP3330 / CGS5409
Pointers.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Introduction to Pointers
Week 9 - Monday CS222.
Presentation transcript:

Week 6 - Wednesday

 What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings

For a long time it puzzled me how something so expensive, so leading edge, could be so useless. And then it occurred to me that a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match. Bill Bryson For a long time it puzzled me how something so expensive, so leading edge, could be so useless. And then it occurred to me that a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match. Bill Bryson

 A pointer is a variable that holds an address  Often this address is to another variable  Sometimes it's to a piece of memory that is mapped to file I/O or something else  Important operations:  Reference ( & ) gets the address of something  Dereference ( * ) gets the contents of a pointer

 We typically want a pointer that points to a certain kind of thing  To declare a pointer to a particular type  Example of a pointer with type int : type * name; int * pointer;

 Students sometimes get worried about where the asterisk goes  Some (like me) prefer to put it up against the type:  Some like to put it against the variable:  It is possible to have it hanging in the middle:  Remember, whitespace doesn't matter in C char* reference;

 A fundamental operation is to find the address of a variable  This is done with the reference operator ( & )  We usually can't predict what the address of something will be int value = 5; int* pointer; pointer = &value; //pointer has value's address int value = 5; int* pointer; pointer = &value; //pointer has value's address

 The reference operator doesn't let you do much  You can get an address, but so what?  Using the dereference operator, you can read and write the contents of the address int value = 5; int* pointer; pointer = &value; printf("%d", *pointer); //prints 5 *pointer = 900; //value just changed! int value = 5; int* pointer; pointer = &value; printf("%d", *pointer); //prints 5 *pointer = 900; //value just changed!

 Java doesn't have pointers  But it does have references  Which are basically pointers that you can't do arithmetic on  Like Java, pointers allow us to do aliasing  Multiple names for the same thing int wombat = 10; int* pointer1; int* pointer2; pointer1 = &wombat; pointer2 = pointer1; *pointer1 = 7; printf("%d %d %d", wombat, *pointer1, *pointer2); int wombat = 10; int* pointer1; int* pointer2; pointer1 = &wombat; pointer2 = pointer1; *pointer1 = 7; printf("%d %d %d", wombat, *pointer1, *pointer2);

 One of the most powerful (and most dangerous) qualities of pointers in C is that you can take arbitrary offsets in memory  When you add to (or subtract from) a pointers, it jumps the number of bytes in memory of the size of the type it points to int a = 10; int b = 20; int c = 30; int* value = &b; value++; printf("%d", *value); //what does it print? int a = 10; int b = 20; int c = 30; int* value = &b; value++; printf("%d", *value); //what does it print?

 An array is a pointer  It is pre-allocated a fixed amount of memory to point to  You can't make it point at something else  For this reason, you can assign an array directly to a pointer int numbers[] = {3, 5, 7, 11, 13}; int* value; value = numbers; value = &numbers[0]; //exactly equivalent //What about the following? value = &numbers; int numbers[] = {3, 5, 7, 11, 13}; int* value; value = numbers; value = &numbers[0]; //exactly equivalent //What about the following? value = &numbers;

 Well, no, they aren't  But you can use array subscript notation ( [] ) to read and write the contents of offsets from an initial pointer int numbers[] = {3, 5, 7, 11, 13}; int* value = numbers; printf("%d", value[3] ); //prints 11 printf("%d", *(value + 3) ); //prints 11 value[4] = 19; //changes 13 to 19 int numbers[] = {3, 5, 7, 11, 13}; int* value = numbers; printf("%d", value[3] ); //prints 11 printf("%d", *(value + 3) ); //prints 11 value[4] = 19; //changes 13 to 19

 We can use a pointer to scan through a string char s[] = "Hello World!"; //13 chars char* t = s; do { printf("(%p): %c %3d 0x%X\n", t, *t,(int)*t, (int)*t); } while (*t++); //why does this work? char s[] = "Hello World!"; //13 chars char* t = s; do { printf("(%p): %c %3d 0x%X\n", t, *t,(int)*t, (int)*t); } while (*t++); //why does this work?

 That it's an int pointer char s[] = "Hello World!"; //13 chars int* bad = (int*)s; //unwise... do { printf("(%p): %12d 0x%08X\n", bad, *bad, *bad); } while (*bad++); char s[] = "Hello World!"; //13 chars int* bad = (int*)s; //unwise... do { printf("(%p): %12d 0x%08X\n", bad, *bad, *bad); } while (*bad++);

 What if you don't know what you're going to point at?  You can use a void*, which is an address to…something!  You have to cast it to another kind of pointer to use it  You can't do pointer arithmetic on it  It's not useful very often char s[] = "Hello World!"; void* address = s; int* thingy = (int*)address; printf("%d\n", *thingy); char s[] = "Hello World!"; void* address = s; int* thingy = (int*)address; printf("%d\n", *thingy);

 There are some tricks you can do by accessing memory with pointers  You can pass pointers to functions allowing you to change variables from outside the function  Next week we are going to start allocating memory dynamically  Arrays of arbitrary size  Structs (sort of like classes without methods)  We need pointers to point to this allocated memory

 In general, data is passed by value  This means that a variable cannot be changed for the function that calls it  Usually, that's good, since we don't have to worry about functions screwing up our data  It's annoying if we need a function to return more than one thing, though  Passing a pointer is equivalent to passing the original data by reference

 Let's think about a function that can change the values of its arguments void swapIfOutOfOrder(int* a, int* b) { int temp; if( *a > *b ) { temp = *a; *a = *b; *b = temp; } void swapIfOutOfOrder(int* a, int* b) { int temp; if( *a > *b ) { temp = *a; *a = *b; *b = temp; }

 You have to pass the addresses (pointers) of the variables directly  With normal parameters, you can pass a variable or a literal  However, you cannot pass a reference to a literal int x = 5; int y = 3; swapIfOutOfOrder(&x, &y);//will swap x and y int x = 5; int y = 3; swapIfOutOfOrder(&x, &y);//will swap x and y swapIfOutOfOrder(&5, &3); //insanity

 Pointers to pointers  Command line arguments  Lab 6

 Keep reading K&R chapter 5  Start work on Project 3