ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.

Slides:



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

Programming and Data Structure
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
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.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Multiple-Subscripted Array
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
Pointers CSE 2451 Rong Shi.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Microsoft Visual C++.NET Chapter 61 Memory Management.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
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”
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
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.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Chapter 8: Part 3 Collections and Two-dimensional 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.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
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.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
Windows Programming Lecture 03. Pointers and Arrays.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
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.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Objectives You should be able to describe: One-Dimensional Arrays
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
14th September IIT Kanpur
2-d Arrays.
MSIS 655 Advanced Business Applications Programming
Presentation transcript:

ADVANCED POINTERS

Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions returning pointers Pointers to functions

Review: Pointers and Arrays flizny == &flizny[0]; // name of array is the address of the first element Both flizny and &flizny[0] represent the memory address of that first element. Both are constants because they remain fixed for the duration of the program. They can be assigned as values to a pointer variable, and the variable can be changed.

Pointers and Arrays short double pointers + 0: 0x0064fd20 0x0064fd28 pointers + 1: 0x0064fd22 0x0064fd30 pointers + 2: 0x0064fd24 0x0064fd38 pointers + 3: 0x0064fd26 0x0064fd40

Pointers and Arrays The value of a pointer is the address of the object to which it points. How the address is represented internally is hardware dependent. The address of a large object, such as type double variable, typically is the address of the first byte of the object. Applying the * operator to a pointer yields the value stored in the pointed-to object. Adding 1 to the pointer increases its value by the size, in bytes, of the pointed-to type. dates +2 == &date[2] /* same address */ *(dates + 2) == dates[2] /* same value */

Troubles with pointers List of some pointer errors Assigning values to uninitialized pointers int *p, m = 100; *p = m;// Error Assigning values to a pointer variable int *p, m = 100; p = m;// Error Not dereferencing a pointer when required int *p, x = 100; *p = &x; printf(“%d”, p);// Error Assigning the address of an uninitialized variable int m, *p; p = &m; Comparing pointers that point to different objects char name1 [20], name2 [30]; char *p1 = name1; char *p2 = name; If (p1 > p2)… //Error

Multidimensional Arrays Two dimensional array definition: float rain[5][12]; // array of 5 arrays of 12 floats This tells us that each element is of type float[12]; that is, each of the five elements of rain is, in itself, an array of 12 float values. rain  Pointer to first row rain + 1  pointer to ith row *(p+i)  pointer to first element in the ith row *(p+i) + j  pointer to jth element in the ith row *(*(p+i) + j )  value stored in the cell (i,j) *(rain + 2) + 3

Initializing a Two-Dimensional Array More Dimensions int box[10][20][30]; You can visualize a one-dimensional array as a row of data, a two- dimensional array as a table of data, and a three-dimensional array as a stack of data tables. Typically, you would use three nested loops to process a three- dimensional array, four nested loops to process a four-dimensional array, and so on. char *name[3] = {“New Zealand”, “Australia”, “India”};

The program goal is to find a)the total rainfall for each year, b)the average yearly rainfall, and c)the average rainfall for each month. To find the total rainfall for a year, you have to add all the data in a given row. To find the average rainfall for a given month, you have to add all the data in a given column. The two-dimensional array makes it easy to visualize and execute these activities.

Pointers as functions arguments The function parameters are declared as pointers; The dereferenced pointers are used in the function body; When the function is called, the addresses are passed as actual arguments. array passed as a pointer Example 1: Sorting function Example 2: copying strings

Functions returning pointers The function larger receives the addresses of the variables a and b, decides which one is larger using the pointers x and y and the returns the address of its location. The returned value is then assigned to the pointer.

Pointers to functions A pointer to a function is declared as follows: type (*fptr) (); This tells the compiler that fptr is a pointer to a function, which returns type value. Assigning the name of the function to the pointer double mul(int, int); double (*p1)(); p1 = mul; Now we can call the function by the pointer p1 with the list of parameters. (*p1)(x,y)  mul(x,y)

Example: A program that uses a function pointer as a function argument A program prints the function values over a given range of values. The printing is done by the function table by evaluating the function passed to it by the main.