unit-7 Pointerdesfsdfsdgsdgaa notes.pptx

TriggeredZulkar 6 views 35 slides May 18, 2025
Slide 1
Slide 1 of 35
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35

About This Presentation

dfwsdfde


Slide Content

Pointer Unit-6

Advantage of pointer Pointer  reduces the code  and  improves the performance , it is used to retrieving strings, trees, etc. and used with arrays, structures, and functions. We can  return multiple values from a function  using the pointer. It makes you able to  access any memory location  in the computer's memory.

Usage of pointer 1) Dynamic memory allocation In c language, we can dynamically allocate memory using malloc() and calloc () functions where the pointer is used. 2) Arrays, Functions, and Structures Pointers in c language are widely used in arrays, functions, and structures. It reduces the code and improves the performance.

An Introduction to Pointers Consider the declaration int i = 3 ; This declaration tells the C compiler to: Reserve space in memory to hold the integer value. Associate the name i with this memory location. Store the value 3 at this location.

We may represent i‘s location in memory

What is Pointer? A normal variable is used to store value. A pointer is a variable that stores the address/reference of another variable. Ex- * p = &n;  Pointer is derived data type in c language. Pointer contains the memory address of that variable as their value. Pointers are also called address variables because they contain the address of other variable. int  n = 10;    int * p = &n;  // Variable p of type pointer is pointing to the address of the variable n of type integer.

The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.

Declaration/initialization of pointer The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer. int  *a; //pointer to int    char  *c; //pointer to char

Pointer Example *p= 50; p=fff4; &p=aaa3

Address Of (&) Operator #include<stdio.h>    int  main(){   int  number=50;    printf ( "value of number is %d, address of number is % u" ,number,&number );     return  0;   }    

#include<stdio.h>    int  main(){   int  number=50;    // *= point the value of variable int  *p;       // declaration of pointer variable p=&number; //(65524) //stores the address of number variable      printf ( "Address of p variable is %x \ n" ,p );  printf ( "Value of p variable is %d \n" ,*p);        return  0;   }    

