CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.

Slides:



Advertisements
Similar presentations
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Advertisements

Informática II Prof. Dr. Gustavo Patiño MJ
CSC Programming for Science Lecture 30: Pointers.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
CSC 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.
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.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
Pointers OVERVIEW.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What the null character is; why.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ Memory Overview 4 major memory segments Key differences from Java
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
CSC 107 – Programming For Science. The Week’s Goal.
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.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
1 Workin’ with Pointas An exercise in destroying your computer.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  Using typedef to make meaningful names.
PHY 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Variables and memory addresses
CSC Programming for Science Lecture 34: Dynamic Pointers.
A Computer Science Tapestry 12.1 Pointers, Memory, Abstractions l A pointer is the a variable/value that is a memory address  Addresses like 1, 2, 3,
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  How we can mix pointers inside structures  Assigning.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
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.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Windows Programming Lecture 03. Pointers and Arrays.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Dynamic Storage Allocation
Motivation and Overview
Dynamic Memory Allocation Reference Variables
C Programming Lecture-8 Pointers and Memory Management
Pointers and dynamic objects
Presentation transcript:

CSC 107 – Programming For Science

Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take advantage of relationship with built-in functions  Understand what NULL is & why we use NULL hardest topic of term  Pointers still hard & this is hardest topic of term

Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take advantage of relationship with built-in functions  Understand what NULL is & why we use NULL hardest topic of term  Pointers still hard & this is hardest topic of term  (Unless you have not learned how to write loops, yet)

Pointers & Variables  Variable gives name for address in memory  Initial values unknown if it is not set  Value at the location used when variable used  Pointer is variable like any other…  … but the pointer’s value is address of a variable  Aliases other variables so both can update location  Pointers can be used like other variables…  …but value is address and not a useful number

& and * Operators variable  & operator gets address of scalar variable  Use only with variables  Use only with variables, including array elements  Lets variable & pointer alias same memory location  Pointer used two ways: arrow & target of arrow  When code has the name alone, using the arrow  With *pointer, value at target can be used or set double x = 4, *y, *z; y = &x; z = y; cout << x << " " << y << " " << *y << endl; cout << x << " " << y << " " << *y << endl;

Arrays vs. Pointers ArraysPointers YamsSweet Potatoes

Arrays vs. Pointers Arrays = YamsPointers = Sweet Potatoes  Makes space at declaration  Variable value is address  Use [] to access entries  Can also access using *  Can be assigned a pointer  Needs target to be useful  Variable value is address  Use * to access target  Can also access using []  Can be assigned array

Arrays vs. Pointers Arrays = YamsPointers = Sweet Potatoes  Makes space at declaration  Variable value is address  Use [] to access entries  Can also access using *  Can be assigned a pointer  Needs target to be useful  Variable value is address  Use * to access target  Can also access using []  Can be assigned array Often use pointers & arrays interchangeably

Arrays + Pointers =  Pointer arithmetic enables mixing & matching  Relies on fact that * and [] are synonyms  Used processing cStrings (& with other arrays, too)  Provides another way to access array elements  Use foo[ nnn ] to access entry in foo at index nnn  But could also access entry using *(foo+ nnn )  Pointers follow same rules and work similarly  But, pointers access offset from where they point

How Pointers Get Used

Using the strstr Function  Finds where 2 nd cString is found in 1 st cString  Pointer to 1 st entry returned by this function  To be found, needs perfect match of entire cString  Looks from left to right until a match is found char myName[]="John J. Jingleheimerschmidt"; char *start = strstr(myName, "J"); char *ptr = strstr(myName, "John"); char *middle = strstr(start + 1, "J"); char *last = strstr(middle + 1, "J"); char *oops = strstr(last + 1, "J");

Slide Is NULL (But Not void)  NULL is special value used by all pointers  Not an actual address – NULL used if when no target  Not a real address, but says DO NOT USE ME  If pointer is NULL, accessing value will crash  May get vague error like "Segmentation Fault"  Most likely, however, the program simply crashes  Use NULL anywhere in code pointer is usable  Needs #include & must be in CAPS

Slide Is NULL (But Not void)  NULL is special value used by all pointers  Not an actual address – NULL used if when no target  Not a real address, but says DO NOT USE ME  If pointer is NULL, accessing value will crash  May get vague error like "Segmentation Fault"  Most likely, however, the program simply crashes  Use NULL anywhere in code pointer is usable  Needs #include & must be in CAPS (Tony apologizes,btw)

