Pointer Arithmetics and DMA Unit 2 Part 2
Pointer Arithmetics and DMA Unit 2 Part 2
com/)
POINTER ARITHMETICS
Basic Concept
To conduct arithmetical operations on pointers is a little different that to conduct them on regular
integer data types.
To begin with, only addition and subtraction operations are allowed to be conducted with them,
the other makes no sense in the world of pointers. Both addition and subtraction have a different
behavior with pointers according the size of data type to which they point.
char *ptrch;
int *ptrint;
Assume:
ptrch
2000 2002
ptrint
When we write:
ptrch++; ptrint++;
Explanation: ptrch++; means ptrch = ptrch + 1;
ptrch pointer points to memory location 1001 here ptrch is a char pointer and pointing to address 1000.
The storage capacity of char is 1 byte therefore when the statement ptrch++
1000 1001 executes, it increase the address pointing by the ptrch using given formula:
ptrch = (address contained in ptrch) + 1 x (size of ptrch data type i.e. char)
ptrch = 1000 + 1 x (1)
ptrch = 1001 // new address in ptrch
ptrch
ptrint pointer points to memory location 2002 Explanation: ptrint++; means ptrint = ptrint + 1;
here ptrch is a int pointer and pointing to address 2000.
The storage capacity of int is 2 byte therefore when the statement ptrint++
2000 2002 executes, it increase the address pointing by the ptrch using given formula:
ptrint = (address contained in ptrint) + 1 x (size of ptrint data type i.e. int)
ptrint = 2000 + 1 x (2)
ptrint = 2002 // new address in ptrint
ptrint
To make your understanding better in pointer arithmetic here are few more examples:
int i = 12,
int *ip = &i;
char ch = 'a',
*cp = &ch;
Suppose the address of i and ch are 1000, 3000 respectively, therefore ip and cp are at 1000,
3000 initially.
As we know, an array is a collection of a fixed number of values. Once the size of an array is
declared, we cannot change it.
Sometimes the size of the array we declared may be insufficient. To solve this issue, we can
allocate memory manually during run-time. This is known as dynamic memory allocation in C
programming.
To allocate memory dynamically, library functions are malloc(), calloc(), realloc() and free() are
used. These functions are defined in the <stdlib.h> header file.
malloc() method
Syntax:
sizeof(..) function returns the size of data type which is passed as argument. Since the size of int
is 2 bytes, this statement will allocate 200 bytes of memory. And, the pointer ptr holds the
address of the first byte in the allocated memory.
Here i am writing a simple program with the help of dynamic memory allocation:
// accept a number and increase it by 5
//try this program and observe the result.
main(){
}
Explanation:
Step 1:- dynamically memory is allocated with memory block of 2 bytes i.e. size of int and the
address of such block is assigned to pointer ptr.
Step 2 & 3:- used to accept a number from the user and store it at the dynamic memory address
which is hold by pointer ptr.
Step 4:- *ptr = *ptr +5; right side of this expression first fetch the value from memory address
stored in pointer ptr then add the value 5 in that and again store the new value in the same
address held by ptr.
Pictorial representation: A dynamic memory block of size 2 bytes has been created
and address for this block assigned say 1000 to ptr.
Step 1:-
1000 1000
ptr
Enter a number: 55
1000 1000 55
ptr
Step 4:-
55 + 5
1000 1000
= 60
ptr
Step 5:-
*ptr = 60
I am giving two programs here; both will give the same result. You only observer the coding
style and on this basis try some simple program from your own using DMA.
#include<stdio.h> #include<stdio.h>
scanf("%d",ptr); scanf("%d",ptr);
} }
calloc() method
Syntax:
sizeof(..) function returns the size of data type which is passed as argument. Since the size of int
is 2 bytes, this statement allocates contiguous space in memory for 5 elements each with the size
of the int.
free() method
“free” method in C is used to dynamically de-allocate the memory. The memory allocated using
functions malloc() and calloc() is not de-allocated on their own. Hence the free() method is used,
whenever the dynamic memory allocation takes place. It helps to reduce wastage of memory by
freeing it.
Syntax:
free(ptr);
realloc() method
Syntax:
Dynamic size array implementation with malloc DMA (dynamic memory allocation)
#include <stdio.h>
#include <stdlib.h>
int main()
{
// This pointer will hold the
// base address of the block created
int* ptr;
int n, i;
// Get the number of elements for the array
n = 5;
printf("Enter number of elements: %d\n", n);
return 0;
}
Output:
Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,
EXAMPLE PROGRAM CODE OF FREE() FUNCTION
#include <stdio.h>
#include <stdlib.h>
int main()
{
// This pointer will hold the
// base address of the block created
int *ptr, *ptr1;
int n, i;