CSC 211 Intermediate Programming. Arrays & Pointers

Similar documents
by Pearson Education, Inc. All Rights Reserved.

Pointers and Strings Prentice Hall, Inc. All rights reserved.

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

C++ Programming Chapter 7 Pointers

Lecture 05 POINTERS 1

Fundamentals of Programming Session 20

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Week 3: Pointers (Part 2)

Chapter 7 C Pointers

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

Unit III Virtual Functions. Developed By Ms. K.M.Sanghavi

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

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

Programming for Engineers Pointers

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

C Pointers Pearson Education, Inc. All rights reserved.

MYcsvtu Notes LECTURE 34. POINTERS

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

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

Chapter 6 - Pointers

Chapter 5 - Pointers and Strings

What is an algorithm?

Chapter 5 - Pointers and Strings

Pointers, Dynamic Data, and Reference Types

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Relationship between Pointers and Arrays

Chapter 1: Object-Oriented Programming Using C++

Fundamentals of Programming Session 20

[0569] p 0318 garbage

Content. In this chapter, you will learn:

C++ For Science and Engineering Lecture 15

In this chapter, you will learn about: Pointers. Dynamic Arrays. Introduction Computer Science 1 CS 23021

Reference operator (&)

Pointers and Dynamic Memory Allocation

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Review of Important Topics in CS1600. Functions Arrays C-strings

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

Chapter 9. Pointers and Dynamic Arrays

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

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

What have we learned about when we learned about function parameters? 1-1

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

Homework #3 CS2255 Fall 2012

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

Dynamic Allocation in C

Pointers. Variable Declaration. Chapter 10

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9.

CSC 270 Survey of Programming Languages. What is a Pointer?

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

12. Pointers Address-of operator (&)

Lecture 23: Pointer Arithmetic

Review of the C Programming Language for Principles of Operating Systems

IS 0020 Program Design and Software Tools

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Lecture 4. Default Arguments. Set defaults in function prototype. int myfunction( int x = 1, int y = 2, int z = 3 );

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Introduction to C++

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

CS2141 Software Development using C/C++ C++ Basics

Variation of Pointers

In Java we have the keyword null, which is the value of an uninitialized reference type

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

Pointers. Reference operator (&) ted = &andy;

C++ PROGRAMMING SKILLS Part 4: Arrays

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

Chapter 11: Pointers

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

CSC1322 Object-Oriented Programming Concepts

Review of the C Programming Language

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Introduction to Computer Science Midterm 3 Fall, Points

Pointers! Arizona State University 1

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Lab 3. Pointers Programming Lab (Using C) XU Silei

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

CS201- Introduction to Programming Current Quizzes

Arrays. Week 4. Assylbek Jumagaliyev

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

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

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Pointers in C/C++ 1 Memory Addresses 2

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

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

Dynamic Memory Allocation

Pointers and Dynamic Arrays

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

BITG 1113: POINTER LECTURE 12

Introduction to C++ Systems Programming

Chapter 7 Array. Array. C++, How to Program

CSC 211 Intermediate Programming. Pointers

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

UNIT- 3 Introduction to C++

Pointers, Arrays and C-Strings

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.

CA31-1K DIS. Pointers. TA: You Lu

Transcription:

CSC 211 Intermediate Programming Arrays & Pointers 1

Definition An array a consecutive group of memory locations that all have the same name and the same type. To create an array we use a declaration statement. It should indicate three things: The type of the value to be stored in each element The name of the array The number of elements in the array Examples: int c[12] ; int b[100], x[27] ; 2

The general form for declaring an array is: typename arrayname[arraysize] ; The expression arraysize, which is the number of elements, must be a constant, or a constant variable, or a constant expression. Constant variables must be initialized when they are declared and can not be modified thereafter. Using constant variables to specify the array size makes program more scalable. 3

