REFERENCES, POINTERS AND STRUCTS

Similar documents
C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Pointers, Dynamic Data, and Reference Types

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Lecture 2: C Programm

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Homework #3 CS2255 Fall 2012

CS 61c: Great Ideas in Computer Architecture

[0569] p 0318 garbage

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CS 161 Exam II Winter 2018 FORM 1

Pointers, Arrays and Parameters

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

CA31-1K DIS. Pointers. TA: You Lu

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Pointers. Memory. void foo() { }//return


Intermediate Programming, Spring 2017*

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

CS61C : Machine Structures

Heap Arrays and Linked Lists. Steven R. Bagley

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

The output will be: marks all or nothing. 1 #include <stdio.h> 2 main() { 3 int i; int j; 4 int *p; int *q; 6 p = &i; 7 q = &j; 8 i = 1;

Week 7 Part I. Kyle Dewey. Monday, August 6, 12

First of all, it is a variable, just like other variables you studied

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

CMSC 341 Lecture 2 Dynamic Memory and Pointers

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

C++ for Java Programmers

Intro to C: Pointers and Arrays

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

ECE 15B COMPUTER ORGANIZATION

Variables, Memory and Pointers

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

CS31 Discussion 1E Spring 17 : week 08

CS 103 Unit 13 Slides

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

C Pointers. 6th April 2017 Giulio Picierro

BITG 1113: POINTER LECTURE 12

Lecture 04 Introduction to pointers

Introduction to Computer Science Midterm 3 Fall, Points

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

CS61C : Machine Structures

FORM 2 (Please put your name and form # on the scantron!!!!)

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

CSE 333 Midterm Exam 5/10/13

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

a data type is Types

Today s lecture. Continue exploring C-strings. Pointer mechanics. C arrays. Under the hood: sequence of chars w/ terminating null

C Pointers. 7.2 Pointer Variable Definitions and Initialization

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

C'Programming' data'separate'from'methods/functions' Low'memory'overhead'compared'to'Java'

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

In Java we have the keyword null, which is the value of an uninitialized reference type

Heap Arrays. Steven R. Bagley

CS107, Lecture 9 C Generics Function Pointers

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

CS 11 C track: lecture 6

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Pointers. Pointers. Pointers (cont) CS 217

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Introduction to Linked Lists

Ch. 17: Linked Lists. Introduction to Linked Lists

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

04-19 Discussion Notes

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Dynamic arrays / C Strings

Transcription:

REFERENCES, POINTERS AND STRUCTS Problem Solving with Computers-I https://ucsb-cs16-sp17.github.io/

Pointer assignment 2 int *p1, *p2, x; p1 = &x; p2 = p1; Q: Which of the following pointer diagrams best represents the outcome of the above code? A. x B. x C. Neither, the code is incorrect

Modify the function to swap the values of a and b: use pointers void swapvalue(int x, int y){ int tmp = x; x = y; y = tmp; } int main() { int a=30, b=40; swapvalue( a, b); cout<<a<<" "<<b<<endl; } Draw the pointer diagram for your code

4 Segmentation faults (aka segfault) Segfault- your program has crashed! What caused the crash? Read or write to a memory location that either doesn t exist or you don t have permission to access Dereferencing a null pointer Avoid segfaults in your code by Always initializing a pointer to null upon declaration Performing a null check before dereferencing it Avoid redundant null checks by specifying pre and post conditions for functions that use pointers

5 int *p; *p = 5; Q: Which of the following is true about the above code? A B C Compile time error Runtime error Code runs without error

6 References in C++ int main() { int d = 5; int &e = d; } A reference in C++ is an alias for another variable A. d 5 B. d 5 e 5 e C. d e 5 D. This code causes an error

7 References in C++ int main() { int d = 5; int & e = d; int f = 10; e = f; How does the diagram change with this code? } A. d: e: f: 10 10 B. d: e: f: 5 10 d: e: f: C. 10 D. Other or error

8 Pointers and references: Draw the diagram for this code int a = 5; int & b = a; int* pt1 = &a; What are three ways to change the value of a to 42?

Call by reference: Modify to correctly swap a and b void swapvalue(int x, int y){ int tmp = x; x = y; y = tmp; } int main() { } int a=30, b=40; swapvalue( a, b); cout<<a<<" "<<b<<endl;

C++ structures A struct is a data structure composed of simpler data types. struct Point { double x; double y; };

Pointers to structures The C arrow operator (->) dereferences and extracts a structure field with a single operator. struct Point { double x; double y; }; Demo program using points

References to structures Draw a diagram to show the state of memory when the function setpoint is called void setpoint(point &q double x, double y) { //Code to set the x and y values of q } int main(){ Point p; setpoint(p, 100.0, 200); cout <<p.x << <<p.y<<endl }

13 Two important facts about Pointers 1) A pointer can only point to one type (basic or derived ) such as int, char, a struct, another pointer, etc 2) After declaring a pointer: int *ptr; ptr doesn t actually point to anything yet. We can either: make it point to something that already exists, or allocate room in memory for something new that it will point to Null check before dereferencing

14 Complex declarations in C/C++ How do we decipher declarations of this sort? int **arr[]; Read * as pointer to (always on the left of identifier) [] as array of (always to the right of identifier) ( ) as function returning (always to the right ) For more info see: http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html

15 Complex declarations in C/C++ Right-Left Rule int **arr []; Step 1: Find the identifier Illegal combinations include: []() - cannot have an array of functions ()() - cannot have a function that returns a function ()[] - cannot have a function that returns an array Step 2: Look at the symbols to the right of the identifier. Continue right until you run out of symbols *OR* hit a *right* parenthesis ")" Step 3: Look at the symbol to the left of the identifier. If it is not one of the symbols *, (), [] just say it. Otherwise, translate it into English using the table in the previous slide. Keep going left until you run out of symbols *OR* hit a *left* parenthesis "(". Repeat steps 2 and 3 until you've formed your declaration.

16 Complex declarations in C/C++ int i; int *i; int a[10]; int f( ); int **p; int (*p)[]; int (*fp) ( ); int *p[]; int af[]( ); int *f(); int fa()[]; int ff()(); int (**ppa)[]; int (*apa[ ])[ ] ;

17 Pointer assignment: Trace the code int x=10, y=20; int *p1 = &x, *p2 =&y; p2 = p1; int **p3; p3 = &p2;

Next time Arrays and pointers Arrays of structs Dynamic memory allocation