CS/COE 0449 (term 2174) Jarrett Billingsley

Slides:



Advertisements
Similar presentations
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.
Advertisements

Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
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:
1 Workin’ with Pointas An exercise in destroying your computer.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Homework due Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
Lecture 5 Pointers 1. Variable, memory location, address, value
Programs – Preprocessing, Compilation and Linking
CSE 374 Programming Concepts & Tools
CS/COE 0449 (term 2174) Jarrett Billingsley
Intro to Pointers in C CSSE 332 Operating Systems
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
CS/COE 0447 (term 2181) Jarrett Billingsley
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
CSCI206 - Computer Organization & Programming
CSE 374 Programming Concepts & Tools
A bit of C programming Lecture 3 Uli Raich.
Motivation and Overview
Memory, Data, & Addressing II CSE 351 Autumn 2017
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
Foundations of Programming: Arrays
COSC 220 Computer Science II
Minimization and Sequential Logic
Programs – Dynamic Linking and Loading
Pointers.
CS/COE 0449 (term 2174) Jarrett Billingsley
CS/COE 0447 (term 2184) Jarrett Billingsley
Introduction to Pointers
Introduction to Pointers
void Pointers Lesson xx
Pseudo-ops, Debugging, etc.
Computer Science 210 Computer Organization
CSC 253 Lecture 8.
C – Structs, Unions, Enums, Typedefs
C – Scope, Lifetime, and the Stack
CS/COE 0449 Jarrett Billingsley
CSC 253 Lecture 8.
CS 0449 Jarrett Billingsley
Bases and Representations, Memory, Pointers, Arrays, For-Loops
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
Programming in C Pointer Basics.
Arrays, Control flow, and Loops
Memory, Data, & Addressing II CSE 351 Winter 2018
Memory, Data, & Addressing II CSE 351 Summer 2018
Beginning C for Engineers
Programming in C Pointer Basics.
MSIS 655 Advanced Business Applications Programming
Pointers Lecture 2 Tue, Jan 24, 2006.
Memory, Data, & Addressing II CSE 351 Autumn 2018
FSMs, Multiplication, and Division
Strings and Pointer Arrays
Memory, Data, & Addressing II CSE 351 Winter 2018
C++ Pointers and Strings
Memory, Data, & Addressing II CSE 351 Winter 2019
Chapter 9: Pointers and String
Pointers and dynamic objects
Programming in C Pointer Basics.
Introduction to Pointers
C++ Pointers and Strings
CS/COE 0449 Jarrett Billingsley
Week 7 - Monday CS 121.
CS/COE 0449 Jarrett Billingsley
Presentation transcript:

CS/COE 0449 (term 2174) Jarrett Billingsley C - Pointers CS/COE 0449 (term 2174) Jarrett Billingsley

Class announcements Project 1! Due at 11:59 PM on Sunday, February 5th Quiz 1 will be next Tuesday!! Yeah, that's right! Midterm 1 is in two weeks!!! Boy, you've got a lot of stuff to get ready for, huh? I really recommend you do the practice exercises in the book... The labs this week and next week will also give you more practice with C. 1/19/2017 CS/COE 0449 term 2174

Question time! 1/19/2017 CS/COE 0449 term 2174

Pointers 1/19/2017 CS/COE 0449 term 2174

Memory! Memory's a big array of bytes. Every byte has an address. The only way your computer can access any data in memory (all the variables etc) is by knowing their addresses. Say we had a variable int x and its address was DC04. How many bytes would it span? In a little-endian machine, what would its value be? (you're learning about endianness in 447, right?) 0xBEEFC0DE! Address Value ... DC0B 44 DC0A 4 DC09 2 DC08 88 DC07 BE DC06 EF DC05 C0 DC04 DE DC03 2A DC02 27 DC01 0E DC00 42 DC0D 8B DC0E BB 1/19/2017 CS/COE 0449 term 2174

You've kinda used pointers in Java... Remember writing linked lists? class Link { Link next; int value; } Link list = new Link(); list.next = new Link(); list.next.next = new Link(); Object references are really just pointers. So if you start getting confused, just think about how those work. next: value: 0 next: value: 0 next: value: 0 1/19/2017 CS/COE 0449 term 2174

int x = 0x100; int y = 0x200; int* px = &x; int* py = &y; Variables vs. Pointers A variable is a named piece of memory. A pointer is a variable that holds a memory address. int x = 0x100; int y = 0x200; int* px = &x; int* py = &y; Name Address Value py DC0C DC04 px DC08 DC00 y 0200 x 0100 1/19/2017 CS/COE 0449 term 2174

