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.

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

CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried.
Programming and Data Structure
Kernighan/Ritchie: Kelley/Pohl:
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointers1 Pointers & Dynamic Arrays Allocating memory at run-time.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Copyright 2001 Oxford Consulting, Ltd1 January Pointers and More Pointers Pointers and Arrays We’ve now looked at  Pointers  Arrays  Strings.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
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.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
(continue) © by Pearson Education, Inc. All Rights Reserved.
CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!
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 and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
C++ crash course Class 7 more operators, expressions, statements.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
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:
Slide 1 Chapter 10 Pointers and Dynamic Arrays. Slide 2 Learning Objectives  Pointers  Pointer variables  Memory management  Dynamic Arrays  Creating.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Data Types Declarations Expressions Data storage C++ Basics.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Variables and memory addresses
Concordia TAV 2002 Comp5421_411 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (1) Tianxiang Shen Summer 2002 Department of Computer.
Engineering Computing I Chapter 5 Pointers and Arrays.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Variables and Types. Primitive Built-in Type Type Meaning Minimum Size bool boolean NA char character 8 bits wchar_t wide character 16 bits char16_t Unicode.
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.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
The C++ Data Types Fundamental Data Types
Pointers.
Student Book An Introduction
Lecture 6 C++ Programming
Pointers and References
Dynamic Memory Allocation
Lecture 18 Arrays and Pointer Arithmetic
Pointers Pointers point to memory locations
C++ Array 1.
Pointers and References
Presentation transcript:

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 EndOfLine = ‘\n’; Real Types double salary; // two machine words float wage; // one machine word Basic Data Types of C

int *ip1, *ip2; double *dp; int ival = 1027; // pi initialized to address no object int *pi = 0; // pi2 initialized to address ival int *pi2 = &ival; // pi and pi2 now both address ival pi = pi2; // pi2 now address no object pi2 = 0; Pointer Types – Initialization

The void* pointer can hold the address value of any pointer type. int i = 0; int *pi = &i; double d = -9.01; double *pd = &d; void *pv = pi; // pv holds the address value of pi pv = pd; // pv now holds the address value of pd pi; // type int*; evaluates to the address contained within pi π // type int** ; evaluates to the actual address of pi int **ppi = &pi ; // a pointer to a pointer to int. pi ppi 2 Pointers – More

Pointer Arithmetic double *p; loc 0loc k pp+k sizeof (double) Data type Current address New address char int double p = 5000 p + 1 = p – 6 = *2 = – 6*4 = 4976

double x[50], y[200]; double z[10][20]; // 2-dimensional array x[0]x[49] addr addr + 49 * 4 const int ArraySize = 10; long A[ArraySize]; int A[20]; int v = 20; A[v] = 0; // index v exceeds 19; core dumped. Array Types

Increment & Decrement Operators int ia[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int i = 0, j = 9; while ( i < 10) ia [i++] = ia[j--]; // ia[10] = {9, 8, 7, 6, 5, 5, 6, 7, 8, 9} i++ increments i after its current value is used as index into ia. int ia[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int i = -1, j = 10; while ( i < 9) // 9 instead of 10 because ia[10] is illegal. ia [++i] = ia[--j]; // ia[10] = {9, 8, 7, 6, 5, 5, 6, 7, 8, 9} ++i increments i before its use as an index into ia.

Shorthand Binary Operators i += 1;i = i + 1; i -= ++j;j = j + 1; i = i – j; i *= j++;i = i * j; j = j + 1; left operand

Multidimensional Arrays int ia[4][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11} }; int main() { const int rowSize = 4; const int colSize = 3; int ia[ rowSize ] [ colSize ]; for ( int i = 0; i < rowSize; ++i) for ( int j = 0; j < colSize; ++j) ia [i][j] = i + j; }

Returns the size of an object or type name in bytes. sizeof (type name); sizeof (object); sizeof object ; #include int ia[] = { 0, 1, 2 }; size_t array_size = size_of ia; size_t element_num = array_size / sizeof ( int ); The value returned is of a machine-specific type size_t. The sizeof Operator

Arrays vs Pointers The array identifier evaluates to the address of its first element. int ia[ ] = { 0, 1, 2, 3, 5, 8, 13, 21 }; // The type of ia is int * // two equivalent forms ia; &ia[0]; // both yield the value of the first element *ia; ia[0]; // both acccess the address of // the second element &ia[1]; ia+1; // both access the value of // the second element *(ia+1); ia[1]; // the dereference operator has a // higher precedence than the // addition operator *ia + 1; // the sum of the first // element and 1 *(ia+1); // the second element Its type is that of pointer to the element’s type.

