CS 2130 Lecture 6 Pointers and Arrays. Pointers are like jumps, leading wildly from one part of the data structure to another. Their introduction into.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Cosc 2150 Arrays in assembly code. Variables and addresses Uncompiled ld [a], %r1 addcc %r1, 2, %r3 ARC has three addressing modes —immediate, direct,
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Arrays Chapter 8 page /24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Pointers Applications
Copyright 2001 Oxford Consulting, Ltd1 January Pointers and More Pointers Pointers and Arrays We’ve now looked at  Pointers  Arrays  Strings.
Arrays and Pointers in C Alan L. Cox
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
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.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
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.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
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.
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:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Integer Types short month; // half of a machine word int car; // one machine word unsigned long distance; Character Types char c = ‘\0’; // one byte char.
Pointers *, &, array similarities, functions, sizeof.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Assembly - Arrays תרגול 7 מערכים.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
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.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Pointers and Dynamic Arrays
Chapter 6 – Data Types CSCE 343.
Introduction to Pointers
Introduction to Pointers
Chapter 10: Pointers 1.
Lecture 18 Arrays and Pointer Arithmetic
Multidimensional Arrays
CS111 Computer Programming
Introduction to Pointers
Presentation transcript:

CS 2130 Lecture 6 Pointers and Arrays

Pointers are like jumps, leading wildly from one part of the data structure to another. Their introduction into high- level languages has been a step backwards from which we may never recover. Tony Hoare Famous British computer scientist

Pointers are cool! Jim Greenlee Famous American computer scientist

Recall A variable associates two things –A symbolic name –A value In actual implementation in computing, a variable typically associates a symbolic name, a physical address and a value Pointers are a special kind of variable where the value stored is the physical address of some other variable or data.

Pointers int i; int *ip; Pointer: Variable that contains address of another variable. A pointer is a data object which is separate from what it points to. ip is a pointer to an integer It is not pointing to an integer

The Mysterious * Declaration int *ip; The name of the variable being declared is ip Per K & R meaning is that when ip gets dereferenced i.e. *ip, the item referred to is an int. Use ip is the value of the pointer *ip is the value of what it is pointing at. Here the * is an operator

Pointers NameContents i Code int i;

Pointers NameContents i ip Code int i; int *ip;

Pointers NameContents i42 ip Code int i; int *ip; i = 42;

Pointers NameContents i42 ip Code int i; int *ip; i = 42; *ip = 84; ERROR!!! Core Dump if lucky ERROR!!! Core Dump if lucky

Pointers NameContents i42 ip Code int i; int *ip; i = 42; ip = &i; Address of Operator

Pointers NameContents i84 ip Code int i; int *ip; i = 42; ip = &i; *ip = 84

Pointers NameContents i?????????? ip Code int i; int *ip; i = 42; ip = &i; *ip = &i; NO!!!

Pointers Powerful and dangerous No runtime checking (for efficiency) Bad reputation Java attempts to remove the features of pointers that cause many of the problems hence the decision to call them references –No address of operators –No dereferencing operator (always dereferencing) –No pointer arithmetic

Questions?

Arrays int ia[6]; Allocates consecutive spaces for 6 integers How much space is allocated?

Arrays int ia[6]; Allocates consecutive spaces for 6 integers How much space is allocated? 6 * sizeof(int) Also creates ia which is effectively a constant pointer to the first of the six integers What does ia[4] mean? ia

Arrays int ia[6]; Allocates consecutive spaces for 6 integers How much space is allocated? 6 * sizeof(int) Also creates ia which is effectively a constant pointer to the first of the six integers What does ia[4] mean? Multiply 4 by sizeof(int). Add to ia and dereference yielding: ia ia[4]

Arrays int ia[6]; Note: ia  &ia[0] Never say, "Pointer and arrays are exactly the same thing!!! int *ip; ip = ia;/* Okay */ ia = ip;/* Illegal */

sizeof Compile time operator Two forms sizeof object sizeof ( type name ) Returns the size of the object or the size of objects of type name in bytes Note: Parentheses can be used inthe first form with no adverse effects

sizeof if sizeof(int) == 4 then sizeof(i) == 4 On a typical 32 bit machine... sizeof(*ip)  4 sizeof(ip)  4 char *cp; sizeof(char)  1 sizeof(*cp)  1 sizeof(cp)  4 int ia[6]; sizeof(ia)  24 Not the same thing!!!

Arrays int ia[6]; ia ia[4] means *(ia + 4) which means *(ia + 4 * sizeof(int)) Or *(ia + 4 * sizeof(*ia))

Pointer Arithmetic Note on the previous slide when we added the literal 4 to a pointer it actually gets interpreted to mean 4 * sizeof(thing being pointed at) This is why pointers have associated with them what they are pointing at!

Arrays int ia[6]; ia Array elements are numbered like this since that's how the pointer arithmetic works out!

Arrays int ia[6]; ia ia[4] = 42; 42

Fun with C int ia[6]; ia[4] = 42; is the same as *(ia + 4) = 42; and since addition is commutative *(4 + ia) = 42; would imply that 4[ia] = 42; should work. Does it? Is it a good idea?