Declaring pointers In Java, you can make an array of any type by sticking brackets after it. int[], int[][], int[][][] etc. In C, you can make a pointer to any type by sticking stars after it. int*, int**, int*** etc. When you make a pointer, the type before the star is the type of value that it points to. So an int* can point to an int variable. 1/19/2017 CS/COE 0449 term 2174

The address-of operator When used as a prefix operator, & means "address of." It gives you the memory address of any variable, array item, etc. The address is given to you as a pointer type. Use it on an int? You get an int*. Let's try printing out addresses using the %p format specifier with printf! 1/19/2017 CS/COE 0449 term 2174

The address-of operator Coming back to this example... int x = 0x100; int y = 0x200; int* px = &x; int* py = &y; What value does &x give us? What type is &x? What value does &px give us? What type is &px? OHOHOHOHO Name Address Value py DC0C DC04 px DC08 DC00 y 0200 x 0100 1/19/2017 CS/COE 0449 term 2174

The dereference operator An address on its own isn't that useful... What's useful is being able to read and write the data at that address. When used as a prefix operator, * is the dereference operator. When you use it on a pointer, it's like you're directly using the variable that it points to. 1/19/2017 CS/COE 0449 term 2174

The dereference operator Okay: now let's see what happens when we dereference these pointers. int x = 0x100; int y = 0x200; int* px = &x; int* py = &y; *px = 0x300; printf("%x\n", x); printf("%x\n", *px); Name Address Value py DC0C DC04 px DC08 DC00 y 0200 x 0100 0300 1/19/2017 CS/COE 0449 term 2174

Watch out.. int x = 0x100; int* px = &x; px = 0x300; What does this program do? int x = 0x100; int* px = &x; px = 0x300; printf("%x\n", x); printf("%x\n", *px); Name Address Value px DC04 DC00 x 0100 0300 1/19/2017 CS/COE 0449 term 2174

Everyone on the same page? 1/19/2017 CS/COE 0449 term 2174

So what? 1/19/2017 CS/COE 0449 term 2174

Every problem in CS... ...can be solved with another level of indirection (pointers). Pointers are the basis of: Dynamic memory management Strings Arrays Object-oriented programming Pretty much everything your operating system does Everything... everything does. Understanding pointers is understanding how software works. 1/19/2017 CS/COE 0449 term 2174

Functions Functions are machines which grind up socks and gloves and turn them into numbers. ?????????????? Very important diagram from the book here. 1/19/2017 CS/COE 0449 term 2174

Passing by reference In Java, you're stuck returning at most one value unless you resort to ugly hacks like returning an Object[]. In C, you can use pointers to modify values given as parameters. Let's write a function that takes two pointers – one for hours and one for minutes – and increments the minutes by 1. If the minutes exceed 59, then they should reset to 0 and tick the hours up by 1. 1/19/2017 CS/COE 0449 term 2174

Arrays! 1/19/2017 CS/COE 0449 term 2174

No more beating around the bush I've been saying in C, arrays are Not A Thing. That's because... As far as C concerned, an array is just a pointer. Wat. char buffer[40]; printf("%p\n", buffer); // woah. 1/19/2017 CS/COE 0449 term 2174

What does the [] operator do? When you write array[i], what's happening? Well, let's use the address-of operator to help us see... 1/19/2017 CS/COE 0449 term 2174

Pointer arithmetic When you use the array indexing operator, you're really just adding an offset to the pointer, and using that as the address to access. In other words, a[i] is interchangeable with *(a + i). Wait, but— If a is a pointer, what does a + i mean? Let's print it out. 1/19/2017 CS/COE 0449 term 2174

Pointer arithmetic array[3] DC0C 0003 array[2] DC08 0002 array[1] DC04 If we write this: int array[] = {0, 1, 2, 3}; Memory looks like this: If we want to access array[2]... What is that equivalent to? *(array + 2) But look at the addresses. How big is each item in the array? When we write array + 2, it doesn't add 2 to the address. We don't get 0xDC02. It adds the size of 2 items to the address. We get 0xDC08. Name Address Value array[3] DC0C 0003 array[2] DC08 0002 array[1] DC04 0001 array[0] DC00 0000 1/19/2017 CS/COE 0449 term 2174

Arrays-of-arrays? Before we go, I will leave you with this: int main(int argc, char** argv) 1/19/2017 CS/COE 0449 term 2174