Pointers & Arrays.

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

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
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”
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Pointers and Classes.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Standard Version of Starting Out with C++, 4th Edition
Arrays Low level collections.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Pointers & Arrays.
Chapter 9: Pointers.
Part IX Fundamentals of C and C++ Programming Exception Handling
Student Book An Introduction
Programming Languages and Paradigms
Pointers Psst… over there.
Andy Wang Object Oriented Programming in C++ COP 3330
Pointer Data Type and Pointer Variables
Pointers Psst… over there.
Dynamic Memory Allocation
Pointer Basics Psst… over there.
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
Chapter 9: Pointers.
Array & Pointers CSCE 121 J. Michael Moore.
Dynamic Memory Copy Challenge
Pointers & Functions.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Chapter 12 Pointers and Memory Management
Dynamic Memory A whole heap of fun….
How Functions Work Part 2
Dynamic Memory A whole heap of fun….
Pointers Lecture 2 Tue, Jan 24, 2006.
Initializing variables
Arrays ICS2O.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Dynamic Memory A whole heap of fun….
Dynamic Memory.
CS250 Introduction to Computer Science II
CS150 Introduction to Computer Science 1
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Chapter 9: Pointers and String
Programming in C Pointers and Arrays.
Dynamic Memory Copy Challenge
Pointers & Functions.
The Stack.
Pointer Data Type and Pointer Variables
Standard Version of Starting Out with C++, 4th Edition
Pointer Basics Psst… over there.
Pointer Data Type and Pointer Variables
Pointers, Dynamic Data, and Reference Types
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
CSC Data Structures, Fall, 2008
Presentation transcript:

Pointers & Arrays

Pointer Arithmetic Some weirdness:

Pointer Arithmetic Adding 1 to pointer moves it one element: 0x116 Address Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 0x110 0x109 0x108 0x107 x 5 0x106 0x105 0x104 0x103 p 0x102 0x101 0x100

Pointer Arithmetic Adding 1 to pointer moves it one element: 0x116 Address Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 0x110 0x109 0x108 0x107 x 5 0x106 0x105 0x104 0x103 p 0x102 0x101 0x100

Pointer Arithmetic Adding 1 to pointer moves it one element: 0x116 Address Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 x 2.5 0x110 0x109 0x108 0x107 0x106 0x105 0x104 0x103 p 0x102 0x101 0x100

Pointer Arithmetic Adding 1 to pointer moves it one element: Size of element determines size of move Address Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 x 2.5 0x110 0x109 0x108 0x107 0x106 0x105 0x104 0x103 p 0x102 0x101 0x100

Arrays Arrays stored as base address int quiz[3]; quiz : 0x100 0x116 Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 quiz[2] 8 0x110 0x109 0x108 0x107 quiz[1] 5 0x106 0x105 0x104 0x103 quiz[0] 10 0x102 0x101 0x100

Arrays Subscript indicates elements to skip forward cout << quiz[2]; quiz[2] quiz + 2 ints 0x100 + 8 = 0x108 Address Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 quiz[2] 8 0x110 0x109 0x108 0x107 quiz[1] 5 0x106 0x105 0x104 0x103 quiz[0] 10 0x102 0x101 0x100

Arrays almost = Pointers Array : base address of contiguous list of one type of data Pointer : memory address for one type of data

Access Array Can access array as pointer Pointer arithmetic to get other elements int nums[3] = {5,6,7}; cout << *nums << endl; cout << *(nums + 1) << endl; cout << *(nums + 2) << endl;

Access Pointer Can use subscript on pointer Accessing elements after first may not be smart

Arrays almost = Pointers Anywhere you need an array you can use a pointer:

Error Why this message talks about int*

Returning a New array Can't return array from function: But can return pointer to where array starts:

Returning a New array Function to return new array containing even numbers: