Pointers
SRIKANTH NALLURI
COMPUTER SCIENCE & ENGINEERING
IIIT SRIKAKULAM
 Pointer is a variable that stores the address of another variable.
Pointers are one of the powerful and frequently used features of C, as
they have a number of useful applications.
Variables contain the values and pointer variables contain the address
of variables that has the value.
Variable directly references the value and Pointer variable indirectly
references the value.
Referencing a value through a pointer is called Indirection.
Declaration and Initialization
 A pointer variable is declared with an asterisk before the variable
name.
The type-specifiers determine that what kind of variable the pointer
variable points to.

General Form:
data_type *pointer—name;
 C provides two operators, & and *, for pointer implementation.
& is the address operator. It is a unary operator that returns the address
of its operand.
* is the Indirection or de-referencing operator. It returns the value of the
variable to which its operand points.
 * and & are inverse of each other.
Initialization:
 Pointer variables should be initialized to 0, Null or an address.
No other constant can be initialized to a pointer variable.
Pointer variable of a particular data type can, hold only the address of the
variable of same data type.
They are used for several reasons, such as:
• Returning multiple values
• Fast accessing and Execution takes less time
• Dynamic memory allocation
• Sending function arguments by reference /call by reference
• Building data structures
• Pointing to functions
• Building special complicated and data structures
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.
Example programs:
#include <stdio.h>
int main( )
{
int a ;
int *pa ;
a = 77 ;
pa = &a ;
printf("a=%d *pa=%dn", a, *pa );
return 0;
}
#include <stdio.h>
int main( void )
{
int a ;
int *pa ;
pa = &a ;
*pa = 77 ;
printf("a=%d *pa=%dn", a, *pa );
return 0;
}
#include<stdio.h>
void main()
{
int x=5;
int *y=&x;
printf(“%dn”, x);
printf(“%dn”, &x);
printf(“%dn”, y);
printf(“%dn”, *y);
printf(“%dn”, &y);
}
Example:
void fun(int *p,int *q)
{
p=q;
*p=2;
}
int i=0,j=1;
int main()
{
fun(&i,&j)
printf(“%d %d|n”,I,j);
return 0;
}
C program to add two numbers using pointers
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to addn");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of the numbers = %dn", sum);
return 0;
}
#include <stdio.h>
int main()
{
int a=36;
int* ptr;
ptr=&a;
printf("%u %u",*&ptr,&*ptr);
return 0;
}
Pointer to Pointer (Double Pointer)
 We already know that a pointer holds the address of another variable
of same type.
When a pointer holds the address of another pointer then such type of
pointer is known as pointer-to-pointer or double pointer.
When we define a pointer to a pointer, the first pointer contains the
address of the second pointer, which points to the location that
contains the actual value as shown below.
Declaration:
 A variable that is a pointer to a pointer must be declared as such. This
is done by placing an additional asterisk in front of its name.
syntax:
datatype **var_name;
Ex: int **a;
Example program:
#include <stdio.h>
int main ()
{
int a;
int *p;
int **q;
a = 3000;
p = &a;
q = &p;
printf("Value of a = %dn",a );
printf("Value available at *p = %dn", *p );
printf("Value available at **q = %dn", **q);
return 0;
}
Void pointer:
A void pointer is nothing but a pointer variable declared using the
reserved word in C ‘void’.
ex: Ex:- void *ptr; // Now ptr is a general purpose pointer variable
 When a pointer variable is declared using keyword void – it becomes a
general purpose pointer variable. Address of any variable of any data type
(char, int, float etc.)can be assigned to a void pointer variable.
A pointer variable declared using a particular data type can not hold the
location address of variables of other data types. It is invalid and will result
in a compilation error.
Ex:- char *ptr;
int var1;
ptr=&var1; // This is invalid because ‘ptr’ is a character pointer variable.
Dereferencing a void pointer
 We use the indirection operator * to serve the purpose.
But in the case of a void pointer we need to typecast the pointer
variable to dereference it.
This is because a void pointer has no data type associated with it.
There is no way the compiler can know (or guess?) what type of
data is pointed to by the void pointer.
 So to take the data pointed to by a void pointer we typecast it with
