! The address operator (&) returns the address of a. ! Pointer: a variable that stores the address of another

Similar documents
Week 4. Pointers and Addresses. Dereferencing and initializing. Pointers as Function Parameters. Pointers & Structs. Gaddis: Chapters 9, 11

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or

Overloading Operators

Functions, Arrays & Structs

Functions, Arrays & Structs

Ch 11. Structured Data (11.2 to 11.8) Data Types Structures. Data Types (C/C++) Data Type: CS 2308 Fall example: Integer.

Structures. Data Types Structures. Data Types (C/C++) Gaddis: A Data Type consists of: Unit 7. example: Integer. CS 1428 Spring 2018

BITG 1113: POINTER LECTURE 12

Data Types (C/C++) Structures

CS201- Introduction to Programming Current Quizzes

Pointers. Variable Declaration. Chapter 10

Pointers, Dynamic Data, and Reference Types

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

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

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

Pointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Homework #3 CS2255 Fall 2012

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

What is an algorithm?

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

Dynamic Allocation of Memory


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

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Structured Data. CIS 15 : Spring 2007

Midterm Review. PIC 10B Spring 2018

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

! 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)

! 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)

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

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Pointers and Dynamic Memory Allocation

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

MEMORY ADDRESS _ REPRESENTATION OF BYTES AND ITS ADDRESSES

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

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

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

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

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

Pointer Data Type and Pointer Variables

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

CSC 211 Intermediate Programming. Arrays & Pointers

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.

CSC1322 Object-Oriented Programming Concepts

Pointers! Arizona State University 1

MYcsvtu Notes LECTURE 34. POINTERS

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

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

! 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)

Quiz 0 Review Session. October 13th, 2014

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

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

Decimal Representation

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

REFERENCES, POINTERS AND STRUCTS

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

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

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

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

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

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

CS 115 Midterm 2 Solutions

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Kapi ap l S e S hgal P T C p t u er. r S. c S ienc n e A n A k n leshw h ar ar Guj u arat C C h - 8

Introducing C++ to Java Programmers

1 Short Answer (5 Points Each)

C++ for Java Programmers

Variables, Memory and Pointers

Exam 3 Chapters 7 & 9

CS 11 C track: lecture 5

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

CS2255 HOMEWORK #1 Fall 2012

Object Reference and Memory Allocation. Questions:

C++ Programming Chapter 7 Pointers

Lectures 6/7 Pointers and Dynamic Arrays

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation

LECTURE 11 STRUCTURED DATA

PROGRAMMAZIONE I A.A. 2017/2018

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Pointers and References

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

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CSC 215 Structures. Dr. Achraf El Allali

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

CS 11 C track: lecture 6

Constants, References

Ch. 17: Linked Lists. Introduction to Linked Lists

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

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

Transcription:

Week 4 Pointers & Structs Gaddis: Chapters 9, 11 CS 5301 Spring 2015 Jill Seaman 1 Pointers and Addresses! The address operator (&) returns the address of a variable. int x; cout << &x << endl;! Pointer: a variable that stores the address of another variable, providing indirect access to it.! An asterisk is used to define a pointer variable int *ptr; // 0xbffffb0c! ptr is a pointer to an int. It can contain addresses of int variables. ptr = &x; 2 Dereferencing and initializing Pointers as Function Parameters! The unary operator * is the dereferencing operator.! *ptr is an alias for the variable that ptr points to. int x = 10; int *ptr; //declaration, NOT dereferencing ptr = &x; //ptr gets the address of x *ptr = 7; //the thing ptr pts to gets 7! Initialization: int x = 10; int *ptr = &x; //declaration, NOT dereferencing! ptr is a pointer to an int, and it is initialized to the 3 address of x.! Use pointers to implement pass by reference. //prototype: void changeval(int *); void changeval (int *val) { *val = *val * 11; int main() { int x; cout << "Enter an int " << endl; cin >> x; changeval(&x); cout << x << endl;! How is it different from using reference parameters? 4

