0% found this document useful (0 votes)
24 views15 pages

Pointers PPS

A pointer stores the address of another variable in memory. It allows a program to indirectly access and manipulate the value of another variable. Pointers contain the address of the pointed variable. Dereferencing a pointer using * accesses the value at the addressed memory location. Pointers can point to data types, arrays, other pointers, and structures. Self-referential structures use pointers to reference other objects of the same structure type, enabling data structures like linked lists.

Uploaded by

a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views15 pages

Pointers PPS

A pointer stores the address of another variable in memory. It allows a program to indirectly access and manipulate the value of another variable. Pointers contain the address of the pointed variable. Dereferencing a pointer using * accesses the value at the addressed memory location. Pointers can point to data types, arrays, other pointers, and structures. Self-referential structures use pointers to reference other objects of the same structure type, enabling data structures like linked lists.

Uploaded by

a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Pointers

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 has a name and a location, both of which are constant

pointer (ptr) is a variable that stores address of another variable, which refers to a memory
location

Pointers do not store the actual value of the variable

Example:

int i = 5;

int *ptr; /* declares a pointer ptr to an integer variable*/

ptr = &i;/*stores address of i to ptr*/

printf(“i = %d\n”, i);

printf(“*ptr = %d\n”, *ptr);

printf(“ptr = %u\n”, ptr);


* is a unary operator (also called indirection operator)

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> *<pointer-name>

pointer-type : It specifies the type of pointer. It can be int , char, float etc. The type specifies the
type of pointer variable.

pointer-name : It can be any name specified by the user.

One of the most common causes of errors in programming is uninitialized pointers.

These errors are difficult to debug because the effect of error is not found until the program
execution.

Example of an uninitialized variable and an uninitialized pointer.

A pointer variable must be assigned a valid memory address.

<pointer declaration><name-of-pointer> = <address of a variable>

int a; // int variable value unknown


int *p = &a; // p has a valid address

*p = 89; // a is assigned value 89

* (Indirection operator) is used to access variable through pointer.

Indirection operator is a Unary operator whose operand must be a pointer value.

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++1 a=a+1 *1=*1+1 *p++

A pointer that does not point to any variable, it contains NULL.

A few advantages:

- Pointers allow you to implement sharing without copying

- 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.

- Pointers allow us to use dynamic memory allocation.

Arithmetic operations can be performed on pointers

Increment/decrement pointer (++ or --)

Add an integer to a pointer( + or += , - or -=)

Pointers may be subtracted from each other


Operations meaningless unless performed on an array

Incrementing Pointer:Incrementing Pointer is generally used in array because we have


contiguous memory in array and we know the contents of next memory location.

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 + 1 = Address Address++ = Address

++Address = Address

Pointers to Pointers and generic pointers

Pointers to Pointers are necessary for manipulating advanced data structures

Pointers that point to other pointers are called Pointers to Pointers

Example: a pointer pointing to an integer pointer.

Above example demonstrates two levels of redirection

Many applications may require two or more levels of redirection

Each level of redirection requires a separate indirection operator.


Suppose a pointer ‘p1′ points to another pointer ‘p2′ that points to a character ‘c’. In memory,
the three variables can be visualized as :

In memory, pointer p1 holds the address of pointer p2. Pointer p2 holds the address of character
‘c’.

Pointer p1 is declared as *p1

Pointer to Pointer is declared as **p2

Pointer to an array

Array name can be used as pointer variable

The base address of array is assigned to the pointer variable

Base address of an array specifies the address of the first element in the array

By using pointer arithmetic, other elements of the array can be accessed.

Example:

Int *ptr;

Inta[4]={1,3,4};

ptr=&a[0] //assigning the base address of an array to a pointer

or

ptr=a; //assigning the base address of an array to a pointer

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.

ptr is a pointer and “a” is base address of array a[5]

ptr+1 points to the second element of an array i.e a[1]


Example

#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;

// Pointer to an array of 5 integers

int(*ptr)[5];

intarr[5];

// Points to 0th element of the arr.

p = arr;

// Points to the whole array arr.

ptr = &arr;

printf("p = %p, ptr = %p\n", p, ptr);

p++;

ptr++;

printf("p = %p, ptr = %p\n", p, ptr);

return0;

Pointers to structures:

Structure like other data types can also be accessed through pointers.

One of the most common methods used to reference structures.


Example: Pointers to structures

Indirect selection operator

Operator-Indirect-Selection eliminates the problems with pointers to structured.

Example: Indirect Selection Operator

(*pointerName).fieldName «pointerName->fieldName.

Pointers in self-referential structures

A structure which references to itself is known as self referential structure

Self referential structure mainly used in implementation of data structures


Example : to create a node in single linked list

struct node

int data;

struct node* link;

In the above example node is pointing to node again until link assigned with

null

Example : to create a node in double linked list

struct node

struct node *llink

int data;

struct node* rlink;

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;

struct node* link;

};

int main()

struct node ob;


return 0;

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.

Types of Self Referential Structures

Self Referential Structure with Single Link

Self Referential Structure with Multiple Links

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;

struct node* link;

};

int main()
{

struct node ob1; // Node1

// Initialization

ob1.link = NULL;

ob1.data1 = 10;

ob1.data2 = 20;

struct node ob2; // Node2

// Initialization

ob2.link = NULL;

ob2.data1 = 30;

ob2.data2 = 40;

// Linking ob1 and ob2

ob1.link = &ob2;

// Accessing data members of ob2 using ob1

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;

struct node* prev_link;

struct node* next_link;

};

int main()

struct node ob1; // Node1

// Initialization

ob1.prev_link = NULL;

ob1.next_link = NULL;
ob1.data = 10;

struct node ob2; // Node2

// Initialization

ob2.prev_link = NULL;

ob2.next_link = NULL;

ob2.data = 20;

struct node ob3; // Node3

// 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;

// Accessing data of ob1, ob2 and ob3 by ob1


printf("%d\t", ob1.data);

printf("%d\t", ob1.next_link->data);

printf("%d\n", ob1.next_link->next_link->data);

// Accessing data of ob1, ob2 and ob3 by ob2

printf("%d\t", ob2.prev_link->data);

printf("%d\t", ob2.data);

printf("%d\n", ob2.next_link->data);

// Accessing data of ob1, ob2 and ob3 by ob3

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.

You might also like