the correct type of the data holded inside the void pointers
location.
Example program:
#include <stdio.h>
void main()
{
int a=5;
float b=23.16;
void *ptr;
ptr=&a;
printf("The value of integer variable a is= %dn",*( (int*) ptr) );
ptr=&b;
printf("The value of float variable is= %fn",*( (float*) ptr) );
}
Call by reference
 Before we discuss function call by reference, lets understand the
terminologies that we will use while explaining this:
Actual parameters: The parameters that appear in function calls.
Formal parameters: The parameters that appear in function
declarations/definitions.
Ex: int sum(int a,int b);
n=sum(10,20);
or
n=sum(a,b);
 Call by reference method copies the address of an argument into the
formal parameter.
In this method, the address is used to access the actual argument used
in the function call. It means that changes made in the parameter alter
the passing argument.
In this method, the memory allocation is the same as the actual
parameters. All the operation in the function are performed on the
value stored at the address of the actual parameter, and the modified
value will be stored at the same address.
Swapping example program:
#include <stdio.h>
void swap( int *x, int *y )
{
int temp;
temp = *x ;
*x = *y ;
*y = temp;
}
int main( )
{
int a =5, b =10 ;
printf("Before swapping:");
printf("na value is %d", a);
printf("nb value is %d", b);
swap( &a, &b ); /*calling swap function*/
printf("nAfter swapping:");
printf("na value is %d", a);
printf("nb value is %d", b);
return 0;
}
Adding 1 to the given number:
#include <stdio.h>
void addOne(int* ptr) {
(*ptr)++; // adding 1 to *ptr
}
int main()
{
int* p,i = 10;
p = &i;
addOne(p);
printf("%d", *p); // 11
return 0;
}
C program to add numbers using call by reference
#include <stdio.h>
long add(long *, long *);
int main()
{
long first, second, *p, *q, sum;
printf("Input two integers to addn");
scanf("%ld%ld", &first, &second);
sum = add(&first, &second);
printf("(%ld) + (%ld) = (%ld)n", first, second, sum);
return 0;
}
long add(long *x, long *y)
{
long sum;
sum = *x + *y;
return sum;
}
pointers.pptx