Pointers and Arrays! You can treat an array variable as if it were a pointer to its first element. int numbers[] = {10, 20, 30, 40, 50; cout << first: << numbers[0] << endl; cout << first: << *numbers << endl; cout << &(numbers[0]) << endl; cout << numbers << endl; Output: first: 10 first: 10 0xbffffb00 0xbffffb00 5 Pointer Arithmetic! When you add a value n to a pointer, you are actually adding n times the size of the data type being referenced by the pointer. int numbers[] = {10, 20, 30, 40, 50; // sizeof(int) is 4. // Let us assume numbers is stored at 0xbffffb00 // Then numbers+1 is really 0xbffffb00 + 1*4, or 0xbffffb04 // And numbers+2 is really 0xbffffb00 + 2*4, or 0xbffffb08 // And numbers+3 is really 0xbffffb00 + 3*4, or 0xbffffb0c cout << second: << numbers[1] << endl; cout << second: << *(numbers+1) << endl; cout << "size: " << sizeof(int) << endl; cout << numbers << endl; cout << numbers+1 << endl; Output: second: 20 second: 20 size: 4 0xbffffb00 0xbffffb04! Note: array[index] is equivalent to *(array + index) 6 Pointers and Arrays! pointer operations * + can be used with array variables.! subscript operations: [ ] can be used with pointers. int list[10]; cin >> *(list+3); int list[] = {1,2,3; int *ptr = list; cout << ptr[2]; 7 Comparing Pointers! pointers (addresses) maybe compared using the relational operators: < <= > >= ==!=! Examples: int arr[25];! What is the difference? ptr1 < ptr2 *ptr1 < *ptr2 cout << (&arr[1] > &arr[0]) << endl; cout << (arr == &arr[0]) << endl; cout << (arr <= &arr[20]) << endl; cout << (arr > arr+5) << endl; 8

Dynamic Memory Allocation The new operator! When a function is called, memory for local variables is automatically allocated.! When a function exits, memory for local variables automatically disappears.! Must know ahead of time the maximum number of variables you may need.! Dynamic Memory allocation allows your program to create variables on demand, during run-time. 9! new operator requests dynamically allocated memory for a certain data type: int *iptr; iptr = new int;! new operator returns address of newly created anonymous variable.! use dereferencing operator to access it: *iptr = 11; cin >> *iptr; int value = *iptr / 3; 10 Dynamically allocated arrays! dynamically allocate arrays with new: int *iptr; //for dynamically allocated array int size; cout << Enter number of ints: ; cin >> size; iptr = new int[size]; for (int i=1; i<size; i++) { iptr[i] = i;! Program will throw an exception and terminate if not enough memory available to allocate 11 delete!! When you are finished using a variable created with new, use the delete operator to destroy it: int *ptr; double *array; ptr = new int; array = new double[25];... delete ptr; delete [] array; // note [] required for dynamic arrays!! Do not delete pointers whose values were NOT dynamically allocated using new!! Do not forget to delete dynamically allocated variables (Memory Leaks!!). 12

Returning Pointers from Functions! functions may return pointers: int * findzero (int arr[]) { int *ptr; ptr = arr; while (*ptr!= 0) ptr++; return ptr;! The returned pointer must point to NOTE: the return type of this function is (int *) or pointer to an int. Returning Pointers from Functions: duplicatearray int *duplicatearray (int *arr, int size) { int *newarray; if (size <= 0) return NULL; //size must be positive //NULL is 0, an invalid address newarray = new int [size]; //allocate new array for (int index = 0; index < size; index++) newarray[index] = arr[index]; //copy to new array return newarray; dynamically allocated memory OR an item passed in via an argument NOTE: if the function returns dynamically allocated memory, int a [5] = {11, 22, 33, 44, 55; int *b = duplicatearray(a, 5); for (int i=0; i<5; i++) if (a[i] == b[i]) cout << i << ok << endl; delete [] b; //caller deletes mem then it is the responsibility of the calling function to delete it. 13 14 Output 0 ok 1 ok 2 ok 3 ok 4 ok Structures! A structure stores a collection of objects of various types! Each element in the structure is a member, and is accessed using the dot member operator. struct Student { int idnumber; string name; int age; string major; ; Defines a new data type Student student1, student2; Defines new variables student1.name = John Smith ; 15 Student student3 = {123456, Ann Page,22, Math ; Structures: operations! Valid operations over entire structs: assignment: student1 = student2; function call: myfunc(gradstudent,x);! Invalid operations over structs: comparison: student1 == student2 output: cout << student1; input: cin >> student2; void myfunc(student, int); //prototype Must do these member by member 16

Arrays of Structures Arrays of Structures: initialization! You can store values of structure types in arrays. Student roster[40]; //holds 40 Student structs! Each student is accessible via the subscript notation. roster[0] = student1; //copy student1 into 1st position! Members of structure accessible via dot notation cout << roster[0].name << endl;! To initialize an array of structs: struct Student { int idnumber; string name; int age; string major; ; int main() { Student roster[] = { {123456,"Ann Page",22,"Math", {111222,"Jack Spade",18,"Physics" ; 17 18 Arrays of Structures Passing structures to functions! Arrays of structures processed in loops: Student roster[40]; //input for (int i=0; i<40; i++) { cout << "Enter the name, age, idnumber and " << "major of the next student: \n"; cin >> roster[i].name >> roster[i].age >> roster[i].idnumber >> roster[i].major; //output all the id numbers and names for (int i=0; i<40; i++) { cout << roster[i].idnumber << endl; cout << roster[i].name << endl;! Structure variables may be passed as arguments to functions: void getstudent(student &s) { // pass by reference cout << "Enter the name, age, idnumber and " << "major of the student: \n"; cin >> s.name >> s.age >> s.idnumber >> s.major; void showstudent(student x) { cout << x.idnumber << endl; cout << x.name << endl; cout << x.age << endl; cout << x.major << endl; // in main: Student student1; getstudent(student1); showstudent(student1); 19 20

Pointers to structures! We can define pointers to structures Student s1 = {12345, Jane Doe, 18, Math ; Student *ptr = &s1;! To access the members via the pointer: cout << *ptr.name << end;! dot operator has higher precedence, so use (): cout << (*ptr).name << end;! or equivalently, use ->: cout << ptr->name << end; // ERROR: *(ptr.name) 21 Dynamically Allocating Structures! Structures can be dynamically allocated with new: Student *sptr; sptr = new Student; sptr->name = Jane Doe ; sptr->idnum = 12345;... delete sptr;! Arrays of structures can also be dynamically allocated: Student *sptr; sptr = new Student[100]; sptr[0].name = John Deer ;... delete [] sptr; No arrows (->) necessary. It s just an array of Student 22 Sample Problem 1 Sample Problem 2! Array Expander: Write a function expander that accepts an int array and the array s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a pointer to the new array.! Write a driver that calls the expander function, and outputs the resulting array. Be sure to deallocate any dynamically allocated memory. 23! Soccer Scores: Write a program that stores the following data about a soccer player in a structure: Player s Name Player s Number Points Scored by Player The program should keep an array of 12 of these structures. Each element is for a different player on a team. When the program runs it should ask the user to enter the data for each player. The program should calculate and display the total points earned by the team. The number and name of the player who has earned the most points should also be displayed. 24