Agenda Introduction Arithematic on a Pointer NULL Pointer Pointers Comparison Function Pointers Pointers and Arrays Arrays of Pointers Pointer to Pointer
WHAT IS A VARIABLE ? A variable 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 %d\n”, sizeof (short)); printf (“size of a int is %d\n”, sizeof ( int )); printf (“size of a long is %d\n”, sizeof (long)); } VARIABLE
DECLARING A VARIABLE IN C int k ; What does 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.
WHAT IS POINTER ? A pointer 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
Operators used in Pointers * & Address Dereferencing (Address of) (Value of) The indirection or dereference operator * gives the ``contents of an object pointed to by a pointer''. The unary or monadic operator & gives the “address of a variable”
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 */
POINTER VARIABLE INITIALIZATION int i ; int * p ; p = & i ; X1000 3 Int i =3; Address of ‘ i ’ Value of ‘ i ’ X1000 p i (Value of i ) Address of i ‘& i ’ ‘*p’ The value ‘3’ is saved in the memory location ‘ x1000’
POINTER EXAMPLES int * ptr ; int x=1, y=2; ptr = &x; x y ptr 1 2 100 200 100 1000 x y ptr 1 1 100 200 100 1000 y = * ptr ;
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 > < stdlib.h >
Defining NULL Value # define NULL 0 float * ptr = ( float *) ; char * ptr = ( char *) ; double * ptr = ( double *) ; 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
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
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 variable is %u \ n",p ); } Address of p variable is 3214864300 After increment: Address of p variable is 3214864304 e
Pointer Decrement 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 variable is %u \ n",p ); } Address of p variable is 3214864300 After decrement: Address of p variable is 3214864296
Pointer Addition New address= current address + (number * 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+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
Pointer Substraction new address= current address - (number * 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-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
The comparison is valid only between pointers that point to the same array . The following relational operators work for pointers operation: ==, !=, >, <, >= and <= A lower 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. 24/51 POINTERS COMPARISON
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 operator to 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 memory location. 5. Differencing You can subtract an integer from a pointer to point to a different memory location. 6. Comparison Valid only with two pointers that point to the same array. 25/51 The following table summarizes pointer operations.
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.
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
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 To assign the address of an integer variable called iIntVar to the first element of the array, we could write something like this. iArrayPtr [0] = & iIntVar ; // iIntVar to the first iArrayPtr element
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 %d\ n”,V ,*P,**Q); / * prints 101 3 times */
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 num has Address : X7730 Address of Pointer pr1 :X6611 Address of Pointer pr2 : X6698 X6698 X7730 123 pr1 pr2 num X7730 X6698 X6611
#include <stdio.h> int main( ) { int num = 123; int *pr2; // Pointer for num int ** pr1; //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); }