1
Prepared By:
Dr. Chandan Kumar
Assistant Professor, Computer Science & Engineering Department
Invertis University, Bareilly
 A derived data type
 A pointer is used when we want to know where the
data is stored in computer memory
 Pointer variables are the special variables which are
used to store the address of other variables i.e. direct
address of the memory location
 Pointer variable contains memory address as their
values
2
For example-
int a=15; a
3
15
110
Memory Representation
Variable_name
Variable_value
Variable_address/
Memory_location
Note: Here we showed memory representation of variable initialization i.e. int
a=15. Now from the picture, it is clear that we can access the value of a variable
with their name as well as their address also. So, if we use the address to access
variables than we use a pointer.
 The computer memory is viewed as an array of consecutive
memory locations
 Each memory location is given a unique identification
number called address
 Finally, A pointer variable is a variable that can hold the
address of the other variable (same type)
 For example- if we want to store the address of integer
variable than the pointer variable must be of type int.
 Two pointer operators are used such as - & (address-of) and
* (De-referencing) operator
4
 A pointer variable must be declared like other variables
before using them in a program
◦ Syntax to declare variables
 data_type variable_names;
 For example- int a,b;
◦ Syntax to declare pointer variable
 data_type *variable_names;
 For example- int *a, *b; or int* a,b;
5
 To assign value of a variable
◦ Syntax
 variable_name=value;
 For example- a=20;
 To assign address of a pointer variable
◦ Syntax
 pointer_variable_name=&variable_name;
 For example- ptr=&a;
 To get value from a pointer variable
 Apply * to the pointer variable i.e. *ptr
6
For example-
a
int a=20;
int *ptr;
ptr=&a; // i.e. address of a assign to ptr
If we print the value of ptr than we get the address of variable
‘a’.
printf(“%d”,ptr); // output will be 100
If we want to print the value of the variable whose address
stored by the pointer variable then we apply * to the pointer
variable.
printf (“%d”,*ptr); // output will be 20
7
20
100
100
ptr
 Like other variables, pointer variables are also
initialized
◦ Syntax to initialized other variables
 data_types variable_names=values;
 int a=5;
 int x[5]={10,20,30,40,50};
◦ Syntax to initialized pointer variable
 Data_type *variable_name=&variable_name;
 int *ptr=&a;
8
 WAP to print address and value of a variable using pointer variable
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
int *ptr;
clrscr();
printf("Enter a numbern");
scanf("%d",&x);
ptr=&x;
printf("The address of x variable is %dn",ptr);
printf("The value of x variable is %d",*ptr);
getch();
}
Output:-
9
Hear we are going to discuss basic operation performed
by the pointers
 Assignments
 Arithmetic operations
◦ Addition
◦ Subtraction
 Comparison
10
 A pointer variable cab be assign the address of the other
variable. For example
int a,*ptr;
ptr=&a;
 If two pointer variables are pointed to the object of
same type than they can be assign. For example
int a, *ptr1, *ptr2;
ptr1=&a;
ptr2=ptr1;
11
 A pointer variable can be assign a null value. For
example
int *ptr;
ptr=‘0’;
Note- A pointer that is not assigned any value. If we don't
have any address to be specified in the pointer at the
time of declaration then we can assign NULL value.
int *ptr=NULL;
In most libraries, the value of the pointer is 0 (zero).
12
 We can perform arithmetic operations on pointers.
Following are the possible arithmetic operations
◦ Addition
◦ Subtraction
13
 When we increase a pointer by 1 the pointer begins
pointing to the next position immediately.
 The pointer value would be raised by the size of the
type of data to which the pointer is pointing.
 By using the addition operation on a pointer that keeps
pointing to every element of the array, performing
some operation on it, and updating itself in a loop, we
can traverse an array.
14
For example
int a[5]={10,11,12,13,14};
int *ptr;
Ptr=&a or &a[0];
ptr++;
or
ptr=ptr+1 Base Address
Gives address of the next element in a list
i.e. here the new address will be 102
New_address= current_address + n*size_of(data_type)
Where n is the number by which the pointer get incresed
15
Memory Representation
ptr=ptr+3
= &a + 3* size_of(int)
= 100+3*2
= 106
It denotes address of a[3] element in the array.
16
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5]={10,11,12,13,14};
int *ptr;//pointer to int
clrscr();
ptr=&a[0];//stores the address of n variable
printf("Address of ptr variable is %u n",ptr);
ptr=ptr+1;
printf("After increment by 1: Address of ptr variable is %u n",ptr);
ptr=ptr+3;
printf("After increment by 3: Address of ptr variable is %u n",ptr);
getch();
return 0;
}
17
Output:
Here, the base address allocated by compiler is 65516.
Note: First address of the array is called base address of
the array.
18
 We may deduct a value from the pointer variable, like