Address Operators & & Have a variable and want the address of it. * * Have address (or pointer) and want value of variable that it's pointing at.

Pop Quiz II #define MAX 6 int ia[MAX]; int *ip; ip = ia; /* Okay??? */ ia = ip; /* Okay??? */ ip[2] = 87; /* Okay??? */ *ip refers to the first element of ia ip++; increments ip by how much? *ip now refers to what? ia[1]!!!

More pointer arithmetic int i; int ia[MAX]; for(i = 0; i< MAX; i++) ia[i] = 0; int *ip; int ia[MAX]; for(ip = ia; ip < ia + MAX; ip++) *ip = 0; Sometimes pointer arithmetic is faster than array manipulation

Order of Operations *ip++ = 0; Equivalent to *ip = 0; ip++;

Order of Operations *(++ip) = 0; Equivalent to ++ip; *ip = 0;

Pointers to Pointers? NameContents i Code int i;

Pointers to Pointers? NameContents i ip Code int i; int *ip;

Pointers to Pointers? NameContents i ip ipp Code int i; int *ip; int **ipp;

Pointers to Pointers? NameContents i42 ip ipp Code int i; int *ip; int **ipp; i = 42;

Pointers to Pointers? NameContents i42 ip ipp Code int i; int *ip; int **ipp; i = 42; ip = &i;

Pointers to Pointers? NameContents i84 ip ipp Code int i; int *ip; int **ipp; i = 42; ip = &i; *ip = 84;

Pointers to Pointers? NameContents i84 ip ipp Code int i; int *ip; int **ipp; i = 42; ip = &i; *ip = 84; ipp = &ip;

Pointers to Pointers? NameContents i22 ip ipp Code int i; int *ip; int **ipp; i = 42; ip = &i; *ip = 84; ipp = &ip; **ipp = 22;

Arrays of Pointers char *month_name(int n) { static char *name[] = { "Illegal month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; return (n 12)? name[0]: name[n]; }

Hungarian Notation Invented by Charles Simonyi "Some people think it's the best thing since structured programming; others hate Hungarian with a passion." Incredibly simplified version p * –Include in the name of every pointer as many p 's as there are * 's –Example int *pint; char **ppchr; *p –Then when dereferencing, each * cancels a p *pint refers to an int *ppchr refers to a pointer to a char

Questions

Recall int ia[6]; ia[2] = 42; Address calculation: 2 * sizeof(*ia) + ia Access is by dereferencing *(2 * sizeof(*ia) + ia) 42

What happens? int ia[6]; ia[8] = 84; Address calculation: 8 * sizeof(*ia) + ia 4284

How does a two dimensional array work? How would you store it?

,00,10,20,31,01,11,21,32,02,12,22,3 Column 0 Column 1 Column 2 Column 3 Column Major Order 0,00,10,20,31,01,11,21,32,02,12,22,3 Row 0 Row 2 Row 1 Row Major Order

Advantage Using Row Major Order allows visualization as an array of arrays ia[1] ia[1][2] 0,00,10,20,31,01,11,32,02,12,22,3 1,2 0,00,10,20,31,01,11,32,02,12,22,31,2

Recall One Dimensional Array int ia[6]; Address of beginning of array: ia  &ia[0] Two Dimensional Array int ia[3][6]; Address of beginning of array: ia  &ia[0][0] also Address of row 0: ia[0]  &ia[0][0] Address of row 1: ia[1]  &ia[1][0] Address of row 2: ia[2]  &ia[2][0]

Declaration int ia[3][4]; Type Address Number of Rows Number of Columns Declaration at compile time i.e. size must be known Declaration at compile time i.e. size must be known

Element Access Given a row and a column index How to calculate location? To skip over required number of rows: row_index * sizeof(row) row_index * Number_of_columns * sizeof(arr_type) This plus address of array gives address of first element of desired row Add column_index * sizeof(arr_type) to get actual desired element 0,00,10,20,31,01,11,21,32,02,12,22,3

Element Access Element_Address = Array_Address + Row_Index * Num_Columns * Sizeof(Arr_Type) + Column_Index * Sizeof(Arr_Type) Element_Address = Array_Address + (Row_Index * Num_Columns + Column_Index) * Sizeof(Arr_Type)

What if array is stored in Column Major Order? Element_Address = Array_Address + (Column_Index * Num_Rows + Row_Index) * Sizeof(Arr_Type) 0,00,10,20,31,01,11,21,32,02,12,22,3

Don't confuse Arrays int ia[10]; Arrays of pointers char *names[13]; Pointers to pointers Node **head; Multidimensional arrays int ia[3][4][5]; As formal parameter int iarr[] As formal parameter char *nms[] Pointers to pointers Node **curr; Multidimensional arrays int iar[][4][5]

Multidimensional Example void tester(int arr[][4][5]) { } int main() { int ia[3][4][5]; int ib[8][4][5]; int *ic; ic = ia; tester(ia); tester(ib); tester(ic); return 0; }

Questions?