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

Similar documents
Lecture 05 POINTERS 1

MYcsvtu Notes LECTURE 34. POINTERS

by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming Session 20

C++ Programming Chapter 7 Pointers

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

CSC 211 Intermediate Programming. Arrays & Pointers

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

Programming for Engineers Pointers

Chapter 6 - Pointers

Fundamentals of Programming Session 20

Lecture 9 - Pointers 1

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

[0569] p 0318 garbage

First of all, it is a variable, just like other variables you studied

Variation of Pointers

C Pointers Pearson Education, Inc. All rights reserved.

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

Homework #3 CS2255 Fall 2012

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

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

School of Science and Technology

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

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

More Pointers Week 11

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

Arrays and Pointers (part 1)

PROGRAMMAZIONE I A.A. 2017/2018

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Content. In this chapter, you will learn:

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Fundamental of Programming (C)

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

What is an algorithm?

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

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

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

Relationship between Pointers and Arrays

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

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

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:

Arrays and Pointers (part 1)

Basics of Programming

i location name 3 value at location 6485 location no.(address)

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

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Pointers. Pointers. Pointers (cont) CS 217

Introduction to Computer Science Midterm 3 Fall, Points

Computers Programming Course 6. Iulian Năstac

Dynamic Memory Allocation

Chapter 7 C Pointers

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

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

... Lecture 12. Pointers

Pointers. 10/5/07 Pointers 1

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings

Functions. Introduction :

Algorithms & Data Structures

Understanding Pointers

More about BOOLEAN issues

C Programming Language

POINTER & REFERENCE VARIABLES

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

UNDERSTANDING THE COMPUTER S MEMORY

Language comparison. C has pointers. Java has references. C++ has pointers and references

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Week 3: Pointers (Part 2)

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

POINTERS, STRUCTURES AND INTRODUCTION TO DATA STRUCTURES

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

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):

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

UNIT - V STRUCTURES AND UNIONS

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Arrays and Pointers. CSE 2031 Fall November 11, 2013

DECLARAING AND INITIALIZING POINTERS

Expressions and Precedence. Last updated 12/10/18

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

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

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Department of Computer Applications

Computers Programming Course 5. Iulian Năstac

SYSC 2006 C Winter 2012

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

EL2310 Scientific Programming

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Transcription:

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

Introduction The Pointer is a Variable which holds the Address of the other Variable in same memory. Such as Arrays, structures, and Functions that are used in program. It contains only the Memory Location of the variable rather than its content.

Pointers Pointer is a variable that contains address of another variable. Consider the declaration: int i=3; main() 625524 { int i=3; printf( \naddress of i=%u,&i); printf( \nvalue of i=%d,i); } i 3 Format specifier for unsigned int Location name Value at location Location number Address of

EXAMPLE FOR POINTER: int X = 547; int *ptr; ptr = &X; Veriable name Contents Location X 547 4000 ptr 4000 4036

Operators used with Pointers 1.The address operator & [ampersand] An address operator gives the address of the variable. 2.The indirection operator * [asterisk] The indirection operator gives the value of the variable that the pointer is pointing to.

POINTER DECLARATION In c Every Variable must be Declared its type,since pointer variables contain address of separate Data type, They must be Declared before use them the declaration of pointer variable takes the following form syntax: data _type * pt_name EXAMPLE: 1. int * p; / * integer pointer */ 2. float *p; /* float pointer */ HERE: 1. Declares the variable p as a pointer variable that points to an integer data type 2. Declares the variable p as a pointer variable that points to an float data type

Address of i=65825 value of i=3 void main() { } int i=3; printf( \naddress of i=%u,&i); printf( \nvalue of i=%d,i); printf( \nvalue of i=%d,*(&i)); Address of i=24524 value of i=3 value of i=3 i 3 625524 Value at address Location name Value at location Location number

j=&i; i 3 65258 j 65258 32525 But, we need to declare int *j; means value at address contained in j is an int 8

In Detail We access the value of the variable by help of pointer this is done by using another unary operator * (asterisk) usually known as Indirection operator consider following statements int quantity, *p,n; //Declare two integer and one pointer variable quantity = 179; p = & quantity; // Assign 179 to quantity // Save address of quantity into p n = quantity; n = *p; n = *(&quantity) //Assign value of quantity to n

EXPLANATION: The First line declares quantity & n as integer variable and p as pointer variable The second line Assigns value 179 to variable quantity The third line assigns address of variable quantity to the pointer variable p The forth line contains * operator in this case *p returns value of variable quantity Now value of n would be 179 p = & quantity; n = * p; n = * & quantity; n = quantity; These are equal

