Chapter 2
[Link] are Pointers ?
pointer is a variable that stores the memory address of another
variable.
Declaring Pointer Variable in C:
type *pointer_name;
Here, pointer_name is the name of the pointer and that should be a
valid C identifier.
The datatype of the pointer and the variable to which the pointer variable
is pointing must be the same.
[Link] are The pointer Operators.
Pointer uses two operators:
1. The address operator (&) :It gives the address of an object.
2. The indirection operator (*) or Dereference Operator : It is used to
access the value present at the memory address .
Pointer Example:
int main()
{
int x = 10;
// Store address of var variable
int * ptr = &x;
// Dereferencing ptr to access the value
printf("%d", *ptr);
return 0;
} OUTPUT: 10
[Link] Pointer Expression.
1. Pointer Assignments
Pointers assign its value to another pointer. When both pointers are the
same data type.
Example:
Notice that the addresses are displayed by using the %p in printf( )
format specifier.
2. Pointer Conversions
One Data type of pointer can be converted into another Data type of
pointer.
There are two general categories:
[Link] that involve void * pointers:
In C, it is permissible to assign a void * pointer to any other type of
pointer. It is also permissible to assign any other type of pointer to a void *
pointer. A void * pointer is called a generic pointer.
[Link] For * void:
Except for void *, all other pointer conversions must be performed by
using an explicit cast. However, the conversion of one type of pointer into
another type may create undefined behaviour.
[Link] Arithmetic:
There are only two arithmetic operations that you can use on pointers:
addition and subtraction.
To understand what occurs in pointer arithmetic, let p1 be an integer
pointer with a current value of 2000. Also, assume ints are 2 bytes long.
After the expression.
you may add or subtract integers to or from pointers.
The expression ptr = ptr + 5;
makes p1 point to the 12th element of p1's type beyond the one it
currently points to
4. Pointer Comparisons:
You can compare two pointers in a relational expression. For instance,
given two pointers p and q, the following statement is perfectly valid:
[Link] Relation Between Pointers and Array.
When an array in C language is declared, compiler allocates sufficient
memory to contain all its elements. Its base address(Address Of First
Element) is also allocated by the compiler.
int arr[5] = { 1, 2, 3, 4, 5 };
Suppose the base address of arr is 1000 and each integer requires two
bytes, the five elements will be stored as follows:
Variable arr will give the base address, which is a
constant pointer pointing to arr[0]. Hence arr contains the address
of arr[0] i.e 1000.
Example:
#include <stdio.h>
int main() OUTPUT:
{
int arr[3] = {10, 20, 30}; Array: 20
int *ptr = arr; // Pointer pointing to first element
printf("Array: %d\n", arr[1]); // Using array indexing
Pointer: 20
printf("Pointer: %d\n", *(ptr+1)); // Using pointer arithmetic
return 0;
}
Array Of pointers:
[Link] a Short Note on Multiple Indirection.
You can have a pointer point to another pointer that points to the
target value. This situation is called multiple indirection, or
pointers to pointer.
Lets suppose we have a pointer ‘p1′ that points to yet another
pointer ‘p2′ that points to a character ‘ch’. In memory, the three
variables can be visualized as :
So we can see that in memory, pointer p1 holds the address of
pointer p2. Pointer p2 holds the address of character ‘ch’.
So ‘p2′ is pointer to character ‘ch’, while ‘p1′ is pointer to ‘p2′ or
we can also say that ‘p2′ is a pointer to pointer to character ‘ch’.
Now, in code ‘p2′ can be declared as :
char *p2 = &ch;
But ‘p1′ is declared as :
char **p1 = &p2;
So we see that ‘p1′ is a double pointer (ie pointer to a pointer to a
character) and hence the two *s in declaration.
Now,
‘p1′ is the address of ‘p2′ ie 5000
‘*p1′ is the value held by ‘p2′ ie 8000
‘**p1′ is the value at 8000 ie ‘c’
Example: