Lecture Seven
Lecture Seven
Pointer Arithmetic
Some arithmetic operators can be used with pointers:
- Increment and decrement operators ++, --
- Integers can be added to or subtracted from
pointers using the operators +, -, +=, and -=
Each time a pointer is incremented by 1, it points to the memory location of the next element of
its base type.
If “p” is a character pointer then “p++” will increment “p” by 1 byte.
If “p” were an integer pointer its value on “p++” would be incremented by 4 bytes.
#include<stdio.h>
int main()
{
int a=10,b=20;
swap(&a, &b);
printf("%d %d", a, b);
return 0;
}
Pointers to Structures
We can create pointers to structure variables
struct Student {int rollno; float fees;};
struct Student stu1;
struct Student *stuPtr = &stu1;
(*stuPtr).rollno= 104;
-or-
Use the form ptr->member:
stuPtr->rollno = 104;
Free store
The free store is a pool of unallocated heap memory given to a program that is used by the
program for dynamic allocation during execution.
Memory Leak
If the objects, that are allocated memory dynamically, are not deleted using delete, the memory
block remains occupied even at the end of the program. Such memory blocks are known as
orphaned memory blocks. These orphaned memory blocks when an increase in number, bring
adverse effects on the system. This situation is called a memory leak
Self-Referential Structure
The self-referential structures are structures that include an element that is a pointer to another
structure of the same type.
struct node
{
int data;
struct node* next;
};
Related links
DJGPP a complete 32-bit C/C++ development system for Intel 80386 and higher PCs.
https://www.sanfoundry.com/c-programming-examples/