Pointer
is a variable that stores the memory address of another variable. It allows for direct manipulation of memory, enabling efficient code and dynamic memory allocation. Pointers are declared with the * symbol, initialized with the address of a variable, and can be dereferenced to access the value at the stored memory location. They support arithmetic operations, can be set to NULL to indicate no valid address, and are fundamental for tasks like dynamic memory management and efficient data manipulation.
Syntax of Pointers
data_type
*
ptr
;
How to use pointers in C
1. Pointer Declaration
2. Pointer Initialization
3. Pointer Dereferencing
1. Pointer Declaration
A pointer declaration is a statement that introduces a pointer variable. The declaration specifies the data type of the variable to which the pointer will point. The basic syntax for pointer declaration is as follows:
data_type
*
pointer_name
;
Here:
1. data_type is the type of the variable to which the pointer will point.
2. * indicates that the variable being declared is a pointer.
3. pointer_name is the name given to the pointer variable.
Here are a few examples of pointer declarations:
int
*
ptr
;
char
*
charPtr
;
float
*
floatPtr
;
int
*
arr[5]
;
int
*
arrPtr
=
arr
;
A
pointer
declaration only reserves space for the memory
address
; it doesn't allocate memory for the data itself. You typically need to initialize the
pointer
by assigning it the address of an existing variable or allocate memory dynamically using functions like
malloc
or
calloc
.
2. Pointer Initialization
Pointer initialization in C involves assigning the memory address of a variable to a pointer. The basic syntax is:
data_type
*
pointer_name
= &
variable
;
Here:
1. data_type is the type of the variable to which the pointer will point.
2. * indicates that the variable being declared is a pointer.
3. pointer_name is the name given to the pointer variable.
Here are a few examples of pointer declarations:
int
x
=
10
;
int
*
ptr
= &
x
;
char
ch
=
'A'
;
char
*
charptr
= &
ch
;
float
f
=
3.14
;
float
*
floatptr
= &
f
;
int
arr[5]
= {
1, 2, 3, 4, 5
};
float
*
arrptr
=
arr
;
int
x
=
10
;
int
*
ptr
= &
x
;
int
**
ptr
= &
ptr1
;
Remember, it's crucial to initialize pointers before using them, either by assigning the address of an existing variable or by allocating memory dynamically. Uninitialized pointers can lead to undefined behavior and should be avoided.
3. Pointer Dereferencing
Dereferencing a pointer in C involves using the * (asterisk) operator to access the value stored at the memory address pointed to by the pointer.
int
x
=
10
;
int
*
ptr
= &
x
;
int
value
= *
ptr
;
Here:
1. data_type is the type of the variable to which the pointer will point.
2. * indicates that the variable being declared is a pointer.
3. pointer_name is the name given to the pointer variable.
Here are a few examples of pointer declarations:
int
x
=
10
;
int
*
ptr
= &
x
;
char
ch
=
'A'
;
char
*
charptr
= &
ch
;
float
f
=
3.14
;
float
*
floatptr
= &
f
;
int
arr[5]
= {
1, 2, 3, 4, 5
};
float
*
arrptr
=
arr
;
int
x
=
10
;
int
*
ptr
= &
x
;
int
**
ptr
= &
ptr1
;
Remember, it's crucial to initialize pointers before using them, either by assigning the address of an existing variable or by allocating memory dynamically. Uninitialized pointers can lead to undefined behavior and should be avoided.