Discussions If there are fewer initializer than elements in the array, the remaining elements are automatically initialized to zero. int n[10] = { 0 } ; explicitly initializes the first element to zero and implicitly initializes the remaining nine elements to zero. Automatic arrays are not implicitly initialized to zero. The programmer must at least initialize the first element to zero for the remaining elements to be automatically zeroed. The following array declaration int n[5] = {32, 27, 64, 18, 95, 14} ; would cause a syntax error because there are 6 initilizers and only 5 array elements. If the array size is omitted from a declaration with an initializer list, the number of elements in the array will be the number of elements in the initializer list. int n[ ] = { 1, 2, 3, 4, 5 } ; 4

Static Arrays and Local Static Variables Arrays that are declared static are initialized when the program is loaded. If a static array is not explicitly initialized by the programmer, that array is initialized to zero by the compiler. A static local variable in a function definition exists for the duration of the program but is only visible in the function body. 5

Passing Arrays to Functions To pass an array argument to a function, specify the name of the array without any brackets. The array size is normally pass as well so the function can process the specific number of elements in the array. C++ automatically passes arrays to functions using simulated call-byreference. The name of the array is the address of the first element of the array. Because the starting address is passed, the called function knows precisely where the array is stored. Therefore, when the called function modifies array elements in its function body, it is modifying the actual elements of the array in their original location. Although entire array are passed call-by-reference, individual array elements are passed call-by-value exactly as simple variables are. The function s parameter list must specify that an array will be received. For example void modifyarray ( int b[ ], int arraysize ) ; The size of the array is not required between the brackets. If it is included, the compiler will ignore it. The function prototype in this case is void modifyarray ( int [ ], int ) ; 6

Pointer Expressions and Pointer Arithmetic vptr = v ; location vptr = &v[0] ; 3000 3004 3008 3012 3016 v[0] v[1] v[2] v[3] v[4]. Pointer variable vptr Fig. 1. The array v and a pointer vptr that points to v. 7

Example vptr += 2 ; would produce 3008 (3000 + 2 * 4) assuming an integer is stored in 4 bytes of memory. In the array v, vptr would now point to v[2]. If vptr has been incremented to 3016, which points to v[4], the statement vptr -= 4 ; would set vptr back to 3000 at the beginning of the array. ++vptr ; vptr++ ; increments the pointer to point to the next location of the array. Each of the statements --vptr ; vptr-- ; decrements the pointer to point to the previous element of the array. 8

Example cont d Pointer variables may be subtracted from one another. For example, if vptr contains the location 3000, and v2ptr contains the address 3008, the statement x = v2ptr - vptr would assign to x the number of array elements from vptr to v2ptr, in this case 2. A pointer can be assigned to another pointer if both pointers are of the same type. Otherwise, a cast operator must be used to convert the value of the pointer on the right of the assignment to the pointer type on the left of the assignment. 9

The Relationship Between Pointers and Arrays Assume that integer array b[5] and integer pointer variable bptr have been declared. bptr = b ; This is equavalent to bptr = &b[0] ; Array element b[3] can alternatively be referenced with the pointer expression *( bptr + 3 ) ; The 3 in the above expression is the offset to the pointer. This notation is called a pointer/offset notation. The parentheses are necessary because the precedence of * is higher than the precedence of +. The address &b[3] can be written with the pointer expression bptr + 3. 10

The Relationship Between Pointers and Arrays cont d The array itself can be treated as a pointer and used in pointer arithmetic. For example, the expression *( b + 3) also refers to the array element b[3]. Pointers can be subscripted exactly as arrays can. For example, the expression bptr[1] refers to the array element b[1]. This expression is referred to as pointer/subscript notation. An array name is a constant pointer: it always points to the beginning of the array. Thus, the expression b += 3 is invalid because it attempts to modify the value of the array name with pointer arithmetic. 11

// Using subscripting and pointer notations with arrays #include <iostream.h> int main() { int b[] = { 10, 20, 30, 40 }; int *bptr = b; // set bptr to point to array b cout << "Array b printed with:\n" << "Array subscript notation\n"; for ( int i = 0; i < 4; i++ ) cout << "b[" << i << "] = " << b[ i ] << '\n'; cout << "\npointer/offset notation where\n" << "the pointer is the array name\n"; for ( int offset = 0; offset < 4; offset++ ) cout << "*(b + " << offset << ") = " << *( b + offset ) << '\n'; cout << "\npointer subscript notation\n"; for ( i = 0; i < 4; i++ ) cout << "bptr[" << i << "] = " << bptr[ i ] << '\n'; cout << "\npointer/offset notation\n"; for ( offset = 0; offset < 4; offset++ ) cout << "*(bptr + " << offset << ") = " << *( bptr + offset ) << '\n'; return 0; } 12

