C PROGRAMMING
BY MOHAMMED TAJUDDIN
C PROGRAMMING
Pointer to poiter :
poiter can hold the another poiter variable address.
Ex : **p
#include<stdio.h>
int main(){
int* ptr1,c=10;
int** ptr2 ;
ptr1=&c;
ptr2=&p;
printf(“%dn”,c);
printf(“%dn”,*ptr1);printf(“%dn”,**ptr2);}
C PROGRAMMING
There are 4 types of pointers
1)Null pointer
2)wild pointer
3)void poiter
4)Dangling pointer
Null Pointer :
You create a null pointer by assigning the null value at the time of
pointer declaration.
This method is useful when you do not assign any address to the
pointer. A null pointer always contains value 0.
C PROGRAMMING
Null Pointer :
Null means that the pointer is referring to the 0th memory location.
If we do not have any address which is to be assigned to the pointer,
then it is known as a null pointer. When a NULL value is assigned to the
pointer, then it is considered as a Null pointer.
#include <stdio.h>
int main(){
int *ptr = NULL; //null pointer
printf("The value inside variable ptr is:n%d",ptr);
return 0;
C PROGRAMMING
Wild Pointer :
Wild pointers are also called uninitialized pointers. Because they point
to some arbitrary memory location and may cause a program to crash
or behave badly.
This type of C pointer is not efficient. Because they may point to some
unknown memory location which may cause problems in our program.
This may lead to the crashing of the program.
It is advised to be cautious while working with wild pointers.
C PROGRAMMING
#include <stdio.h>
int main(){
int *p; //wild pointer
printf("n%d",*p);
return 0;
C POGRAMMING
Dangling Pointer:
A pointer that points to a memory location that has been deleted is called a
dangling pointer.
Example 1:
Deallocation of memory:
#include<stdio.h>
#include<stdlib.h>
int main(){
int *ptr = (int *)malloc(sizeof(int));
free(ptr);
ptr = NULL;
}
C PROGRAMMING
Pointer arithmatic :
Following arithmetic operations are possible on the pointer in C
language:
Increment
Decrement
Addition
Subtraction
Comparison
C PROGRAMMING
#include<stdio.h>
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;
}
C PROGRAMMING
Decrement :
Let's see the example of decrementing pointer variable on 64-bit OS.
#include <stdio.h>
void 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 decrement: Address of p variable is %u n",p); // P will now point
to the immidiate previous location.
}
C PROGRAMMING
Pointer Addition :
#include<stdio.h>
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;}
C PROGRAMMING
Poiter Subtraction :
#include<stdio.h>
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; //subtracting 3 from pointer variable
printf("After subtracting 3: Address of p variable is %u n",p);
return 0; }

POINTERS.pptx

  • 1.
  • 2.
    C PROGRAMMING Pointer topoiter : poiter can hold the another poiter variable address. Ex : **p #include<stdio.h> int main(){ int* ptr1,c=10; int** ptr2 ; ptr1=&c; ptr2=&p; printf(“%dn”,c); printf(“%dn”,*ptr1);printf(“%dn”,**ptr2);}
  • 3.
    C PROGRAMMING There are4 types of pointers 1)Null pointer 2)wild pointer 3)void poiter 4)Dangling pointer Null Pointer : You create a null pointer by assigning the null value at the time of pointer declaration. This method is useful when you do not assign any address to the pointer. A null pointer always contains value 0.
  • 4.
    C PROGRAMMING Null Pointer: Null means that the pointer is referring to the 0th memory location. If we do not have any address which is to be assigned to the pointer, then it is known as a null pointer. When a NULL value is assigned to the pointer, then it is considered as a Null pointer. #include <stdio.h> int main(){ int *ptr = NULL; //null pointer printf("The value inside variable ptr is:n%d",ptr); return 0;
  • 5.
    C PROGRAMMING Wild Pointer: Wild pointers are also called uninitialized pointers. Because they point to some arbitrary memory location and may cause a program to crash or behave badly. This type of C pointer is not efficient. Because they may point to some unknown memory location which may cause problems in our program. This may lead to the crashing of the program. It is advised to be cautious while working with wild pointers.
  • 6.
    C PROGRAMMING #include <stdio.h> intmain(){ int *p; //wild pointer printf("n%d",*p); return 0;
  • 7.
    C POGRAMMING Dangling Pointer: Apointer that points to a memory location that has been deleted is called a dangling pointer. Example 1: Deallocation of memory: #include<stdio.h> #include<stdlib.h> int main(){ int *ptr = (int *)malloc(sizeof(int)); free(ptr); ptr = NULL; }
  • 8.
    C PROGRAMMING Pointer arithmatic: Following arithmetic operations are possible on the pointer in C language: Increment Decrement Addition Subtraction Comparison
  • 9.
    C PROGRAMMING #include<stdio.h> int main(){ intnumber=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; }
  • 10.
    C PROGRAMMING Decrement : Let'ssee the example of decrementing pointer variable on 64-bit OS. #include <stdio.h> void 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 decrement: Address of p variable is %u n",p); // P will now point to the immidiate previous location. }
  • 11.
    C PROGRAMMING Pointer Addition: #include<stdio.h> 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;}
  • 12.
    C PROGRAMMING Poiter Subtraction: #include<stdio.h> 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; //subtracting 3 from pointer variable printf("After subtracting 3: Address of p variable is %u n",p); return 0; }