pointer addition.
 An address will be given by subtracting any number
from a pointer.
 The value subtraction formula from the pointer variable
is given below:
new_address= current_address - (number*size_of(data_type))
19
#include<stdio.h>
#include<conio.h>
void main()
{
int num=50;
int *ptr;//pointer to int
clrscr();
ptr=&num;//stores the address of number variable
printf("Address of ptr variable is %u n",ptr);
ptr=ptr-2; //subtracting 3 from pointer variable
printf("After subtracting 2: Address of ptr variable is %u n",ptr);
getch();
}
20
 Address + Address
 Address * Address
 Address % Address
 Address / Address
 Address & Address
 Address ^ Address
 Address | Address
 ~Address
21
 Pointers can be compared by the use of relational
operators like = =, <, and >. If two pointer variables are
pointing to the object of the same data type then they
can be compared with one another.
 For example,
int *ptr1,*ptr2;
int x=10;
ptr1=&x;
Then we can use relational operators
if (ptr1>ptr2) and so on
22
 The pointer reduces the code and enhances the
performance, it is used with arrays, structures, and
functions to retrieve strings, trees, etc.
 Using the pointer we can return multiple values from
one method.
 It lets us access any memory location in the memory of
our machine.
23
 Many pointer implementations are in c language.
 Dynamic allocation of memory