Main() { int i=3,*j, **k; j=&i; k=&j; i 3 65258 65258 32525 j Illustration 32525 65522 printf( \n %u,&i); 65258 Address of i Value at j printf( \n %u,j); printf( \n %u,*k); printf( \n %u,&j); printf( \n %u,k); printf( \n %u,&k); printf( \n %u,j); printf( \n %u,k); printf( \n %d,i); printf( \n %d, *(&i)); k printf( \n %d, *j); 11 printf( \n %d, **K);

An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 12

An Illustration int i = 5, j = 10; int *ptr; /* declare a pointer-to-integer variable */ int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable 13

An Illustration int i = 5, j = 10; int *ptr; int **pptr; /* declare a pointer-to-pointer-to-integer variable */ ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr pptr int * integer pointer variable int ** integer pointer pointer variable Double Indirection 14

An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; /* store address-of i to ptr */ pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable 15

An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; /* store address-of ptr to pptr */ *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr 16

An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 3 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *ptr int de-reference of ptr 3 17

An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable **pptr int de-reference of dereference of pptr address of ptr 7 18

An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 10 ptr int * integer pointer variable address of j pptr int ** integer pointer pointer variable address of ptr *ptr int de-reference of ptr 10 19

An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 9 ptr int * integer pointer variable address of j pptr int ** integer pointer pointer variable **pptr int de-reference of dereference of pptr address of ptr 9 20

An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 9 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *pptr int * de-reference of pptr value of ptr (address of i) 21

An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable -2 j int integer variable 9 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *ptr int de-reference of ptr -2 22

Pointers Pointer Arithmetic

Pointer Expressions and Pointer Arithmetic Only Two Arithmetic operations can be performed on pointers : addition and subtraction Increment/decrement pointer (++ or --) Example: ++vptr, vptr++, --vptr, vptr-- Add an integer to a pointer( + or +=, - or -=) Example: vptr=vptr+3, vptr+=3, vptr=vptr-3, vptr-=3 Pointers may be subtracted from each other Operations meaningless unless performed on an array

Example Let P1 be an integer pointer with a current value of 2000. Assume ints are 2 byte long. Then take an expression P1++; Now P1 contains 2002 not 2001. Reason is that each time P1 is incremented it will point to the next integer. The same is true for decrement Now P1 contains 2000 P1- -;

Example Five elements (float) array on machine with 4 byte floats vptr points to first element v[ 0 ]whose address location is 3000 (vptr = 3000) vptr += 2; Or vptr = vptr+2; // sets vptr to 3008 vptr points to v[ 2 ] (incremented by 2), but the machine has 4 byte float, so it points to address 3008 pointer variable vptr location 3000 3004 3008 3012 3016 v[0] v[1] v[2] v[3] v[4]

Char *Ch = &I; Int *In = &J; Example //assume(3000) //assume(3000) Ch Ch+1 Ch+2 Ch+3 Ch+4 Ch+5 3000 3001 3002 3003 3004 3005 Memory In In+1 In+2

Rule An integer quantity can be added to or subtracted from a pointer variable The amount of increment depends on the type of the pointer variable pv+n pv + n*sizeof(variable type that pointer point to) pv-n pv - n*sizeof(variable type that pointer point to)

Example: (double pointer) Assume long (long integer) is 4 bytes, and pointer variable is 2 bytes. long a[10]={5,10,15, }; long *pa, **ppa; int i=5; pa = &a[0]; Expression Value Note 800 700 ppa ppa=&pa; 700 640 pa Variable Address Value a 640 5 644 10 648 15 640 644 648 652 656 660 a 5 10 15 pa 700 640 ppa 800 700 pa+1 644 640+1*4 pa+3 652 640+3*4 pa+i 660 640+i*4 ppa+1 702 700+1*2 ppa+i 710 700+i*2 *pa+1 6 5+1 *(pa+1) 10 a[1]=pa[1]=*(a+1) pa[2] 15 648 *ppa 640 value of pa *ppa+1 644 pa+1 *(ppa+1) invalid *(702) **ppa+1 6 a[0]+1 = 5+1 *(*ppa+1) 10 *(pa+1)=*(640+1*4)

Subtracting pointers One pointer variable can be subtracted from another provided both variables point to elements of the same array. The resulting value indicates the number of elements separating the corresponding array elements. Example: #include<stdio.h> #include<conio.h> void main() { } int arr[] = {10,20,30,40,50,60,70}; int *i, *j; i = &arr[1]; j = &arr[5]; printf( %d %d, j-i, *j-*i); getch(); Output: 4 40 Suppose array begins at location 65502, then the elements would be present at locations 65504 and 65512 respectively. j-i would print a value 4 and not 8. this is because j and i are pointing to locations that are 4 integer apart

Pointer comparison Pointer comparison ( <, ==, > ) See which pointer points to the higher numbered array element Also, see if a pointer points to 0

Pointer assignment Pointers of the same type can be assigned to each other If not the same type, a cast operator must be used Exception: pointer to void (type void *) Generic pointer, represents any type No casting needed to convert a pointer to void pointer void pointers cannot be dereference

Arrays and pointers are closely related` Array name like a constant pointer Pointers can do array subscripting operations Example: Declare an array b[ 5 ] and a pointer bptr bptr = b; // To set them equal to one another // The array name (b) is actually the address of first element of the array bptr = &b[ 0 ]; // Explicitly assigns bptr to address of first element of b To access element b[ 3 ]: x=*( bptr + 3 ) // Where 3 is the offset. Called pointer/offset notation x=bptr[ 3 ] // Called pointer/subscript notation x=*( b + 3 ) // bptr[ 3 ] same as b[ 3 ] // Performing pointer arithmetic on the array itself

Pointers and Arrays Strong relation between pointers and arrays Pointers and arrays can be used interchangeably. The array name is equivalent to the address of the first element in the array Example: int a[10]; int *pa; pa = &a[0]; /* is equivalent to pa = a */ So, a[1] *(pa+1) pa[1] *(a+1) &a[1] pa+1 a+1 a[i] *(pa+i) pa[i] *(a+i) &a[i] pa+i a+i a[i]+=5 *(pa+i)+=5 pa[i]+=5 Example: f(int s[]) { } f(int *s) { }

Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable)? a[1] float float array element (variable)? a[2] float float array element (variable)? a[3] float float array element (variable)? ptr float * float pointer variable *ptr float de-reference of float pointer variable? 35

Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable)? a[1] float float array element (variable)? a[2] float float array element (variable)? a[3] float float array element (variable)? ptr float * float pointer variable address of a[2] *ptr float de-reference of float pointer variable? 36

Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable)? a[1] float float array element (variable)? a[2] float float array element (variable) 3.14 a[3] float float array element (variable)? ptr float * float pointer variable address of a[2] *ptr float de-reference of float pointer variable 3.14 37

Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable)? a[1] float float array element (variable)? a[2] float float array element (variable) 3.14 a[3] flot float array element (variable)? ptr float * float pointer variable address of a[3] *ptr float de-reference of float pointer variable? 38

Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable)? a[1] float float array element (variable)? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr *ptr float * float pointer variable float de-reference of float pointer variable address of a[3] 9.0 39

Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable)? a[1] float float array element (variable)? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr *ptr float * float pointer variable float de-reference of float pointer variable address of a[0]? 40

Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable)? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr float * float pointer variable address of a[0] *ptr float de-reference of float pointer variable 6.0 41

Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable)? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr float * float pointer variable addres s of a[2] *ptr float de-reference of float pointer variable 3.14 42

Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable)? a[2] float float array element (variable) 7.0 a[3] float float array element (variable) 9.0 ptr *ptr float * float pointer variable float de-reference of float pointer variable address of a[2] 7.0 43

Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable)? a[2] float float array element (variable) 7.0 a[3] float float array element (variable) 9.0 ptr *ptr float * float pointer variable float de-reference of float pointer variable address of a[1] 7.0 44

Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable) 7.0 a[2] float float array element (variable) 7.0 a[3] float float array element (variable) 9.0 ptr *ptr float * float pointer variable float de-reference of float pointer variable address of a[1] 7.0 45

Example: (double pointer) Assume long (long integer) is 4 bytes, and pointer variable is 2 bytes. long a[10]={5,10,15, }; long *pa, **ppa; int i=5; pa = &a[0]; Expression Value Note 800 700 ppa ppa=&pa; 700 640 pa Variable Address Value a 640 5 644 10 648 15 640 644 648 652 656 660 a 5 10 15 pa 700 640 ppa 800 700 pa+1 644 640+1*4 pa+3 652 640+3*4 pa+i 660 640+i*4 ppa+1 702 700+1*2 ppa+i 710 700+i*2 *pa+1 6 5+1 *(pa+1) 10 a[1]=pa[1]=*(a+1) pa[2] 15 648 *ppa 640 value of pa *ppa+1 644 pa+1 *(ppa+1) invalid *(702) **ppa+1 6 a[0]+1 = 5+1 *(*ppa+1) 10 *(pa+1)=*(640+1*4)

Arrays of Pointers Arrays can contain pointers For example: an array of strings char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"}; Strings are pointers to the first character char * each element of suit is a pointer to a char The strings are not actually stored in the array suit, only pointers to the strings are stored suit[0] H e a r t s \0 suit[1] suit[2] suit[3] D i a m o n d s \0 C l u b s \0 S p a d e s \0 suit array has a fixed size, but strings can be of any size

Summary A pointer may be incremented or decremented An integer may be added to or subtracted from a pointer. Pointer variables may be subtracted from one another. Pointer variables can be used in comparisons, but usually only in a comparison to NULL.

Summary Do not attempt the following operations on pointers they would never work out; Addition of two pointers Multiplication of a pointer with a constant Division of a pointer with a constant

Pros and Cons Advantages of Pointers Pointer reduces the length and complexity of a program. Pointers are more efficient in handling the data tables. Pointers increase the execution speed. Pointers are closely associated with arrays and provide an alternate way to access individual array elements. Function cannot return more than one value. But when the same function can modify many pointer variables and function as if it is returning more than one variable.

Pros and Cons Disadvantages of Pointers If sufficient memory is not available during runtime for the storage of pointers, the program may crash (least possible) If the programmer is not careful and consistent with the use of pointers, the program may crash (very possible)

Thank You!!