pointers.pptx

  • 1.
    Pointers SRIKANTH NALLURI COMPUTER SCIENCE& ENGINEERING IIIT SRIKAKULAM
  • 2.
     Pointer isa variable that stores the address of another variable. Pointers are one of the powerful and frequently used features of C, as they have a number of useful applications. Variables contain the values and pointer variables contain the address of variables that has the value. Variable directly references the value and Pointer variable indirectly references the value. Referencing a value through a pointer is called Indirection.
  • 3.
    Declaration and Initialization A pointer variable is declared with an asterisk before the variable name. The type-specifiers determine that what kind of variable the pointer variable points to.  General Form: data_type *pointer—name;
  • 4.
     C providestwo operators, & and *, for pointer implementation. & is the address operator. It is a unary operator that returns the address of its operand. * is the Indirection or de-referencing operator. It returns the value of the variable to which its operand points.  * and & are inverse of each other.
  • 5.
    Initialization:  Pointer variablesshould be initialized to 0, Null or an address. No other constant can be initialized to a pointer variable. Pointer variable of a particular data type can, hold only the address of the variable of same data type.
  • 6.
    They are usedfor several reasons, such as: • Returning multiple values • Fast accessing and Execution takes less time • Dynamic memory allocation • Sending function arguments by reference /call by reference • Building data structures • Pointing to functions • Building special complicated and data structures
  • 7.
    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.
  • 8.
    Example programs: #include <stdio.h> intmain( ) { int a ; int *pa ; a = 77 ; pa = &a ; printf("a=%d *pa=%dn", a, *pa ); return 0; }
  • 9.
    #include <stdio.h> int main(void ) { int a ; int *pa ; pa = &a ; *pa = 77 ; printf("a=%d *pa=%dn", a, *pa ); return 0; }
  • 10.
    #include<stdio.h> void main() { int x=5; int*y=&x; printf(“%dn”, x); printf(“%dn”, &x); printf(“%dn”, y); printf(“%dn”, *y); printf(“%dn”, &y); }
  • 11.
    Example: void fun(int *p,int*q) { p=q; *p=2; } int i=0,j=1; int main() { fun(&i,&j) printf(“%d %d|n”,I,j); return 0; }
  • 12.
    C program toadd two numbers using pointers #include <stdio.h> int main() { int first, second, *p, *q, sum; printf("Enter two integers to addn"); scanf("%d%d", &first, &second); p = &first; q = &second; sum = *p + *q; printf("Sum of the numbers = %dn", sum); return 0; }
  • 13.
    #include <stdio.h> int main() { inta=36; int* ptr; ptr=&a; printf("%u %u",*&ptr,&*ptr); return 0; }
  • 14.
    Pointer to Pointer(Double Pointer)  We already know that a pointer holds the address of another variable of same type. When a pointer holds the address of another pointer then such type of pointer is known as pointer-to-pointer or double pointer. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below.
  • 15.
    Declaration:  A variablethat is a pointer to a pointer must be declared as such. This is done by placing an additional asterisk in front of its name. syntax: datatype **var_name; Ex: int **a;
  • 16.
    Example program: #include <stdio.h> intmain () { int a; int *p; int **q; a = 3000; p = &a; q = &p; printf("Value of a = %dn",a ); printf("Value available at *p = %dn", *p ); printf("Value available at **q = %dn", **q); return 0; }
  • 17.
    Void pointer: A voidpointer is nothing but a pointer variable declared using the reserved word in C ‘void’. ex: Ex:- void *ptr; // Now ptr is a general purpose pointer variable  When a pointer variable is declared using keyword void – it becomes a general purpose pointer variable. Address of any variable of any data type (char, int, float etc.)can be assigned to a void pointer variable. A pointer variable declared using a particular data type can not hold the location address of variables of other data types. It is invalid and will result in a compilation error. Ex:- char *ptr; int var1; ptr=&var1; // This is invalid because ‘ptr’ is a character pointer variable.
  • 18.
    Dereferencing a voidpointer  We use the indirection operator * to serve the purpose. But in the case of a void pointer we need to typecast the pointer variable to dereference it. This is because a void pointer has no data type associated with it. There is no way the compiler can know (or guess?) what type of data is pointed to by the void pointer.  So to take the data pointed to by a void pointer we typecast it with the correct type of the data holded inside the void pointers location.
  • 19.
    Example program: #include <stdio.h> voidmain() { int a=5; float b=23.16; void *ptr; ptr=&a; printf("The value of integer variable a is= %dn",*( (int*) ptr) ); ptr=&b; printf("The value of float variable is= %fn",*( (float*) ptr) ); }
  • 20.
    Call by reference Before we discuss function call by reference, lets understand the terminologies that we will use while explaining this: Actual parameters: The parameters that appear in function calls. Formal parameters: The parameters that appear in function declarations/definitions. Ex: int sum(int a,int b); n=sum(10,20); or n=sum(a,b);
  • 21.
     Call byreference method copies the address of an argument into the formal parameter. In this method, the address is used to access the actual argument used in the function call. It means that changes made in the parameter alter the passing argument. In this method, the memory allocation is the same as the actual parameters. All the operation in the function are performed on the value stored at the address of the actual parameter, and the modified value will be stored at the same address.
  • 22.
    Swapping example program: #include<stdio.h> void swap( int *x, int *y ) { int temp; temp = *x ; *x = *y ; *y = temp; } int main( ) { int a =5, b =10 ; printf("Before swapping:"); printf("na value is %d", a); printf("nb value is %d", b); swap( &a, &b ); /*calling swap function*/ printf("nAfter swapping:"); printf("na value is %d", a); printf("nb value is %d", b); return 0; }
  • 23.
    Adding 1 tothe given number: #include <stdio.h> void addOne(int* ptr) { (*ptr)++; // adding 1 to *ptr } int main() { int* p,i = 10; p = &i; addOne(p); printf("%d", *p); // 11 return 0; }
  • 24.
    C program toadd numbers using call by reference #include <stdio.h> long add(long *, long *); int main() { long first, second, *p, *q, sum; printf("Input two integers to addn"); scanf("%ld%ld", &first, &second); sum = add(&first, &second); printf("(%ld) + (%ld) = (%ld)n", first, second, sum); return 0; } long add(long *x, long *y) { long sum; sum = *x + *y; return sum; }