Cyber security refers to every aspect of protecting an organization and its employees and assets against cyber threats. As cyberattacks become more common and sophisticated and corporate networks grow more complex, a variety of cyber security solutions are required to mitigate corporate cyber risk.
1. Agenda
Introduction
Arithematic on a Pointer
NULLPointer
Pointers Comparison
Function Pointers
Pointers andArrays
Arrays of Pointers
Pointer to Pointer
2. WHAT IS A V
VA
AR
RI
IA
AB
BL
LE
E?
»Avariable is something with a name.
»The value of that variable is not fixed.
»Size of the variable depends on the datatype.
»The program on the right will tell you the size
of the variables in your system.
#include <stdio.h>
int main()
{
printf(“size of a short is %dn”,
sizeof(short));
printf(“size of a int is %dn”,
sizeof(int));
printf(“size of a long is %dn”,
sizeof(long));
}
3. DECLARING A VARIABLE IN C
int k ;
Whatdoes the above statement mean?
Int means the compiler needs to set aside 4 bytes of memory to
hold this particular value k.
This variable k is going to be stored in the computers RAM.
k = 2 ;
Now the value 2 is going to be placed in the memory location for k.
So if we check the place in RAM where the variable is stored, we
will find 2 there.
4. WHAT IS POINTER ?
» Apointer is a variable which contains the address (in memory)of another
variable.
» Pointers are symbolic representation of addresses.
» We can have a pointer to any variable type.
var
( Normal Variable )
ptr
( Pointer Variable )
647 1001
1001 5001
5. Operators used in Pointers
&
Address
Dereferencing
*)
of
(Value
(Address of)
The unary or monadic operator & gives the
“address of a variable”
The indirection or dereference operator
* gives the ``contents of an object
pointed to by a pointer''.
6. POINTER VARIABLE INITIALIZATION
Basic syntax: Type *Name
Examples:
int *P; /* P is var that can point to an int var */
float *Q; /* Q is a float pointer */
char *R; /* R is a char pointer */
Complex example:
int *AP[5]; /* AP is an array of 5 pointers to ints */
7. POINTER VARIABLE INITIALIZATION
int i ;
int * p ;
p = & i;
X1000
Int i=3;
Value of ‘i’
X1000
p
Address of ‘i’
‘&i’
i
3
(Value of i)
Address of i
The value ‘3’ is saved in the memory location ‘x1000’
‘*p’
8. POINTER EXAMPLES
int x=1, y=2;
int *ptr;
ptr = &x; x y ptr
1 2 100
200
100 1000
y ptr
1 1 100
200
100 1000
y = * ptr; x
9. NULL POINTER
NULL Pointer is a pointer which is pointing to nothing.
NULL pointer points the base address of segment.
In case, if you don’t have address to be assigned to pointer then you can
simply use NULL
Pointer which is initialized with NULL value is considered as NULL
pointer.
NULL is macro constant defined in following header files –
<stdio.h>
<alloc.h>
<mem.h>
<stddef.h>
10. Defining NULL
Value
# define NULL 0
float *ptr = (float *)0; char
*ptr = (char *)0; double
*ptr = (double *)0; char
*ptr = '0';
int *ptr = NULL;
#include <stdio.h>
int main()
{
int *ptr = NULL;
printf("The value of
ptr is %u",ptr);
return 0;
}
OUTPUT
0
11. POINTER ARITHEMATIC
In C pointer holds address of a value, so there can be arithmetic operations
on the pointer variable. Following arithmetic operations are possible on
pointer in C language:
Increment
Decrement
Addition
Subtraction
Comparison
12. Pointer increment
new address= current address + i *
sizeof(data type)
#include <stdio.h>
void main()
{
int number=50;
int *p; //pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p)
;
p=p+1;
printf("After increment: Address of p vari
able is %u n",p);
}
Address of p variable is 3214864300 After increment:
Address of p variable is 3214864304
e
13. Pointer Decrement
Newaddress=current address -i * sizeof(datatype)
#include <stdio.h>
void main()
{
int number=50;
int *p; //pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p)
;
p=p-1;
printf("After increment: Address of p vari
able is %u n",p);
}
Address of p variable is 3214864300 After decrement:
Address of p variable is 3214864296
14. Pointer Addition
Newaddress=currentaddress + (number * sizeof(datatype))
#include <stdio.h>
void main()
{
int number=50;
int *p; //pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p)
;
p=p+3;
printf("After adding
3: Address of p variable is %u n",p);
}
Address of p variable is 3214864300 After decrement:
Address of p variable is 3214864312
15. Pointer Substraction
newaddress=current address -(number * sizeof(datatype))
#include <stdio.h>
void main()
{
int number=50;
int *p; //pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p)
;
p=p-3; //substracting 3 from pointer variable
printf("After substracting
3: Address of p variable is %u n",p);
}
Address of p variable is 3214864300 After decrement:
Address of p variable is 3214864288
16. The comparison is valid only between pointers that point to the same array.
The following relational operators work for pointers operation:
==, !=, >, <, >= and <=
Alower array element that is those having a smaller subscript, always have a lower address than
the higher array elements.
Thus if pPointer1 and pPointer2 pointing to the elements of the same array, the following
comparison is TRUE,
pPointer1 < pPointer2
If pPointer1 points to an earlier member of the array than pPointer2 does.
Many arithmetic operations that can be performed with regular variables, such as multiplication
and division, do not work with pointers and will generate errors in C.
POINTERS COMPARISON
17. Operation Description
1. Assignment (=)
You can assign a value to a pointer. The value should be an address with
the address-of-operator(&) or from a pointer constant (array name)
2. Indirection (*)
The indirection operator(*) gives the value stored in the pointed to
location.
3. Address of (&)
You can use the address-of operatorto find the address of a pointer, so
you can use pointers to pointers.
4. Incrementing You can add an integer to a pointer to point to a different memorylocation.
5. Differencing
You can subtractan integer from a pointer to point to a different memory
location.
6. Comparison Valid only with two pointers that point to the same array.
The following table summarizes pointer operations.
18. FUNCTION POINTER
In C, like normal data pointers (int *, char *, etc), we can have pointers to functions.
Unlike normal pointers, a function pointer points to code, not data. Typically a function
pointer stores the start of executable code.
Unlike normal pointers, we do not allocate de-allocate memory using function pointers.
20. POINTERS AND ARRAYS
int a[10] , x ;
int *pa ;
pa = &a [0] /* pa pointer to address of a[0] */
x = * pa /* x = contains of pa (a[0] in this case) */
pa = a; instead of pa = &a[0]
a[i] can be written as *(pa + i)
a
21. ARRAYS OF POINTERS
Pointers may be arrayed like any other data type.
For example, a pointer array iArrayPtr of sized 20 is declared as,
int *iArrayPtr[20]; // assign the address of variable
Toassigntheaddressof aninteger variable called iIntVar to thefirst
element of the array, we could write somethinglike this.
iArrayPtr [0] = &iIntVar; // iIntVar to the first iArrayPtr element
22. POINTER TO POINTER
A pointer can also be made to point to a
pointer variable.
Example:
int V = 100;
int * P = &V; / * P points to int V */
int * * Q = &P; / * Q points to int pointer P * /
printf(“%d %d %dn”,V,*P,**Q); / * prints 101 3 times */
23. As per the figure , pr2 is a pointer for num(as pr2 is having address of variable
num),similarly pr1 is a pointer for another pointer pr1 (as pr1 is having the address
of pointer pr2).A pointer ehich points to another pointer is known as double pointer.
Variable numhasAddress : X7730
Address of Pointer pr1 :X6611
Address of Pointer pr2 : X6698
X6698 X7730 123
pr1 pr2 num
X7730
X6698
X6611
24. #include <stdio.h>
int main( )
{
int num = 123;
int *pr2;
int ** pr1;
// Pointer for num
//Double pointer for pr2
pr2 = # //Address of variable num and * storing it in pointer pr2
pr1 = &pr2; //Storing the address of pointer pr2 into pointer pr1
/* POSSIBLE WAYS TO FIND THE VALUES OF VARIABLE NUM*/
printf(“n Value of num is: %d”,num);
printf(“n Value of num using pr2 is: %d”, *pr2);
printf(“n Value of num using pr1 is: %d”, **pr1);
/* POSSIBLE WAYS TO FIND ADDRESS OF NUM */
printf(“n Address of num is: %U”,&num);
printf(“n Address of num using pr2 is: %u”, pr2);
printf(“n Address of num using pr1 is: %u”, *pr1);
/* FIND VALUE OF POINTER */
printf(“n Value of pointer pr2 is : %u”,pr2);
printf(“n Value of pointer pr2 using pr1 is : %u”,*pr1);
/* WAYS TO FIND ADDRESS OF POINTER */
Printf(“ n Address of pointer pr2 is :%u”,&pr2);
Printf(“ n Address of pointer pr1 is :%u”,&pr1);
}