Using NULL With Pointers char * ptr = NULL; char *prof = "Matthew Hertz"; char letter = *ptr; float * toiletBulb = ptr; toiletBulb = NULL; int * pIX = null; ptr = strstr("Hi Mom!", "mom"); strstr(NULL, "Monkeys!"); strstr(prof, ptr); *ptr = NULL;

Using NULL With Pointers char * ptr = NULL; char *prof = "Matthew Hertz"; char letter = *ptr; float * toiletBulb = ptr; toiletBulb = NULL; int * pIX = null; ptr = strstr("Hi Mom!", "mom"); strstr(NULL, "Monkeys!"); strstr(prof, ptr); *ptr = NULL;

Using NULL With Pointers char * ptr = NULL; char *prof = "Matthew Hertz"; char letter = *ptr; float * toiletBulb = ptr; toiletBulb = NULL; int * pIX = null; ptr = strstr("Hi Mom!", "mom"); strstr(NULL, "Monkeys!"); strstr(prof, ptr); *ptr = NULL;

Using NULL With Pointers char * ptr = NULL; char *prof = "Matthew Hertz"; char letter = *ptr; float * toiletBulb = ptr; toiletBulb = NULL; int * pIX = null; ptr = strstr("Hi Mom!", "mom"); strstr(NULL, "Monkeys!"); strstr(prof, ptr); *ptr = NULL;

Using NULL With Pointers char * ptr = NULL; char *prof = "Matthew Hertz"; char letter = *ptr; float * toiletBulb = ptr; toiletBulb = NULL; int * pIX = null; ptr = strstr("Hi Mom!", "mom"); strstr(NULL, "Monkeys!"); strstr(prof, ptr); *ptr = NULL;

Using NULL With Pointers char * ptr = NULL; char *prof = "Matthew Hertz"; char letter = *ptr; float * toiletBulb = ptr; toiletBulb = NULL; int * pIX = null; ptr = strstr("Hi Mom!", "mom"); strstr(NULL, "Monkeys!"); strstr(prof, ptr); *ptr = NULL;

Using NULL With Pointers char * ptr = NULL; char *prof = "Matthew Hertz"; char letter = *ptr; float * toiletBulb = ptr; toiletBulb = NULL; int * pIX = null; ptr = strstr("Hi Mom!", "mom"); strstr(NULL, "Monkeys!"); strstr(prof, ptr); *ptr = NULL;

Using NULL With Pointers char * ptr = NULL; char *prof = "Matthew Hertz"; char letter = *ptr; float * toiletBulb = ptr; toiletBulb = NULL; int * pIX = null; ptr = strstr("Hi Mom!", "mom"); strstr(NULL, "Monkeys!"); strstr(prof, ptr); *ptr = NULL;

Using NULL With Pointers char * ptr = NULL; char *prof = "Matthew Hertz"; char letter = *ptr; float * toiletBulb = ptr; toiletBulb = NULL; int * pIX = null; ptr = strstr("Hi Mom!", "mom"); strstr(NULL, "Monkeys!"); strstr(prof, ptr); *ptr = NULL;

Using NULL With Pointers char * ptr = NULL; char *prof = "Matthew Hertz"; char letter = *ptr; float * toiletBulb = ptr; toiletBulb = NULL; int * pIX = null; ptr = strstr("Hi Mom!", "mom"); strstr(NULL, "Monkeys!"); strstr(prof, ptr); *ptr = NULL;

Using NULL With Pointers char * ptr = NULL; char *prof = "Matthew Hertz"; char letter = *ptr; float * toiletBulb = ptr; toiletBulb = NULL; int * pIX = null; ptr = strstr("Hi Mom!", "mom"); strstr(NULL, "Monkeys!"); strstr(prof, ptr); *ptr = NULL;

Using the strchr Function  Finds where char found in 1 st cString  Pointer to 1 st entry returned by this function  To be found, needs exact match of the char  Looks from left to right until a match is found char myName[]="John J. Jingleheimerschmidt"; char *start = strchr(myName, 'J'); char *ptr = strchr(myName, myName[4]); char *startOver = strchr(start, 'J'); char *middle = strchr(start + 1, 'J'); char *nope = strchr(myName, 'j');

Your Turn  Get into your groups and try this assignment

For Next Lecture  Dynamic memory allocation in 12.9 &  More interesting topic in all of computer science?  As program runs, can we grow & shrink arrays?  My thesis was related to which lecture this semester?  Angel also has Weekly Assignment #11 due Tues.  Last programming assignment  Last programming assignment available now