This power point presentation explains briefly about the possible arithmetic operations in pointers. It also explains implicit and explicit typecasting of pointers
2. Type Casting:
Converting the data type of one variable into another data type.
Syntax:
(data type to be converted to) variable;
Eg:
float a = 2.3;
int b;
b=(int)a;
Types:
Implicit Conversion:
int float double (small no of bytes higher no of bytes)
Explicit Conversion:
double float int (higher no of bytes small no of bytes)
3. Converting pointer from one type to another:
Pointer of one type cannot be implicitly converted from
one type to another but can be explicitly converted using
type casting.
In such a conversion a pointer always assumes that it is
point to a object of its type but reality may differ.
Syntax:
(data type to be converted to *) variable;
int *p;
(char *)p;
Normal variable cannot be converted to pointer variable
using type casting. Pointer variable of one type can be type
casted to another type.
4. Notes:
Type conversion is a powerful feature but yet it may
difficult to remove bugs and crashes and should be used
with uttermost vigilance.
It may also lead to unexpected and unreliable results but
program would compile successfully.
While typecasting one pointer to another because even
after type casting the pointer can point to anything but it
will still think it is pointing to something of it declared type
and have properties of the original type.
void pointer can be used for this purpose. As the void
pointer doesn’t point to any type of data, it can be type
casted to any type, and results in no error.
5. Example program to convert the pointer from pointing to int
to char data type.
main()
{
int i = 10;
char *p1;
int *p2;
p2 = &i;
p1 = (char *) p2; // Type Casting and Pointer Conversion
printf (" *p1 = %c And *p2 = %d", *p1,*p2); // Output will be depending upon
the compiler.
}
6. Example program to convert the pointer from pointing to
void to char data type.
main()
{
int i = 10;
int *p1;
void *p2;
p2 = &i;
p1 = (int *) p2; // Type Casting and Pointer Conversion
printf (" *p1 = %d And *p2 = %d", *p1,*p2);
}
Output:
*p1 = 10 And *p2 = 10
7. int a=13;
2 bytes as a whole form the
65345 65346 integer
float q=2.3;
float *s;
s=&q;
*s will give the value 2.3
4 bytes form the
23456 25457 25458 25459 float value
Pointer variable ‘s’ will be assigned with the address of q. The
address of q assigned will be 23456. As the given type is float,
*s will take the value as a whole from four bytes.
8 bits 8 bits
8 bits 8 bits 8 bits 8 bits
8. Notes:
When the address of a variable is assigned to a pointer
variable, only the starting byte’s address will be stored.
According to the data type the no of bytes from the starting
bytes will be taken account.
If the data type is not proper, pointer variable will not be
knowing till which byte the data is stored.
The arithmetic operations performed on the pointer variable,
it must be applied such that it is able to retrieve the whole
value whether it is an integer or float or char etc.,
9. When a pointer of certain base type is increased, it increases
its value in such a way that it points to next element of its
base type.
If a pointer of certain base type is decremented, its value
decreases in such a way that it points to previous value of its
base type.
Increment as well as decrement in fixed quanta of size of the
base type.
Pointer value can be incremented or decremented only by
integer as it is holding the address.
Two forms of pointer arithmetic:
Pointer + integer
Pointer – pointer (Will dealt later while dealing with array and
pointers
10. Unary Pointer Arithmetic Operators:
Pointer variable ++;
Adds sizeof(datatype) number of bytes to pointer, so that it
points to the next entry of the datatype.
The no of bytes is determined by the data type of the value
the pointer variable is pointing to.
Pointer variable --;
Subtracts sizeof(datatype) number of bytes to pointer, so that
it points to the next entry of the datatype.
11. Example of incrementing the float type pointer.
23456 23457 23458 23459 23460 23461 23462
-------
4 bytes form the float value Incrementing the pointer will
make the pointer to point to starting of next 4 bytes
main()
{
float a = 2.3, *b=&a;
printf(“Address of á’ stored in b:”, b);
printf(“Value stored in a:”, *b);
printf(“Incrementing the pointer variable:
”, ++b);
printf(“Value retrieved after
incrementing:”, *b);
}
Output:
Address of á’ stored in b: 23456
Value stored in a: 2.3
Incrementing the pointer variable: 23460
Value retrieved after incrementing:
// Not known. Will be retrieved at the
execution time.
8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits
12. main()
{
int *ptrn;
int a;
float *ptrflt;
float b;
ptrn=&a;
ptrflt=&b;
ptrn++; //increments by sizeof(int) (2 bytes)
ptrflt--; //increments by sizeof(long) (4 bytes)
}
13. Arithmetic Operations between a pointer and an integer:
Pointer + integer
Pointer Variable + n is valid, if n is an integer.
The result is the following:
Pointer Variable + (n*sizeof(data type of the value pointer points
to))
It advances the pointer by n number of size of data type.
Pointer Variable - n is similar.
The result will be:
Pointer Variable - (n*sizeof(data type of the value pointer points
to))
It decrements the pointer by n number of size of data type.
14. Eg:
float h=4.6, *ptrf;
ptrf=&h;
If h is stored at address 23454, then ptrf will be having the address
value 23454 stored in it.
Pointer Increment Value Incremented Pointing address Value retrieved
ptrf - 23454 4.6
ptrf+1
// ptrf++
ptrf+(1*sizeof(float)
)
23458 Points to the next four
bytes starting from 23458
ptrf+2 ptrf+(2*sizeof(float)
)
23462 Points to the next four
bytes starting from 23462
ptrf+3 Ptrf+(3*sizeof(float)
)
23466 Points to the next four
bytes starting from 23466
15. Notes:
Arithmetic operations addition (or) subtraction of an integer value
can be done on a pointer.
Multiplication, Division, Modulus by an integer cannot be done on
a pointer. This will give the error Illegal use of a pointer in function
main.
Addition of two pointers cannot be done. This will give the error
Invalid pointer addition.
16. Pointer variable can be compared only with another pointer.
Comparison can be done using <, >, ==, <= and >= operators.
When two pointers are compared, actually the address they are
holding is compared.
Using ‘==’ operator on pointers, will check whether both pointers
being compared are pointing to the same address or not
If pointer a and pointer b are holding the same address, then
a==b will be true. Otherwise false.
The use arithmetic operations and the comparisons will be having
wide space when the pointer is applied to array.
A pointer can be checked whether it is pointing to NULL using the
‘==’ (Equal to) operator
int *p=NULL;
if(p==0 ) will return true.
17. Valid Comparisons:
Comparing same type pointers.
Comparing pointers pointing to same location.
Comparison can be done to check for NULL pointer
Invalid Comparisons:
Comparing pointer and a normal variable. // Compiler Error
Comparing different type of pointers. Such as comparing char
pointer with int or some other type of pointer. // Warning