Traversal of an Array through Pointer Manipulation #include int main() { int ia[9] = { 0, 1, 1, 2, 3, 5, 8, 13, 21 }; int *pbegin = ia; int *pend = ia + 9; while (pbegin != pend ) { cout << *pbegin << ‘ ’; ++pbegin; } // iterate across an array without // knowing its actual size #include void ia_print (int *pbegin, int *pend) { while ( pbegin != pend) { cout << *pbegin << ‘ ’; ++pbegin; } int main() { int ia[9] = { 0, 1, 1, 2, 3, 5, 8, 13, 21 }; ia_print ( ia, ia+9 ); }

Array Parameters An array is always passed as a pointer to its first element. // three equivalent declarations of putValues( ) void putValues ( int* ); void putValues ( int[ ] ); // a better declaration if the argument // calling the function with is an array void putValues ( int [10] ); // ok, but the array’s size is irrelevant The changes to an array parameter within the called function are made to the array argument itself (call-by-reference). // A function does not intend to change the array elements void putValues (const int [ 10 ]);

Multidimensional Arrays as Parameters Specify the size of all its dimensions beyond that of its first. void putValues (int matrix [ ][10], int rowSize ); If the function is to accept a matrix with variable sizes in all its dimensions, it needs to use pointer to pointer types. void putValues (int matrix[ ][ ], int rowSize, int colSize); // wrong void putValues (int **matrix, int rowSize, int colSize); // ok now

Cont’d void putValues (int **matrix, int rowSize, int colSize); int ia[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } }; // illegal: the type of ia does not match int** putValues ( ia, 2, 3); int** pa; pa = new int* [ 2]; // two-step dynamic memory allocation for (int i = 0; i < 2; i++) pa[i] = new int [3]; pa[0][0] = 0; pa[0][1] = 1; pa[0][2] = 2; pa[1][0] = 3; pa[1][1] = 4; pa[1][2] = 5; putValues ( pa, 2, 3 ); // ok now

Dynamic memory allocation: memory is allocated at run-time. int *pi = new int; // pi → int *pi = new int (1024); // allocates and initializes an object int *pia = new int [10]; // allocates an array of ten integers delete pi; delete [ ] pia; (int) new and delete

Reference Type A reference serves as an alternative name for an object. int ival = 1024; int &refVal = ival; // ok: refVal is a reference to ival int &refVal; // error: a reference must be initialized to an object refVal += 2; // ok int *pi = &refVal; // initialize pi with the address of ival Similar to the use of a pointer but without resorting to pointer syntax.

References (Cont’d) // defines two objects of type int int ival = 1024, ival2 = 2048; // defines one reference and one object int &rval = ival, rval2 = ival2; // defines one object, one pointer, one reference int ival3 = 1024, *pi = &ival3, &ri = ival3; // defines two references int &rval3 = ival3, &rval4 = ival2; ival ival2 rval 2048 rval2 ival3 pi rval3 rval4 ri

Pointers, Arrays, and References int a[ ] = { 3, 8, -4, 6 }; int &b = a[0]; int *c = &(a[3]); int *&d = c; int **e = &d; What are stored in the array a[ ] after execution of the statements below? b--; *d += b + a[1]; c = &(a[1]); **e -= a[2]; c[0] = *d + **e + b; d[1] = 2 * (**e);

Initialization (1) int a[ ] = { 3, 8, -4, 6 }; int &b = a[0]; a[0] a[1] a[2] a[3] b is an alias for a[0]. It can be seen as another “variable” sharing the memory location assigned to a[0]. b reference to a variable

Initialization (2) int *c = &(a[3]); int *&d = c; a[0] a[1] a[2] a[3] b c is a pointer pointing at a[3]. c d is a reference (different name) for the memory location named c. Whenever c changes its value, that is, points to a new location, d automatically changes its value and points to the same new location. d reference to a pointer address

Initialization (3) int **e = &d; a[0] a[1] a[2] a[3] b e is a pointer pointing at the memory location labeled by d, i.e., by c. cd e address e is a pointer to a pointer.

Update (1) b--; a[0] a[1] a[2] a[3] b cd e *d += b + a[1]; a[0]--; 216 a[3] += a[0] + a[1];

Update (2) c = &(a[1]); a[0] a[1] a[2] a[3] b cd e **e -= a[2] ; a[1] -= a[2]; c now points at a[1]; so does d c[0] = *d + **e + b; a[1] = a[1] + a[1] + a[0]; 26 d[1] = 2 * (**e); a[2] = 2 * a[1];