Docsity

cryptography foundations, Essays (university) of Cryptography and System Security

Partial preview of the text

Download cryptography foundations and more Essays (university) Cryptography and System Security in PDF only on Docsity!

POINTERS

Sk.RaziyaSultana Ass.Professor CSE

OBJECTIVES:

 (^) What is Pointer?  (^) what does "Address of a memory location" means?  (^) Declaring, Initializing and using a pointer variable  (^) Pointer address operators  (^) Pointer Expressions  (^) Pointer Arithmetic  (^) Pointer to a Pointer  (^) Pointers as Function Argument  (^) Pointer to Array  (^) Pointer to Strings  (^) Types Of Pointers  (^) Advantage of pointer  (^) Disadvantage of pointer

ADDRESS IN C  Whenever a variable is defined in C language, a memory location is assigned for it, in which it's value will be stored. We can easily check this memory address, using the & symbol.  If var is the name of the variable, then &var will give it's address.  (^) Let's write a small program to see memory address of any variable that we define in our program.

CONTINUE

#include<stdio.h> void main() { int var = 7; printf("Value of the variable var is: %d\n", var); printf("Memory address of the variable var is: %x\n", &var); }  (^) You must have also seen in the function scanf(), we mention &var to take user input for any variable var.  (^) scanf("%d", &var);  (^) This is used to store the user inputted value to the address of the variable var.

CONTINUE….

 Whenever a variable is declared in a program, system allocates a location i.e an address to that variable in the memory, to hold the assigned value.  The question is how we can access a variable using it's address? Since the memory addresses are also just numbers, they can also be assigned to some other variable. The variables which are used to hold memory addresses are called Pointer variables.  A pointer variable is therefore nothing but a variable which holds an address of some other variable. And the value of a pointer variable gets stored in another memory location.

CONTINUE
INITIALIZATION OF POINTER VARIABLE

Pointer Initialization is the process of assigning address of variable to a pointer variable. It contains the address of a variable of the same data type. In C language address operator & is used to determine the address of a variable. The & (immediately preceding a variable name) returns the address of the variable associated with it. Syntax: data-type *pointer-variable= &another-variable; or data-type *pointer-variable; pointer-variable= &another-variable eg: int a = 10; int *ptr; //pointer declaration ptr = &a; //pointer initialization  (^) Pointer variable always points to variables of the same datatype.

CONTINUE..

EXAMPLE:

#include <stdio.h> int main () { int var = 100 ; int * ptr; /pointer declaration/ ptr = &var; /pointer initialization/ printf(" \n Value of var= %d", *ptr); printf(" \n Address of var= %X", ptr); return 0 ; }

POINTER ADDRESS OPERATORS:

pointer Address operators: The & Operator (Address Of Operator) The "Address Of" Operator denoted by the ampersand character (&), & is a unary operator, which returns the address of a variable. After declaration of a pointer variable, we need to initialize the pointer with the valid memory address; to get the memory address of a variable Address Of" (&) Operator is used. Consider the program #include <stdio.h> int main() { int x=10; //integer variable int ptrX; //integer pointer declaration ptrX=&x; //pointer initialization with the address of x printf("Value of x: %d\n",ptrX); return 0; }

POINTER EXPRESSIONS

C supports a rich set of built-in operations like arithmetic, relational, assignment, conditional, etc. which can be performed on identifiers. Just like any other variable, these operations can be also performed on pointer variables.

ARITHMETIC OPERATORS

We can perform arithmetic operations to pointer variables using arithmetic operators. We can add an integer or subtract an integer using a pointer pointing to that integer variable. The given table shows the arithmetic operators that can be performed on pointer variables: Examples: *ptr1 + *ptr *ptr1 * *ptr *ptr1 + *ptr2 - *ptr We can also directly perform arithmetic expression using integers. Lets look at the example given below where p1 and p2 are pointers. p1+10, p2-5, p1-p2+10, p1/

EXAMPLE:

// Program showing pointer expressions // during Arithmetic Operations #include <stdio.h> int main() { // Integer variables int a = 20, b = 10; // Variables for storing arithmetic // operations solution int add, sub, div, mul, mod; // Pointer variables for variables // a and b int *ptr_a, *ptr_b; // Initialization of pointers ptr_a = &a; ptr_b = &b;

CONTINUE…

// Performing arithmetic Operations // on pointers add = *ptr_a + *ptr_b; sub = *ptr_a - *ptr_b; mul = *ptr_a * *ptr_b; div = *ptr_a / *ptr_b; mod = *ptr_a % *ptr_b; // Printing values printf("Addition = %d\n", add); printf("Subtraction = %d\n", sub); printf("Multiplication = %d\n", mul); printf("Division = %d\n", div); printf("Modulo = %d\n", mod); return 0; }  (^) Example: Incorrect: ptr_a/ptr_b; Correct: ptr_a / ptr_b; Correct: (ptr_a)/(ptr_b);