Output Array b printed with: Array subscript notation b[0] = 10 b[1] = 20 b[2] = 30 b[3] = 40 Pointer/offset notation where The pointer is the array name *( b + 0 ) = 10 *( b + 1 ) = 20 *( b + 2 ) = 30 *( b + 3 ) = 40 Pointer subscript notation bptr[0] = 10 bptr[1] = 20 bptr[2] = 30 bptr[3] = 40 Pointer/offset notation *( bptr + 0 ) = 10 *( bptr + 1 ) = 20 *( bptr + 2 ) = 30 *( bptr + 3 ) = 40 13

Allocating Memory With new Example: int *y = new int ; or int *y ; *y = 10 ; y = new int ; *y = 10 ; or int *y = new int[10] ; The general form is typename pointer_name = new typename ; typename pointer_name = new typename[intexpression] ; 14

Example #include <iostream> using namespace std ; int main( 0 { int *pi = new int ; // allocate space for an int *pi = 1001 ; // store a value there cout << int ; cout << value = << *pi << : location = << pi << \n ; double *pd = new double ; // allocate space for for a double *pd = 10000001.0 ; // store a double there cout << double ; 15

Example cont d cout << value = << *pd << : location = << pd << \n ; cout << size of pi = << sizeof pi ; cout << : size of *pi = << sizeof *pi << \n ; cout << size of pd = << sizeof pd ; cout << : size of *pd = << sizeof *pd << \n ; return 0 ; } Output int value = 1001 : location == 007B0A60 double value = 1e+007 : location = 007B0CD0 size of pi = 4 : size of *pi = 4 size of pd = 4 : size of *pd = 8 16

Discussions int *intptr ; char *namestr ; intptr = new int ; namestr = new char[6] ; // creates a variable of type int and stores // its address into intptr // creates a 6-element char array and stores the // base address of the array into namestr The new operator does two things: it creates an uninitialized variable (or array) of designated type; it returns a pointer to this variable (or the base address of an array). A dynamic variable is unnamed and cannot be directly addressed. It must be indirectly addressed through the pointer returned by the new operator. 17

Delete operator The built-in operator delete is used to destroy a dynamic variable. The delete operator has two forms, one for deleting a single variable, the other for deleting an array: delete Pointer ; // Delete the variable pointed to by Pointer delete [ ] pointer ; The delete operation does not delete the pointer; it deletes the pointed-to-variable. Applying delete to the NULL pointer has no effect. The delete operator must only be applied to a pointer value that was obtained from the new operator. delete intptr ; // returns the variable pointed to by intptr to the free store. // The value of intptr is then undefined delete [ ] namestr ; // Returns the array pointed to by namestr to the free // store. The value of namestr is then undefined. 18

Memory Leak Remember that a major reason for using dynamic data is to economize on memory space. It is counterproductive to keep dynamic variables when they are no longer needed a situation known as a memory leak. If this is done too often, you may run out of memory. Example: int *ptr1 = new int ; // create a dynamic variable int *ptr2 = new int ; // create a dynamic variable *ptr2 = 44 ; // assign a value to a dynamic variable *ptr1 = *ptr2 ; // copy one dynamic variable to another ptr1 = ptr2 ; // copy one pointer to another delete ptr2 ; // destroy a dynamic variable 19

Inaccessible Object & Dangling Pointer Inaccessible Object: A Dynamic variable on the free store without any pointer pointing to it. Dangling Pointer: A pointer that points to a variable that has been deallocated. #include <stddef.h>.... int *ptr1 = new int ; int *ptr2 = new int ; *ptr2 = 44 ; *ptr1 = *ptr2 ; delete ptr1 ; ptr1 = ptr2 ; delete ptr2 ; ptr1 = NULL ; // For NULL // Avoid an inaccessible object // Avoid a dangling pointer 20