Pointers
Pointers
POINTER
• When declaring a variable, the compiler sets aside memory storage
with a unique address to store that variable.
• The compiler associates that address with the variable’s name.
• When the program uses the variable name, it automatically
accesses a proper memory location.
• No need to concern which address.
• But we can manipulate the memory address by using pointers.
DECLARATION OF A POINTER
• A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location.
• Like any variable or constant, a pointer must be declared before using it to store any variable address.
• The general form of a pointer variable declaration is −
• type *var-name;
• Here, type is the pointer's base type; it must be a valid C data type.
• var-name is the name of the pointer variable.
• The asterisk * used to declare a pointer is the same asterisk used for multiplication.
• However, in this statement the asterisk is being used to designate a variable as a pointer.
• Take a look at some of the valid pointer declarations −
• int *ip; /* pointer to an integer */
• double *dp; /* pointer to a double */
• float *fp; /* pointer to a float */
• char *ch /* pointer to a character */
• int* p1, p2; /*Here, we have declared a pointer p1 and a normal variable p2/*
When the above code is compiled and executed, it produces the following result −
Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
TYPES OF POINTERS IN C
• Null Pointer
• We can create a null pointer by assigning null value during the pointer declaration.
• This method is useful when you do not have any address assigned to the pointer.
• A null pointer always contains value 0.
• Following program illustrates the use of a null pointer:
• #include <stdio.h>
int main()
{
int *p = NULL; //null pointer
printf(“The value inside variable p is:\n%x”,p);
return 0;
}
• Output:
• The value inside variable p is: 0
• Void Pointer
• In C programming, a void pointer is also called as a generic pointer.
• It does not have any standard data type.
• A void pointer is created by using the keyword void.
• It can be used to store an address of any variable.
• Following program illustrates the use of a void pointer:
#include <stdio.h>
int main()
{
void *p = NULL; //void pointer
printf("The size of pointer is:%d\n",sizeof(p));
return 0;
}
Output:
• The size of pointer is:4
• Wild pointer
• A pointer is said to be a wild pointer if it is not being initialized to anything.
• These types of C pointers are not efficient because they may point to some unknown memory location which may cause problems in our program and it may lead to crashing of the
program.
• One should always be careful while working with wild pointers.
• Following program illustrates the use of wild pointer:
#include <stdio.h>
int main()
{ int *p; //wild pointer
printf("\n%d",*p);
return 0;
}
Output
timeout: the monitored command dumped core sh: line 1: 95298 Segmentation fault timeout 10s main
POINTER ARITHMETICS IN C
The pointer operations are summarized in the figure
Priority operation (precedence)
• When working with C pointers, we must observe the following priority rules:
• The operators * and & have the same priority as the unary operators (the negation!, the incrementation
++, decrement--).
• In the same expression, the unary operators *, &,!, ++, - are evaluated from right to left.
• If a P pointer points to an X variable, then * P can be used wherever X can be written.
• The following expressions are equivalent:
• int X =10
int *P = &Y; For this code, below expressions are true
•
Expression Equivalent Expression
Y=*P+1 Y=X+1
*P=*P+10 X=X+10
*P+=2 X+=2
++X
++*P
X++
(*P)++
• In the latter case, parentheses are needed: as the unary operators * and ++ are evaluated from right to
left, without the parentheses the pointer P would be incremented, not the object on which P points.
• Below table shows the arithmetic and basic operation that can be used when dealing with C pointers
Operation Explanation
In C programming, name of the array always points to address of the first element of an array.
• In the example, arr and &arr[0] points to the address of the first element.
• &arr[0] is equivalent to arr
• Since, the addresses of both are the same, the values of arr and &arr[0] are also the same.
• arr[0] is equivalent to *arr (value of an address of the pointer) Similarly,
• &arr[1] is equivalent to (arr + 1) AND, arr[1] is equivalent to *(arr + 1).
• &arr[2] is equivalent to (arr + 2) AND, arr[2] is equivalent to *(arr + 2).
• &arr[3] is equivalent to (arr + 3) AND, arr[3] is equivalent to *(arr + 3)...
• &arr[i] is equivalent to (arr + i) AND, arr[i] is equivalent to *(arr + i).
• In C, we can declare an array and can use pointer to alter the data of an array.
POINTERS AND 2D ARRAY
• In a two dimensional array, we can access each element by using two subscripts,
• where first subscript represents the row number and second subscript represents the column number.
• The elements of 2-D array can be accessed with the help of pointer notation also.
• Suppose arr is a 2-D array, we can access any element arr[i][j] of the array using the pointer expression *(*(arr + i) + j).
• Now we’ll see how this expression can be derived.
Let us take a two dimensional array arr[3][4]:int arr[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
• Since memory in a computer is organized linearly.
• Hence it is not possible to store the 2-D array in rows and columns.
• The concept of rows and columns is only theoretical,
• Actually, a 2-D array is stored in row-major order i.e rows are placed next to each other.
• The following figure shows how the above 2-D array will be stored in memory.