CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
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.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
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 A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
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.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
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 8 Arrays and Strings
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.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
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”
POINTERS.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
CSci 162 Lecture 6 Martin van Bommel. Functions on Structures Rather than pass a copy of structure to a function, or return a copy back as the function.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
13.4 Product of Two Matrices
Standard Version of Starting Out with C++, 4th Edition
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.
Introduction to C++ Programming Language
Dynamic Memory A whole heap of fun….
Multidimensional Arrays
Arrays Arrays A few types Structures of related data items
Pointers & Arrays.
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.
The Stack.
Presentation transcript:

CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04

Overview 2D arrays Pointers

2D Arrays 20 x 20 grid of integers access each element using 2 subscripts – cout << data[1][2]; – cin >> data[2][10]; Don’t make the mistake of – cout << data[1, 2]; int data[20][20];

Initializing 2D Arrays int data[2][3] = { {1, 3, 5}, {2, 4, 6} }; rows 3 columns

Initializing 2D Arrays rows 3 columns int data[2][3] = { {1, 3}, {2} };

Example Find row with largest sum int data[5][6] = {{10, 12, 4, 5, 13, 17}, {19, 21, -3, 1, 2, 18}, {-9, 14, 15, 99, 1, 3}, {1, 2, 33, 4, 5, 6}, {100, -1, -1, 1, -1, 1}};

Passing Arrays to Functions 1D Array – Don’t put anything in [] of function prototype or function header 2D array – Provide size of 2 nd dimension (num columns) in function prototype and function header int foo (int arr[]); int x[5]; foo(x); int bar (int arr[][5]); int y[5][6]; bar(y);

2D array as matrix Write a program to read 2 matrices (of compatible size) out of a file and add them together.

Pointers - Background Each storage location in main memory is identifiable by a memory address. – How the computer handles storage and retrieval Every C++ variable has a memory address Retrieve the address via the & operator (address-of) int x = 3; cout << x << endl; //value stored in x cout << &x << endl; //address of where x is stored

Pointer A pointer is a variable that store the address of a memory location of a particular type Pointer is declared with the *. – Does not distribute to all variables being declared in a single statement… int* x, y, z;//only x is ptr The * operator allows access to the memory location. – Doesn’t always have to be from a pointer. int x = 10; int* ptr; ptr = &x; cout << *ptr << endl; You can declare a pointer to any data type (including void).

Problems with Pointers Uninitialized pointers – pointer contains an invalid memory address due to lack of initialization Dangling pointers – pointer was valid at one point, but has become invalid

Passing Pointers to Functions int myFunction (int*); //indicate a pointer will be passed int main () { int value = 3; int* v; v = &value; myFunction(v); myFunction(&value); //… } If the value pointed to by the function parameter, will value be changed as well? Why or why not?

Ignore Software Engineering Observation 8.1 of text. That’s just dumb. Read Section 8.5 for reasonable and non-dumb solution!

?