Pointers COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.

Slides:



Advertisements
Similar presentations
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Advertisements

An introduction to pointers in c
Branches Two branch instructions:
CompSci A Peek at the Lower Levels  It is good to have a sense of what happens at the hardware level  Not required for this course  It may.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
CS 300 – Lecture 20 Intro to Computer Architecture / Assembly Language Caches.
CS 300 – Lecture 19 Intro to Computer Architecture / Assembly Language C Coding & The Simulator Caches.
Concept of Basic Time Complexity Problem size (Input size) Time complexity analysis.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
The audio will be turned on just before our start time at 7:00 pm ET.
Databases with PHP A quick introduction. Y’all know SQL and Databases  You put data in  You get data out  You can do processing on it very easily 
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
C Course Lecture 4 This lecture we'll talk about: Multi-dimensional arrays. Pointer arithmetic. Pointers to structures. Multi-file programming. What is.
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.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
CS 102 Computers In Context (Multimedia)‏ 01 / 23 / 2009 Instructor: Michael Eckmann.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
How to start Milestone 1 CSSE 371 Project Info There are only 8 easy steps…
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
How Computers Work … and how you can work them. Art 315 Lecture 03 Dr. J Parker Fall 2010.
CS 101: Introduction to computer programming and utilization Abhiram Ranade.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Multiple Files and beyond COP 3275 – PROGRAMMING USING C.
Pointers OVERVIEW.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
1 CSE1301 Computer Programming: Revision. 2 Topics Type of questions What do you need to know? About the exam Exam technique Staff consultation Revision.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
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:
GAM 200 Club. How to Game Engine GAM 200 Club Zachary Nawar.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
1 CS161 Introduction to Computer Science Topic #9.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
COP 3275 – Iteration and loops Instructor: Diego Rivera-Gutierrez.
Careers - CV Writing Skills To start or improve your CV and through: what you have learned about the skills that employers look for. what you identified.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
CMSC 2021 CMSC 202 Computer Science II for Majors Spring 2002 Sections Ms. Susan Mitchell.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Variables and memory addresses
CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1.
COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez.
COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.
Representation of Data - Instructions Start of the lesson: Open this PowerPoint from the A451 page – Representation of Data/ Instructions How confident.
1 1.Log in to the computer in front of you –Temp account: 210class / 2.Update your in Cascadia's system –If I need to you I'll use.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.
User-Written Functions
Arrays as pointers and other stuff
Presentation transcript:

Pointers COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Administrative stuff Reminder: No class on Friday. Celebrate the US Independence. Dress in Stripes and stars.... Yell “ ‘merica” at everyone and have fun, but be safe. Also no Quiz, celebrate that too. Due date for Homework #4 is now Monday. Test cases will be posted tonight. Homework #3 grades are out. If you got a 20 with a message about compilation GO TALK TO THE TA ASAP. Questions?

Pointers We started by talking about indirection Remember? I asked you to pray for me (that’s called intercessory prayer, and it’s cool) We were talking about defining pointers. We talked about the & operator We talked about the * operator But…. Why the heck would someone want pointers????

Let’s talk about applications! Let’s say we are coding a management system for a course. We have a struct that saves all the info for a student (name, UFID, DOB, major, year, etc). struct student{ char *name; int UFID; char major[3]; struct Date DOB; int year; }; We need to store all the students in the class With what we know right now. How would you store the students? Arrays?

