Pointers in C

Views:
 
     
 

Presentation Description

No description available.

Comments

By: mujahidsppt (36 month(s) ago)

plz send this ppt to mujahids44@gmail.com

Presentation Transcript

Pointers in C : 

Pointers in C

Overview : 

Overview

Intro : 

Intro 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.

Slide 4: 

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

Operators used with Pointers : 

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 : 

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: int * p ; / * integer pointer */ float *p; /* float pointer */ HERE: 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

In Detail : 

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; quantity = 179; p = & quantity; n = *p;

EXPLANATION: : 

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

An Illustration : 

9 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;

An Illustration : 

10 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;

An Illustration : 

11 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; Double Indirection

An Illustration : 

12 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;

An Illustration : 

13 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;

An Illustration : 

14 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;

An Illustration : 

15 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;

An Illustration : 

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;

An Illustration : 

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;

An Illustration : 

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;

An Illustration : 

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;

Pointer Arithmetic : 

20 Pointer Arithmetic What’s ptr + 1? The next memory location! What’s ptr - 1? The previous memory location! What’s ptr * 2 and ptr / 2? Invalid operations!!!

Pointer Arithmetic and Array : 

21 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;

Pointer Arithmetic and Array : 

22 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;

Pointer Arithmetic and Array : 

23 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;

Pointer Arithmetic and Array : 

24 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;

Pointer Arithmetic and Array : 

25 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;

Pointer Arithmetic and Array : 

26 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;

Pointer Arithmetic and Array : 

27 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;

Pointer Arithmetic and Array : 

28 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;

Pointer Arithmetic and Array : 

29 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;

Pointer Arithmetic and Array : 

30 Pointer Arithmetic and Array Type of a is float * a[2]  *(a + 2) ptr = &(a[2]) ptr = &(*(a + 2)) ptr = a + 2 a is a memory address constant ptr is a pointer variable 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;

Arithmetic and Logical Operations on Pointers : 

Arithmetic and Logical Operations on Pointers 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.

Arithmetic Operations on Pointers : 

Arithmetic Operations on Pointers When an integer is added to or subtracted from a pointer, the new pointer value is changed by the integer times the number of bytes in the data variable the pointer is pointing to. For example, if the pointer valptr contains the address of a double precision variable and that address is 234567870, then the statement: valptr = valptr + 2; would change valptr to 234567886

Pros and Cons : 

Pros and Cons 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. Advantages of Pointers

Pros and Cons : 

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)

Rules : 

Rules Always initialize a pointer by calling an allocator Always check to see if the allocation request failed Always deallocate memory controlled by a pointer when done with it Never access memory that has not been allocated - pay attention to the size of arrays Never access memory that has been deallocated Do not allocate and deallocate memory with wild abandon, because that can fragment the virtual memory address space, causing future allocation requests to fail - particularly in long running programs such as web servers