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:
(a)Reserve space in memory to hold the integer value.
(b)Associate the name i with this memory location.
(c)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.
1. int n = 10;
2. int* p = &n; // Variable p of type pointer is pointing to the address
 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
1. #include<stdio.h>
2. int main(){
3. int number=50;
4. printf("value of number is %d, address of number is %u",number,&number);
5. return 0;
6. }
1. #include<stdio.h>
2. int main(){
3. int number=50; // *= point the value of variable
4. int *p; // declaration of pointer variable
5. p=&number; //(65524) //stores the address of number variable
6. printf("Address of p variable is %x n",p);
7. printf("Value of p variable is %d n",*p);
8. return 0;
9. }
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;
}
Arithmetic operations on the pointer
• Increment
• Decrement
• Addition
• Subtraction
• Comparison
addition
1. int main(){
2. int number=50;
3. int *p;//pointer to int
4. p=&number;//stores the address of number variable
5. printf("Address of p variable is %u n",p);
6. p=p+3; //adding 3 to pointer variable
7. printf("After adding 3: Address of p variable is %u n",p);
8. return 0;
9. }
increment
1. int main(){
2. int number=50;
3. int *p;//pointer to int
4. p=&number;//stores the address of number variable
5. printf("Address of p variable is %u n",p);
6. p=p+1;
7. printf("After increment: Address of p variable is %u 
n",p); // in our case, p will get incremented by 4 bytes.
8. return 0;
9. }
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[0];
 int i;
 for(i=0;i<5;i++)
 printf("%d ",*(p+i));
 }
Traversing an array by using pointer
1. #include<stdio.h>
2. void main ()
3. {
4. int arr[5] = {1, 2, 3, 4, 5};
5. int *p;
6. *p=arr; //int *p = arr;
7. int i;
8. printf("printing array elements...n");
9. for(i = 0; i< 5; i++)
10. {
11. printf("%d ",*(p+i));
12. }
13. }
Pointer to array
Pointer is also known as array pointer
int a[3]={2,5,6};
int *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.

unit-7 Pointerdesfsdfsdgsdgaa notes.pptx

  • 1.
  • 2.
    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.
  • 3.
    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.
  • 4.
    An Introduction toPointers Consider the declaration int i = 3 ; This declaration tells the C compiler to: (a)Reserve space in memory to hold the integer value. (b)Associate the name i with this memory location. (c)Store the value 3 at this location.
  • 5.
     We mayrepresent i‘s location in memory
  • 6.
    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. 1. int n = 10; 2. int* p = &n; // Variable p of type pointer is pointing to the address
  • 7.
     The sizeof the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.
  • 8.
    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
  • 9.
     Pointer Example*p= 50; p=fff4; &p=aaa3
  • 10.
    Address Of (&)Operator 1. #include<stdio.h> 2. int main(){ 3. int number=50; 4. printf("value of number is %d, address of number is %u",number,&number); 5. return 0; 6. }
  • 11.
    1. #include<stdio.h> 2. intmain(){ 3. int number=50; // *= point the value of variable 4. int *p; // declaration of pointer variable 5. p=&number; //(65524) //stores the address of number variable 6. printf("Address of p variable is %x n",p); 7. printf("Value of p variable is %d n",*p); 8. return 0; 9. }
  • 12.
    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 ;
  • 13.
    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 ) ; }
  • 14.
    Character pointer #include <stdio.h> intmain() { char *cp; cp="a string"; while(*cp!=0) { putchar(*cp); cp++; } return 0; }
  • 15.
    Pointer expression A fewspecial 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
  • 16.
    assignment main() { int var=20; int *p1,*p2; p1=&var; p2=p1; printf(“p1,p2are:%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; }
  • 17.
    Arithmetic operations onthe pointer • Increment • Decrement • Addition • Subtraction • Comparison
  • 18.
    addition 1. int main(){ 2.int number=50; 3. int *p;//pointer to int 4. p=&number;//stores the address of number variable 5. printf("Address of p variable is %u n",p); 6. p=p+3; //adding 3 to pointer variable 7. printf("After adding 3: Address of p variable is %u n",p); 8. return 0; 9. }
  • 19.
    increment 1. int main(){ 2.int number=50; 3. int *p;//pointer to int 4. p=&number;//stores the address of number variable 5. printf("Address of p variable is %u n",p); 6. p=p+1; 7. printf("After increment: Address of p variable is %u n",p); // in our case, p will get incremented by 4 bytes. 8. return 0; 9. }
  • 20.
    array of pointer “Arrayof 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];
  • 21.
    void main() { // creatingan 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]); } }
  • 22.
    Algorithm to printarray 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
  • 23.
    Print array usingpointer  #include<stdio.h>  main()  {  int a[5]={5,4,6,8,9};  int *p=&a[0];  int i;  for(i=0;i<5;i++)  printf("%d ",*(p+i));  }
  • 24.
    Traversing an arrayby using pointer 1. #include<stdio.h> 2. void main () 3. { 4. int arr[5] = {1, 2, 3, 4, 5}; 5. int *p; 6. *p=arr; //int *p = arr; 7. int i; 8. printf("printing array elements...n"); 9. for(i = 0; i< 5; i++) 10. { 11. printf("%d ",*(p+i)); 12. } 13. }
  • 25.
    Pointer to array Pointeris also known as array pointer int a[3]={2,5,6}; int *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
  • 26.
    Declaration of thepointer 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;
  • 27.
    int main() { // Pointerto 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; }
  • 28.
    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))
  • 29.
    Example: pointer topointer //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 }
  • 30.
    Pointer to afunction 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
  • 31.
    WAP to swaptwo 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; }
  • 32.
    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;
  • 33.
    struct st { inti; float f; }ref; struct st *p = &ref;
  • 34.
  • 35.
    questions  What ispointer?  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.