Array of students AndrewMichaelCristinaAnaLaura [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; …

Array of students AndrewMichaelCristinaAnaJuliana [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … What if Cristina decides to drop the class? How do we handle that in code?

Array of students AndrewMichaelCristinaAnaJuliana [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … What if Cristina decides to drop the class? How do we handle that in code?

Array of students AndrewMichael[Empty]AnaJuliana [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … Option 1: we just add an empty space… clear the struct for Cristina, and we are done. Drawbacks: 1.all our functions on the list, would have to account for possible empty spaces before the end of the array. 2.We always have to visit the whole array instead of just the active students

Array of students AndrewMichael[Empty]AnaJuliana [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … Option 2: We can move the last student (Michael) to Cristina’s space…

Array of students Andrew[Empty]MichaelAnaJuliana [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … Option 2: We can move the last student (Michael) to Cristina’s space…. Drawbacks: We lose alphabetical order. We would need to sort

Array of students AndrewMichael[Empty]AnaJuliana [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … Option 3: we move everyone just one slot

Array of students Andrew[Empty]JulianaAnaMichael [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … Option 3: we move everyone just one slot. Drawback: Sort of expensive… What if it had been Ana? We have to move EVERY SINGLE student…

Pointers allows us to model lists… A list is a data structure. We haven’t discussed the concept, because it’s not the focus of this class. Data structures are ways to organize data in efficient ways (computationally speaking).

Lists We will definitely implement a List next week. But essentially they look like this: AnaAndrewCristinaJulianaMichael What if Cristina decides to drop the class? How do we handle that in code?

Lists We will definitely implement a List next week. But essentially they look like this: AnaAndrewCristinaJulianaMichael What if Cristina decides to drop the class? How do we handle that in code?

Lists We will definitely implement a List next week. But essentially they look like this: AnaAndrewCristinaJulianaMichael What if Cristina decides to drop the class? How do we handle that in code?

Lists We will definitely implement a List next week. But essentially they look like this: AnaAndrewCristinaJulianaMichael What if Cristina decides to drop the class? How do we handle that in code?

Lists We will definitely implement a List next week. But essentially they look like this: AnaAndrewCristinaJulianaMichael What if Cristina decides to drop the class? How do we handle that in code?

Lists We will definitely implement a List next week. But essentially they look like this: AnaAndrewJulianaMichael What if Cristina decides to drop the class? How do we handle that in code?

Lists So lists are cool… AnaAndrewCristinaJulianaMichael

Lists So lists are cool… while for the visualization seeing it so linearly made so much sense… the truth is that in memory…. The list is all over the place (usually) AnaAndrewCristinaJulianaMichael

Lists So lists are cool… while for the visualization seeing it so linearly made so much sense… the truth is that in memory…. The list is all over the place (usually)

Lists So lists are cool… while for the visualization seeing it so linearly made so much sense… the truth is that in memory…. The list is all over the place (usually) Ana Andrew Cristina Juliana Michael

Other uses of pointers? Files! We haven’t worked with those yet. Wonder why? We usually upload a file to memory, and they are all encoded differently. Different programs have different formats that are not interchangeable. Doc, docx,pdf,txt,ppt,pptx,xls,c, …

Other uses of pointers? Calling functions without knowing their name…. (ok you don’t need to understand this, but it IS beautiful) A pointer can point to anything in memory.

Other uses of pointers? Calling functions without knowing their name…. (ok you don’t need to understand this, but it IS beautiful) A pointer can point to anything in memory. That includes: code! Now let’s say we have multiple methods that do similar things, but they have the same parameters… Examples? Sort methods Math operations Sets of problems that we can only approximate, and there are multiple approximation methods.

Other uses of pointers? Calling functions without knowing their name…. (ok you don’t need to understand this, but it IS beautiful) A pointer can point to anything in memory. That includes: code! if(input == 'A' ) { A(); }else if(input == 'B‘) { B(); }else if(input == 'C') { C(); } A B C All of them are functions (pieces of code) and they do similar functionality

Pointers to functions Calling functions without knowing their name…. (ok you don’t need to understand this, but it IS beautiful) A pointer can point to anything in memory. That includes: code! if(input == 'A' ) { A( ); }else if(input == 'B‘) { B( ); }else if(input == 'C') { C( ); } A B C All of them are functions (pieces of code) and they do similar functionality

Pointers to functions A pointer can point to anything in memory. That includes: code! if(input == 'A' ) { A( ); }else if(input == 'B‘) { B( ); }else if(input == 'C') { C( ); } Let’s say someone really smart, comes up with method D, and wants to make it available on your program to compare against A,B,C. A B C D

Pointers to functions A pointer can point to anything in memory. That includes: code! if(input == 'A' ) { A( ); }else if(input == 'B‘) { B( ); }else if(input == 'C') { C( ); }else if(input == 'D'){ D( ); } A B C D That’s a bit annoying. Having to change the code… What if the person That wrote D doesn’t have the original code?

Pointers to functions A pointer can point to anything in memory. That includes: code! Let’s say we have a “magic” pointer ptr. We could have this code: if(ptr != NULL) { ptr( ); } A B C D ptr I’ll talk about the specific type of ptr next week. For now just believe me: it is possible to this with pointers

Pointers to functions A pointer can point to anything in memory. That includes: code! Let’s say we have a “magic” pointer ptr. We could have this code: if(ptr != NULL) { ptr( ); } A B C D ptr I’ll talk about the specific type of ptr next week. For now just believe me: it is possible to this with pointers

Pointers to functions A pointer can point to anything in memory. That includes: code! Let’s say we have a “magic” pointer ptr. We could have this code: if(ptr != NULL) { ptr( ); } A B C D ptr I’ll talk about the specific type of ptr next week. For now just believe me: it is possible to this with pointers

Pointers to functions A pointer can point to anything in memory. That includes: code! Let’s say we have a “magic” pointer ptr. We could have this code: if(ptr != NULL) { ptr( ); } A B C D ptr I’ll talk about the specific type of ptr next week. For now just believe me: it is possible to this with pointers E Z … This sort of idea is VERY common in optimization software. Matlab uses it quite a bit.

Let’s go back to the simple stuff…. Memory./a.out When we execute (e.g. %./a.out) instructions Program Counter (PC)

Let’s go back to the simple stuff…. Memory./a.out As execution continues, the PC points to the next instruction to execute. instructions Program Counter (PC)

Let’s go back to the simple stuff…. Memory./a.out When we reach variable definitions like this one: int i = 0; instructions Program Counter (PC) 0 i int i = 0;

Ok… now pointers? Memory./a.out When we reach pointer definitions like this one: int *prt_i = 0; instructions Program Counter (PC) 0 i int *prt_i = 0 ptr_i

Ok… now pointers? Memory./a.out When we reach pointer definitions like this one: int *prt_i = NULL; instructions Program Counter (PC) 0 i int *prt_i = NULL ptr_i

Ok… now pointers? Memory./a.out When we use & and apply it to i &i (in any context….) instructions Program Counter (PC) 0 i int *prt_i = NULL ptr_i &i

Ok… now pointers? Memory./a.out So, if I do… ptr_i = &i; instructions Program Counter (PC) 0 i prt_i = &i; ptr_i

Ok… now pointers? Memory./a.out Now… what exactly is the arrow???? instructions Program Counter (PC) 0 i prt_i = &i; ptr_i

Ok… now pointers? Memory./a.out Now… what exactly is the arrow???? The arrow is the value of the pointer. printf("%p\n", prt_i); instructions Program Counter (PC) 0 i prt_i = &i; ptr_i

Ok… now pointers? Memory./a.out If I take * and apply it to ptr_i *ptr_i (in any context) instructions Program Counter (PC) 0 i prt_i = &i; ptr_i