Table of Contents
show
Definition
Pointer is a variable which stores the address of another variable. Since pointer is also a kind of variable, thus pointer itself will be stored at different memory location
Declaration
data_type *pointer_name;
Data type represents Type of variable that the pointer points to,
Asterisk is called as Indirection Operator
Initialization
Pointer variable holds address of another variable
data_type variable;
data_type *pointer;
pointer = &variable;
Memory required for storing pointer
- Pointer Variable Stores the address of the variable
- Variable may be integer,character,float but the address of the variable is always integer so Pointer requires 2 bytes of memory
Pointer operators
- Referencing
- Dereferencing
Referencing
- A pointer variable is made to refer to an object. The reference to an object can be created with the help of a reference operator &
- The reference operator & is a unary operator and should appear on the left side of the operand
- The operand of reference operator should be a variable of arithmetic type or pointer type or function type
- The reference operator is also known as address of operator
Dereferencing
- Dereferencing Operation is performed to access or manipulate data contained in memory location pointed to by a pointer
- The object pointed to or referenced by a pointer can be indirectly accessed by dereferencing the pointer
- A dereferencing operation allows a pointer to be followed to the data object to which it points
- A pointer can be dereferencing using a dereference operator (*)
- The dereference operator & is a unary operator and should appear on the left side of the operand
- The operand of a dereference operator should be of pointer type
- The dereference operator is also known as indirection operator or value-at operator
#include<stdio.h>
int main()
{
int a = 3;
int *ptr = &a;
int **pptr = &ptr;
printf("a=%d\n",a);
printf("&a = %p\n",&a);
printf("ptr=%p\n",ptr);
printf("*ptr = %d\n",*ptr);
printf("&ptr = %d\n",&ptr);
printf("pptr=%p\n",pptr);
printf("**pptr = %d\n",**pptr);
printf("&pptr = %d\n",&pptr);
return 0;
}
Output
a=3
&a = 000000000062FE1C
ptr=000000000062FE1C
*ptr = 3
&ptr = 6487568
pptr=000000000062FE10
**pptr = 3
&pptr = 6487560
Assigning to a pointer
- A pointer can be assigned or initialized with the address of the object. A pointer object cannot hold a non-address value and thus can only be assigned or initialized with address
- A pointer to a type cannot be initialized or assigned the address of an object of another type
- A pointer can be assigned or initialized with another pointer of the same type. However, it is not possible to assign a pointer of one type to a pointer of another type without explicit type casting
Generic pointer (void pointer)
- In C General Purpose Pointer is called as void Pointer.
- It does not have any data type associated with it
- It can store address of any type of variable
- The compiler has no idea what type of object a void Pointer really points to
Syntax
void * pointer_name;
Example
void *ptr; // ptr is declared as Void pointer
char cnum;
int inum;
float fnum;
ptr = &cnum; // ptr has address of character data
ptr = &inum; // ptr has address of integer data
ptr = &fnum; // ptr has address of float data
Advantage
To increase reusability of pointer.
Null pointer
- A null pointer is a special pointer that does not point anywhere.
- It does not hold the address of any object or function
- It has numeric value 0
Syntax
int *nptr = 0;
The macro or symbolic constant NULL defined in the header files stdio.h, stddef.h, stdlib.h, alloc.h and mem.h can be used for the creation of a null pointer
Example
int *iptr = NULL;
Views: 0