Pointers
What is a pointer?
A pointer is a variable that stores memory
address. If it is a variable, it must have a valid C
data type. Yes, every pointer variable has a data
type associated with it. Which means an integer
pointer can hold only integer variable addresses.
Note: We never say pointer stores or holds a memory location.
Instead, we say pointer points to a memory location. So from
now always use the language pointer points to a memory
location.
Reference operator &
Because we are dealing with memory addresses,
we must know how to get memory address of a
variable.
We use unary & (reference of) operator to get
memory address of a variable. Reference
operator is also known as address of operator.
Syntax to use reference of operator
&variable-name;
Dereference operator *
Once you have a memory address, you must be willing to
get value stored at that memory address, for that we need
to dereference the memory address.
Dereferencing is the process of retrieving value at memory
location pointed by a pointer. We use unary * dereference
operator to get value pointed by a memory address.
Dereference operator is also known as indirection
operator.
Syntax to use dereference operator
*memory-address-or-pointer-variable;
How to declare pointer variable?
Once you got basics of memory addresses,
reference and dereference operator. Let us
declare our first pointer variable.
Pointer variable declaration follows almost
similar syntax as of normal variable.
Syntax to declare pointer variable
data-type * pointer-variable-name;
data-type is a valid C data type.
* symbol specifies it is a pointer variable. You
must prefix * before variable name to declare it
as a pointer.
pointer-variable-name is a valid C identifier i.e.
the name of pointer variable.
Example to declare pointer variable
int * ptr;
In above example declared an integer pointer.
Following are some examples of declaring a
pointer in C:
int *ptr; //pointer to int
float *ptr; //pointer to float
char *ptr; //pointer to char
double *ptr; //pointer to double
Assigning Value to a Pointer Variable
When we declare a pointer, it contains garbage value,
which means it could be pointing anywhere in the
memory.
Pointer Initialization is the process of assigning the
address of a variable to a pointer.
In C language, the 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.
#include<stdio.h>
int main(void)
{
int a = 10;
// declare a pointer
int *ptr;
// assign value to pointer
ptr = &a;
printf("Value at ptr is: %d \n", *ptr);
printf("Address pointed by ptr is: %p \n", ptr);
return 0;
}
Pointer variables must always point to variables of the
same datatype.
For example, if we have a float type variable and an
int type pointer, then C compiler will give error.
#include<stdio.h>
int main(void)
{
float a = 10;
// declare a pointer
int *ptr;
// assign value to pointer
ptr = &a;
printf("Value at ptr is: %d \n", *ptr);
printf("Address pointed by ptr is: %p \n", ptr);
return 0; O\P
} warning: assignment from incompatible pointer type ptr = &x;
NULL pointer in C.
A pointer that is assigned a NULL value is called a
NULL pointer in C.
We can give a pointer a null value by assigning it zero
to it.
For example,
int *ptr = 0;
The above code will initialize the ptr pointer will a
null value.
We can also use the NULL macro, which is nothing
but a predefined constant for null pointer. This is
defined in the <stdio.h> header library.
int *ptr = NULL;
Pointers also deals with the following
Pointer to Pointer
Pointer and Arrays
Pointer Athematic
Pointers as Function
Pointer to Pointer
A pointer to a pointer is a form of multiple
indirection, or a chain of pointers.
The first pointer contains the address of the
second pointer, which points to the location
that contains the actual value as shown
below.
Declaring pointer to pointer in C
The syntax to declare a double pointer is
pointer_data_type **variable_name = &ordinary_pointer_variable;
A variable that is a pointer to a pointer must be declared
as such. This is done by placing an additional asterisk in
front of its name. Like this we can move up to 12 stages.
For example, the following declaration declares a pointer
to a pointer of type int −
int **var;
#include <stdio.h>
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);
return 0;
}
When the above code is compiled and
executed, it produces the following result −
Value of var :3000
Value available at *ptr :3000
Value available at **pptr :3000