Pointers PPS
Pointers PPS
A pointer is a constant or variable contains an address that can be used to access data.
Pointers are special variables that can hold the address of another variable.
pointer (ptr) is a variable that stores address of another variable, which refers to a memory
location
Example:
int i = 5;
Indirection (dereferencing of a pointer) specifies that the pointer value is to be used to reference
(address) another variable.
A pointer is declared as :
pointer-type : It specifies the type of pointer. It can be int , char, float etc. The type specifies the
type of pointer variable.
These errors are difficult to debug because the effect of error is not found until the program
execution.
The result of above is an expression that can be used to access the pointed variable for the
purpose of inspection or alternation.
To access the variable ‘’a” through the pointer p, p is declared as *P and p is initialized as p=&a
Example: add 1 to the variable “a” can be done with any of the following statements:
A few advantages:
- Pointers allow modifications by a function that is not the creator of the memory i.e. function A
can allocate the memory and function C can modify it, without using global, which is a no-no for
safe programming.
Incrementing Pointer Variable Depends Upon data type of the Pointer variable
Formula:(Afterincrementing )
new value = current address + i * size_of(data type) Three Rules should be used to increment
pointer –
++Address = Address
In memory, pointer p1 holds the address of pointer p2. Pointer p2 holds the address of character
‘c’.
Pointer to an array
Base address of an array specifies the address of the first element in the array
Example:
Int *ptr;
Inta[4]={1,3,4};
or
To access other elements in an array, pointer arithmetic with restricted set of pointer operators
can be used.
First, pointer is assigned to the base address of an array using its name.
#include<stdio.h>
intmain()
intarr[5] = { 1, 2, 3, 4, 5 };
int*ptr = arr;
printf("%p\n", ptr);
return0;
We can also declare a pointer that can point to whole array instead of only one element of the
array. This pointer is useful when talking about multidimensional arrays.
Syntax:data_type (*var_name)[size_of_array];
Example:
int (*ptr)[10];
Here ptr is pointer that can point to an array of 10 integers. Since subscript have higher
precedence than indirection, it is necessary to enclose the indirection operator and pointer name
inside parentheses. Here the type of ptr is ‘pointer to an array of 10 integers’.
Note : The pointer that points to the 0th element of array and the pointer that points to the whole
array are totally different. The following program shows this:
#include<stdio.h>
intmain()
// Pointer to an integer
int*p;
int(*ptr)[5];
intarr[5];
p = arr;
ptr = &arr;
p++;
ptr++;
return0;
Pointers to structures:
Structure like other data types can also be accessed through pointers.
(*pointerName).fieldName «pointerName->fieldName.
struct node
int data;
In the above example node is pointing to node again until link assigned with
null
struct node
int data;
In the above example node is pointing to node again until link assigned with null
A self referential structure is used to create data structures like linked lists, stacks, etc. Following
is an example of this kind of structure:
structstruct_name
{
datatypedatatypename;
struct_name * pointer_name;
};
A self-referential structure is one of the data structures which refer to the pointer to (points) to
another structure of the same type. For example, a linked list is supposed to be a self-referential
data structure. The next node of a node is being pointed, which is of the same struct type. For
example,
typedefstructlistnode {
void *data;
structlistnode *next;
} linked_list;
In the above example, the listnode is a self-referential structure – because the *next is of the type
structlistnode.
Self Referential structures are those structures that have one or more pointers which point to the
same type of structure, as their member.
In other words, structures pointing to the same type of structures are self-referential in nature.
struct node {
int data1;
char data2;
};
int main()
In the above example ‘link’ is a pointer to a structure of type ‘node’. Hence, the structure ‘node’
is a self-referential structure with ‘link’ as the referencing pointer.
An important point to consider is that the pointer should be initialized properly before accessing,
as by default it contains garbage value.
Self Referential Structure with Single Link: These structures can have only one self-pointer as
their member. The following example will show us how to connect the objects of a self-
referential structure with the single link and access the corresponding data members. The
connection formed is shown in the following figure.
#include <stdio.h>
struct node {
int data1;
char data2;
};
int main()
{
// Initialization
ob1.link = NULL;
ob1.data1 = 10;
ob1.data2 = 20;
// Initialization
ob2.link = NULL;
ob2.data1 = 30;
ob2.data2 = 40;
ob1.link = &ob2;
printf("%d", ob1.link->data1);
printf("\n%d", ob1.link->data2);
return 0;
Output:
30
40
Self Referential Structure with Multiple Links: Self referential structures with multiple links can
have more than one self-pointers. Many complicated data structures can be easily constructed
using these structures. Such structures can easily connect to more than one nodes at a time. The
following example shows one such structure with more than one links.
The connections made in the above example can be understood using the following figure.
#include <stdio.h>
struct node {
int data;
};
int main()
// Initialization
ob1.prev_link = NULL;
ob1.next_link = NULL;
ob1.data = 10;
// Initialization
ob2.prev_link = NULL;
ob2.next_link = NULL;
ob2.data = 20;
// Initialization
ob3.prev_link = NULL;
ob3.next_link = NULL;
ob3.data = 30;
// Forward links
ob1.next_link = &ob2;
ob2.next_link = &ob3;
// Backward links
ob2.prev_link = &ob1;
ob3.prev_link = &ob2;
printf("%d\t", ob1.next_link->data);
printf("%d\n", ob1.next_link->next_link->data);
printf("%d\t", ob2.prev_link->data);
printf("%d\t", ob2.data);
printf("%d\n", ob2.next_link->data);
printf("%d\t", ob3.prev_link->prev_link->data);
printf("%d\t", ob3.prev_link->data);
printf("%d", ob3.data);
return 0;
Output:
10 20 30
10 20 30
10 20 30
In the above example we can see that ‘ob1’, ‘ob2’ and ‘ob3’ are three objects of the self
referential structure ‘node’. And they are connected using their links in such a way that any of
them can easily access each other’s data. This is the beauty of the self referential structures. The
connections can be manipulated according to the requirements of the programmer.