1 / 45

Subject: Computer Programming using C Topic: Pointers Comp.Engg(third sem)

Subject: Computer Programming using C Topic: Pointers Comp.Engg(third sem). by Narinder Pal GPCG,Jalandhar. Contents. Introduction Operators used with pointers Pointer declaration IIIustration Pointer Arithmetic Pointer Arithmetic and Array Pointer to Pointer Pointer to Function

abrial
Download Presentation

Subject: Computer Programming using C Topic: Pointers Comp.Engg(third sem)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Subject: Computer Programming using CTopic: PointersComp.Engg(third sem) by Narinder Pal GPCG,Jalandhar

  2. Contents • Introduction • Operators used with pointers • Pointer declaration • IIIustration • Pointer Arithmetic • Pointer Arithmetic and Array • Pointer to Pointer • Pointer to Function • Pros and Cons • Rules

  3. Intro The Pointer is a Variable which holds the Addressof 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.

  4. EXAMPLE FOR POINTER IntX=547; Variable Name Contents Location 547 X 4000 4000 4036 ptr

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

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

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

  8. 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= *&quantity; n= quantity; n=*p; There are equal

  9. An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; *ptr = 3; *pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;

  10. An Illustration int i = 5, j = 10; int *ptr;// declare a pointer-to-integer value int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; *pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;

  11. Double indirection An Illustration int i = 5, j = 10; int *ptr; int **pptr;// declare a pointer -to-pointer-to-integer value ptr = &i; pptr = &ptr; *ptr = 3; *pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;

  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;

  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;

  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;

  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;

  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;

  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;

  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;

  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;

  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!!!

  21. Don’t and Do • Don,t try to perform mathematical operations such as division,multiplication, and modulus on pointers. Adding and Subtracting pointers are acceptable. • Don,t forget that subtracting from or adding to pointer changes the pointer based on the size of the data type to.It doesn,t change it by 1 or by the number being added (unless it’s a pointer to a one byte character).

  22. Don’t and Do • Don’t try to increment or decrement an array variable. Assign a pointer to the beginning address of the array and increment it. • Do understand the size of variable types on your computer.

  23. Pointer Arithmetic and Array Example: The following code initializes the pointer variable p_array with the address of the first element of array[]: int array[100],*p_array; p_array=array;

  24. Illustration // to find the smallest element in array of N elements #include<stdio.h> #include<conio.h> main() { int i,n,small,*ptr,a[50]; printf(“\n size of the array\n”); scanf(%d”,&n); printf(“\n Array element\n”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); for(i=0;i<n;i++) printf(“%5d”,a[i]); ptr=a; //assign address of a[0] to pointer ptr small=*ptr; //contents of a[0] assigned to small ptr++; //pointer points to next element in the array i.e[i] for(i=0;i<n;i++) { if small>*ptr) small=*ptr; ptr++; } printf(“\n Smallest element is %5d\n”,small); }

  25. Array of pointer • A multidimensional array can be expressed in term of array of pointer rather than a pointer to a group of contiguous array.An arry of pointer is declared as data-type *array[size of array] //2D array In normal 2D array consider the declaration: int a[3][4]; but in array of pointer it can be represented as: int *a[3]; Row size

  26. Pointer Arithmetic and Array

  27. Pointer to pointer Pointer is itself a numeric variable, it is stored in your computer’s memory at a particular address. Therefore,you can create a pointer to a pointer, a variable whose value is the address of a pointer. int x=12; //x is a type int variable int *ptr =&x;// ptr is a pointer to x int **ptr_to_ptr=&ptr; /*ptr_to_ptr is a pointer to a pointer to type int*/ ptr ptr_to_ptr X 2050 8050 12 2050 8050 1020

  28. Pointer to pointer Example #include<stdio.h> #include<conio.h> Main() { int p=5,*q,**r; q=&p; r=&q; printf(“\n Value of p:%d”,p); printf(“\n value of *q:%d”,*q); printf(“\n Value of **r:%d,**r); getch(); } Output: Value of p:5 Value of *q:5 Value of **r:5

  29. Pointers and Functions Pointers are used with functions in different ways.Following are the main usages of pointers in relation to functions • Pointers as arguments • Functions returning pointers

  30. Pointer as arguments • Here instead of pass photocopy of actual argument to formal argument,we pass actual address of actual arguments to formal arrgument (call by reference). #include<stdio.h> #include<conio.h> main() { int a,b,res; int sum(int*,int*) //function prototype printf(“\n Enter Two No.”); scanf(“%d%d”,&a,&b); res=sum(&a,&b); //passing address of actual arguments getch(); } int sum(int*x,int*y) //pointers as formal arguments { int ans; ans=*x+*y; return(ans); }

  31. Function returning pointers We can create functions that returns pointer variables. With this method we can return multiple values from a function. #include<stdio.h> #include<conio.h> int *big(int*,int*); main() { int a,b,res; int sum(int*,int*) //function prototype printf(“\n Enter Two No.”); scanf(“%d%d”,&a,&b); res=big(&a,&b); //passing address of actual arguments printf(“The bigger value is=%d”,res); getch(); } int *big(int*x,int*y) //pointers as formal arguments { if(*x>*y) return(x); else return(y); }

  32. THANKS

More Related