You can not imagine C language without the C pointers. A pointer is a very important concept of C language, so you should have good knowledge of pointer. In this blog post, I am giving a small introduction to C Pointers. This pointers introduction post gives only small information on pointers. If you want complete information about the pointers you read my other post “Complete information of pointers in C/C++“.
C Pointers Introduction:
A pointer is similar to a variable but the difference is that pointers are store the address of a location in memory and the variable stores the value. In other words, we can say, a pointer is used to reference a location in the memory.
When we have used a pointer to store the address in the memory then using the dereferencing techniques we can also get the value from the address which is stored by the pointer.
Syntax:
The Declaration of a pointer is very important because at the time of declaration you define the capability of the pointer. Every pointer has the data types (pre-defined or user-defined) and names followed by an asterisk (*). Asterisk is a unary operator.
Data_Type * Pointer_Name;
Let’s see the below-mentioned example to understand the declaration of a pointer.
char *cPtr // pointer to a character int *iPtr; // pointer to an integer float *fPtr; // pointer to a float double *dPtr; // pointer to a double
How to Use Pointers?
I think before understanding how to use the pointers, we should know the two important unary operators. These unary operators are indirection operator ( * ) and address of operator ( &).
Indirection operator or Dereference Operator ( * )
It is a unary operator that is used in the declaration of the pointer and accesses a value indirectly, through a pointer. The operand of the indirection operator should be a pointer and the result of the operation is value addressed by the operand (pointer).
In another word you can understand that if the operand of indirection operator has type ‘‘pointer to type’’, the result of the operation has type ‘‘type’’.
Let see an example,
int *iPtr;
// Use of indirection operator in the declaration of pointer
a = *iPtr;
//Use of indirection operator to read the value of the address pointed by the pointer
*iPtr = a;
//Use of indirection operator to write the value to the address pointed by pointer.
Address of operator ( &)
It is also a unary operator and gives the address of the operand. According to C standard “The operand of the unary & operator shall be either a function designator or an lvalue that designates an object that is not a bit-field and is not declared with the register storage-class specifier”.
Let see an example,
int data = 0;
// declaration of integer variable
&data =>
Give the address of the data
int *pData ;
// declaration of pointer
&pData =>
Give the address of the pointer
Now I think we should come on the topic “how to use pointers”. So we require mainly three steps to use the pointers in the programs these are mentioned below.
1. Declare a pointer
2. Assigned address to the pointer.
3. Access the pointers.
Let see an example,
In the below example, I am creating an integer pointer (iPtr) and using this printer I am accessing an integer variable (data).
#include <stdio.h> int main (void) { int data = 20; // declaration of variable int *iPtr = NULL; // declaration of pointer iPtr = &data; // Assign address of data to the pointer printf("Address of data: %p\n\n", &data); //Address stored in pointer printf("Address stored in iPtr: %p\n\n", iPtr); //Read value from the stored address with help of pointer printf("value of *iPtr = %d\n\n", *iPtr ); //Assign value to the stored address with help of pointer *iPtr = 5; //Again Read value from the stored address with help of pointer printf("New value of *iPtr = %d\n\n", *iPtr); printf("data = %d\n\n", data); return 0; }
Output:
Advantages of C Pointers:
- We can access the memory location with the help of C Pointers.
- With the help of pointers, we can pass the structure in an efficient way. It helps to reduce stack usage.
- We can access the array elements with the help of C Pointers.
- Pointers are used for dynamic memory allocation using the memory management function.
- It is used in complex data structures like linked lists, trees, etc.
- Using the pointer we can jump from one application to another application.
You can check, MCQs On Pointers in C/C++.
You want to learn more about C Pointers, you can check the below articles.
- A Detail description of the pointer in C.
- Dangling, Void, Null and Wild Pointers
- Function pointer in c, a detailed guide
- How to use the structure of function pointer in c language?
- Function pointer in structure.
- Pointer Arithmetic in C.
- void pointer in C.
- 10 questions about dynamic memory allocation.
- Memory Layout in C.
- 100 C interview Questions
- File handling in C.
- C format specifiers.