main( ) { int i = 3, *x ; float j = 1.5, *y ; char k = 'c', *z ; int *j ; j = & i ; printf ( "\ nValue of i = %d", i ) ; printf ( "\ nValue of j = %f", j ) ; printf ( "\ nValue of k = %c", k ) ; x = & i ; y = &j ;

z = &k ; printf ( "\ nOriginal address in x = %u", x ) ; printf ( "\ nOriginal address in y = %u", y ) ; printf ( "\ nOriginal address in z = %u", z ) ; //p=&I //*p== i x++ ; y++ ; z++ ; printf ( "\ nNew address in x = %u", x ) ; printf ( "\ nNew address in y = %u", y ) ; printf ( "\ nNew address in z = %u", z ) ; }

Character pointer #include < stdio.h > int main() { char *cp; cp="a string"; while(*cp!=0) { putchar (*cp); cp++; } return 0; }

Pointer expression A few special pointer expressions are: Pointer assignment - pointer assign its value to another pointer Pointer conversion - assign a pointer of one type to a pointer of another type type *pointer variable = ( type *) &variable; Pointer arithmetic - in this include addition, deletion, increment, decrement using pointer

assignment main() { int var=20; int *p1,*p2; p1=&var; p2=p1; printf (“p1,p2 are:% d%d ”, *p1,*p2); printf (“p1,p2 are:%d%d”,p1p2); return 0; } conversion int main() { float d = 2,*p; p = &d; int *p1; p1 = (int *)&d; printf ("p1 = %d, p = %d, *p = %f \n", p1, p, *p); printf ("*p1=%f",(float) *p1); return 0; } arithmatic main() { Int v=20; Int *p1; P1=&v; Printf (“v is %d \n”, v); (*p1)++; //same as increment Printf (“p1 is %d \n”, *p1); Return 0; }

A rithmetic operations on the pointer Increment Decrement Addition Subtraction Comparison

addition int  main(){   int  number=50;         int  *p; //pointer to int        p=&number; //stores the address of number variable          printf ( "Address of p variable is %u \ n" ,p );         p=p+3;    //adding 3 to pointer variable      printf ( "After adding 3: Address of p variable is %u \ n" ,p );        return  0;   }    

increment int  main(){   int  number=50;         int  *p; //pointer to int        p=&number; //stores the address of number variable          printf ( "Address of p variable is %u \ n" ,p );         p=p+1;         printf ( "After increment: Address of p variable is %u \ n" ,p );  // in our case, p will get incremented by 4 bytes.        return  0;   }    

array of pointer “ Array of pointers ” is an array of the  pointer variables . It is also known as pointer arrays. Syntax int * var_name [ array_size ]; Example: int * ptr [3];   ptr [ i ] = & arr [ i ]; Note: We can make separate pointer variables which can point to the different values or we can make one integer array of pointers that can point to all the values. Example: int i ,* ptr [3];

void main() {         // creating an array      int arr [3] = { 1, 2, 3 };         // we can make an integer pointer array to      // storing the address of array elements      int i , * ptr [3];         for ( i = 0; i < 3; i ++) {             // assigning the address of integer.          ptr [ i ] = & arr [ i ];      }         // printing values using pointer      for ( i = 0; i < 3; i ++) {             printf ( "Value of arr [%d] = %d \n" , i , * ptr [ i ]);      } }

Algorithm to print array using pointer Step1:  start Step2:  Read array a[] with n elements Step 3:  initialize pointer p=&a[0] [or p=a] Step 4:  if i <n go to next step otherwise go to step 7 Step 5:  print *( p+i ) Step 6:   i =i+1 go to step 4 Step 7:  stop

Print array using pointer #include<stdio.h> main () { int a [ 5 ] = { 5 , 4 , 6 , 8 , 9 } ; int *p=&a [ ] ; int i ; for ( i = ;i < 5 ;i++ ) printf ( "%d " ,* ( p+i )) ; }

Traversing an array by using pointer #include<stdio.h>    void  main ()   {        int   arr [5] = {1, 2, 3, 4, 5};   i nt *p; *p= arr ;   // int  *p =  arr ;         int   i ;        printf ( "printing array elements...\n" );        for ( i  = 0;  i < 5;  i ++)       {            printf ( "%d  " ,*( p+i ));       }   }  

Pointer to array Pointer is also known as array pointer i nt a[3]={2,5,6}; i nt * ptr =a; // Variable p of type pointer is pointing to the  address of an integer array arr. We have a pointer ptr that focuses to the 0th component of the array. We can likewise declare a pointer that can point to whole array rather than just a single component of the array. int *p[10]; // declaration of pointer type array

Declaration of the pointer to an array: Syntax data type (*var name)[size of array]; Example //Pointer to array of five elements Int (* ptr )[5]=NULL; // parenthesis to pronounce pointer to an array // pointer to an array of five numbers int (* ptr)[5] = NULL;

int main() {         // Pointer to an array of five numbers      int (*a)[5];         int b[5] = { 1, 2, 3, 4, 5 };         int i = 0;         // Points to the whole array b         a = &b;         for ( i = 0; i < 5; i ++)             printf ( "%d \n" , *(*a + i ));         return 0; }

Pointer to pointer Pointer store the address of a variable , similarly the address of a pointer can also be stored in some other pointer. Syntax int a=5; // p1=&a; //variable declaration int *p1; //pointer to an integer int **p2; //p2=&p1 //pointer to pointer to an integer *(*p2) int ***p3; //pointer to pointer to pointer to an integer *(*(*p3))

Example: pointer to pointer //program to print value using pointer to pointer main() { int a=5; int *p1; int **p2; int ***p3; p1=&a; p2=&p1; p3=&p2; printf (“% d”,a ); //output=5 printf (“%d”,*p1); //output=5 printf (“%d”,**p2); //output=5 printf (“%d”,***p3); //output=5 printf (“%d”,*p2); //address of p1 will be printed }

Pointer to a function Function passing the array as a parameter using call by value or call by reference mechanism . Example: void  show ( int );   // function declaration void (*p)( int )=&display; // Pointer p is pointing to the address of a function

WAP to swap two numbers Call by value void swap( int,int ) // function prototype or declaraion main() { int a,b ; printf (“enter two numbers”); scanf (“% d%d ”,& a,&b ); swap( a,b ); //function call printf (“% d%d ”, a,b ); } void swap(int x,int y) //function definition { int x,y,t ; t=x; x=y; y=t; } Call by reference void swap(int*,int*) // function prototype or declaraion main() { int a,b ; printf (“enter two numbers”); scanf (“% d%d ”,& a,&b ); swap(& a,&b ); //function call printf (“% d%d ”, a,b ); } void swap(int *p1,int *p2) //function definition { int t; t=*p1; *p1=*p2; *p2=t; }

Pointer to structure Pointer to structure holds the add of the entire structure. It is used to create complex data structures such as linked lists, trees, graphs and so on. The members of the structure can be accessed using a special operator called as an arrow operator ( -> ) Declaration struct tagname * ptr ; example − struct student *s Accessing ptr -> membername ;

struct   st  {        int   i ;        float  f;   }ref;   struct   st  *p = &ref;

example

questions What is pointer? What is pointer variable? what is its purpose? How is pointer variable accessed? What are the operators exclusively used with pointer? How to print the address of variable ussing pointer ? explain with example.