We can dynamically allocate memory in c language
using malloc) (and calloc) (functions where the pointer
is used.
 Arrays, Functions and Structures
Pointers are commonly used in arrays, functions, and
structures in c language. Reduces code and improves
performance.
24
 A normal variable stores the value whereas the pointer variable stores
the address of the variable.
 The content of the C pointer always is a whole number i.e. address.
 Always C pointer is initialized to null, i.e. int *p = NULL.
 The value of the NULL pointer is 0.
 & symbol is used to get the address of the variable.
 * symbol is used to get the value of the variable that the pointer is
pointing to.
 If a pointer in C is assigned to NULL, it means it is pointing to nothing.
 Two pointers can be subtracted to know how many elements are
available between these two pointers.
 But, Pointer addition, multiplication, division are not allowed.
 The size of any pointer is 2 byte (for 16-bit compiler).
25
26

Pointers in c

  • 1.
    1 Prepared By: Dr. ChandanKumar Assistant Professor, Computer Science & Engineering Department Invertis University, Bareilly
  • 2.
     A deriveddata type  A pointer is used when we want to know where the data is stored in computer memory  Pointer variables are the special variables which are used to store the address of other variables i.e. direct address of the memory location  Pointer variable contains memory address as their values 2
  • 3.
    For example- int a=15;a 3 15 110 Memory Representation Variable_name Variable_value Variable_address/ Memory_location Note: Here we showed memory representation of variable initialization i.e. int a=15. Now from the picture, it is clear that we can access the value of a variable with their name as well as their address also. So, if we use the address to access variables than we use a pointer.
  • 4.
     The computermemory is viewed as an array of consecutive memory locations  Each memory location is given a unique identification number called address  Finally, A pointer variable is a variable that can hold the address of the other variable (same type)  For example- if we want to store the address of integer variable than the pointer variable must be of type int.  Two pointer operators are used such as - & (address-of) and * (De-referencing) operator 4
  • 5.
     A pointervariable must be declared like other variables before using them in a program ◦ Syntax to declare variables  data_type variable_names;  For example- int a,b; ◦ Syntax to declare pointer variable  data_type *variable_names;  For example- int *a, *b; or int* a,b; 5
  • 6.
     To assignvalue of a variable ◦ Syntax  variable_name=value;  For example- a=20;  To assign address of a pointer variable ◦ Syntax  pointer_variable_name=&variable_name;  For example- ptr=&a;  To get value from a pointer variable  Apply * to the pointer variable i.e. *ptr 6
  • 7.
    For example- a int a=20; int*ptr; ptr=&a; // i.e. address of a assign to ptr If we print the value of ptr than we get the address of variable ‘a’. printf(“%d”,ptr); // output will be 100 If we want to print the value of the variable whose address stored by the pointer variable then we apply * to the pointer variable. printf (“%d”,*ptr); // output will be 20 7 20 100 100 ptr
  • 8.
     Like othervariables, pointer variables are also initialized ◦ Syntax to initialized other variables  data_types variable_names=values;  int a=5;  int x[5]={10,20,30,40,50}; ◦ Syntax to initialized pointer variable  Data_type *variable_name=&variable_name;  int *ptr=&a; 8
  • 9.
     WAP toprint address and value of a variable using pointer variable #include<stdio.h> #include<conio.h> void main() { int x; int *ptr; clrscr(); printf("Enter a numbern"); scanf("%d",&x); ptr=&x; printf("The address of x variable is %dn",ptr); printf("The value of x variable is %d",*ptr); getch(); } Output:- 9
  • 10.
    Hear we aregoing to discuss basic operation performed by the pointers  Assignments  Arithmetic operations ◦ Addition ◦ Subtraction  Comparison 10
  • 11.
     A pointervariable cab be assign the address of the other variable. For example int a,*ptr; ptr=&a;  If two pointer variables are pointed to the object of same type than they can be assign. For example int a, *ptr1, *ptr2; ptr1=&a; ptr2=ptr1; 11
  • 12.
     A pointervariable can be assign a null value. For example int *ptr; ptr=‘0’; Note- A pointer that is not assigned any value. If we don't have any address to be specified in the pointer at the time of declaration then we can assign NULL value. int *ptr=NULL; In most libraries, the value of the pointer is 0 (zero). 12
  • 13.
     We canperform arithmetic operations on pointers. Following are the possible arithmetic operations ◦ Addition ◦ Subtraction 13
  • 14.
     When weincrease a pointer by 1 the pointer begins pointing to the next position immediately.  The pointer value would be raised by the size of the type of data to which the pointer is pointing.  By using the addition operation on a pointer that keeps pointing to every element of the array, performing some operation on it, and updating itself in a loop, we can traverse an array. 14
  • 15.
    For example int a[5]={10,11,12,13,14}; int*ptr; Ptr=&a or &a[0]; ptr++; or ptr=ptr+1 Base Address Gives address of the next element in a list i.e. here the new address will be 102 New_address= current_address + n*size_of(data_type) Where n is the number by which the pointer get incresed 15 Memory Representation
  • 16.
    ptr=ptr+3 = &a +3* size_of(int) = 100+3*2 = 106 It denotes address of a[3] element in the array. 16
  • 17.
    #include<stdio.h> #include<conio.h> int main() { int a[5]={10,11,12,13,14}; int*ptr;//pointer to int clrscr(); ptr=&a[0];//stores the address of n variable printf("Address of ptr variable is %u n",ptr); ptr=ptr+1; printf("After increment by 1: Address of ptr variable is %u n",ptr); ptr=ptr+3; printf("After increment by 3: Address of ptr variable is %u n",ptr); getch(); return 0; } 17
  • 18.
    Output: Here, the baseaddress allocated by compiler is 65516. Note: First address of the array is called base address of the array. 18
  • 19.
     We maydeduct a value from the pointer variable, like pointer addition.  An address will be given by subtracting any number from a pointer.  The value subtraction formula from the pointer variable is given below: new_address= current_address - (number*size_of(data_type)) 19
  • 20.
    #include<stdio.h> #include<conio.h> void main() { int num=50; int*ptr;//pointer to int clrscr(); ptr=&num;//stores the address of number variable printf("Address of ptr variable is %u n",ptr); ptr=ptr-2; //subtracting 3 from pointer variable printf("After subtracting 2: Address of ptr variable is %u n",ptr); getch(); } 20
  • 21.
     Address +Address  Address * Address  Address % Address  Address / Address  Address & Address  Address ^ Address  Address | Address  ~Address 21
  • 22.
     Pointers canbe compared by the use of relational operators like = =, <, and >. If two pointer variables are pointing to the object of the same data type then they can be compared with one another.  For example, int *ptr1,*ptr2; int x=10; ptr1=&x; Then we can use relational operators if (ptr1>ptr2) and so on 22
  • 23.
     The pointerreduces the code and enhances the performance, it is used with arrays, structures, and functions to retrieve strings, trees, etc.  Using the pointer we can return multiple values from one method.  It lets us access any memory location in the memory of our machine. 23
  • 24.
     Many pointerimplementations are in c language.  Dynamic allocation of memory We can dynamically allocate memory in c language using malloc) (and calloc) (functions where the pointer is used.  Arrays, Functions and Structures Pointers are commonly used in arrays, functions, and structures in c language. Reduces code and improves performance. 24
  • 25.
     A normalvariable stores the value whereas the pointer variable stores the address of the variable.  The content of the C pointer always is a whole number i.e. address.  Always C pointer is initialized to null, i.e. int *p = NULL.  The value of the NULL pointer is 0.  & symbol is used to get the address of the variable.  * symbol is used to get the value of the variable that the pointer is pointing to.  If a pointer in C is assigned to NULL, it means it is pointing to nothing.  Two pointers can be subtracted to know how many elements are available between these two pointers.  But, Pointer addition, multiplication, division are not allowed.  The size of any pointer is 2 byte (for 16-bit compiler). 25
  • 26.