Pointer A variable that represent the location (rather than value) of a data item, such as variable or an array element It is used to pass information.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Pointers.
Dynamic memory allocation
Lectures 10 & 11.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
C Programming Lecture 14 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
1 Pointers. Variable Memory Snapshot 2 int nrate = 10; The variable is stored at specific memory address A variable is nothing more than a convenient.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Kernighan/Ritchie: Kelley/Pohl:
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
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.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Pointers Applications
Pointers CSE 2451 Rong Shi.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
Chapter 8 Arrays and Strings
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
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: 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:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Engineering Computing I Chapter 5 Pointers and Arrays.
Pointer Tran, Van Hoai. Pointers and Addresses  Pointer: group of cells (2,4 cells) Variable: group of cells Pointer is also a variable  Each cell (or.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
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.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Day 03 Introduction to C.
Day 03 Introduction to C.
Lecture 6 C++ Programming
14th September IIT Kanpur
Programming with Pointers
prepared by Senem Kumova Metin modified by İlker Korkmaz
7 Arrays.
C Programming Lecture-8 Pointers and Memory Management
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Pointers.
Presentation transcript:

Pointer A variable that represent the location (rather than value) of a data item, such as variable or an array element It is used to pass information back and forth between function and its reference point It provides a way to return multiple data items from a function via function arguments It also permits references to other functions to be specified as arguments to the given function

Pointer It is closely associated with arrays and therefore provide an alternate way to access individual array elements. It provides a convenient way to represent multidimensional arrays

Fundamentals If a data item occupies one or more contiguous memory cells, the data can be accessed if we know the location of of the first memory cell. If v is a data item, then the address of its location can be determined by &v (here & is address operator) We can assign this address into another variable pv=&v This new variable pv is pointer to v, since it points to the location of v

Fundamentals The relationship between pv and v is pv v Data item represented by v can be accessed by expression *pv, where * is called indirection operator. (v=*pv) If we write pv=&v and u=*pv, the value of v is indirectly assigned to u Address of v Value of v

Example int u=3, v, *pu, *pv; pu=&u; v=*pu; pv=&v; Address EC7 Address F8E pu u Address EC5 Address F8C pvv F8E3 F8C3

Example Int v=3,*pv,u1,u2; u1=2*(v+5) pv=&v; u2=2*(*pv+5); Pointer variable can be assigned the value of another pointer variable (i.e pv=pu); Pointer variable can point to another pointer variable int *p,*u,v=3; u=&v; p=&u; **p is 3;

Example Pointer variable can be assigned a null (zero) value, then it points nowhere pu=0 Ordinary variables cannot be assigned arbitrary addresses &x=F8C is not permitted Data type of the pointer must be the same as the data type of the object data.

Passing pointer to a function Passing arguments by reference (or by address or by location) funct(int *,int *); main() { funct(&u,&v); } funct(int *p, int *q) { *p=0; *q=0; }

Problems Analyzing a line of text (count no. of vowels, consonants, digits, whites paces & other characters) Swapping two variables Coordinate change from Cartesian to polar Square root of a quadratic equation

Why & is required in scanf? char item[20]; int partno; float cost; scanf(“%s %d %f”,item, &itemno, &cost); Since item is the name of an array, it is understood to represent an address and does not require & & is required for itemno and partno in order to access the addresses of these variables rather than its values

Passing a portion of an array main () { float z[100]; process(&z[50]);//or process(z+50); } void process(float f[])// or void process(float *f) { } Here 50 element of z (z[50] to z[99]) will be available in the process. If f[0] is changed, then z[50] will be affected.

Returning pointer to the caller double *scan(double z[]); main() { double z[100],*pz; pz=scan(z); } double *scan(double f[]) { double *pf; pf=… return pf; }

Pointers and one-dimensional arrays int x[10]={10,11,12,13,14,15}; suppose the address of x is 72. here x[2] means 12 *(x+2) means 12 &x[2] means 76 x+2 means 76 We can not write x++ or &x[2]=&x[1] If a numerical array is defined as a pointer variable, the array element cannot be assigned initialized values

Pointers and one-dimensional arrays But a character-type pointer variable can be assigned an entire string as a part of the variable declaration. Thus, a string can be represented either by a one-dimensional character array: char x[]=“This is a string”; or a character pointer. char *x=“This is a string”;

Dynamic memory allocation We can represent an array in terms of a pointer variable and allocate memory block for the array dynamically (during the runtime of the program on the basis of user’s requirement) int *x; x=(int *) malloc (10*sizeof(int)); Or scanf(“%d”,&n); x=(int *) malloc (n*sizeof(int)); Example: sorting a list of numbers.

Pointers and multidimensional arrays A multidimensional array can be represented by a lower-dimensional array of pointers int x[10][20]; is same as int (*x)[20]; here x points to the first row (or first array of 20 element), x+1 points to the second 20 element array and so on. x[2][5] is same as *(*(x+2)+5)

Pointers and multidimensional arrays int (*x)[20] means pointer to array of integers. Here is no. of rows is variable, but no. of column is fixed int *x[20] means array of pointer to integers. Here is no. of column is variable, but no. of row is fixed. Here x[2][5] is equivalent to *(x[2]+5).