Pointer in C:
In a similar way, the pointer stores the address of another variable in its memory Location.
Like a variable, before using a pointer we have to declare it first.
The syntax for declaring pointer:
data_type *variable_name;
(or)
data_type* variable_name;
*(Asterisk symbol): Also called an Indirection operator. It is used in front of the pointer name to indicate that this is the pointer that points to a variable of type int, char, float, and void.
Declaration of the pointer in C:
int *p; // P is a Pointer, pointing to int type
char *q; // Q is a pointer, pointing to char type
float *r; // r is a pointer, pointing to float type
Initialization of the Pointer in C:
int n=10; // Normal variable
int *p = &n; // Pointer variable
Variable p of type pointer pointing to the address of the variable n of type integer.
&(ampersand): Ampersand operator is used to locating the address of the variable n.
Example1: let's print the address and value of the pointer from the above declaration.
Program:
Output:
Explanation: To hold the address of another variable, use the &(ampersand) symbol. To hold the value of another variable, use the indirection(*) operator.
Pointer To An Array:
The Pointer to an array means the pointer is pointing to the array. The actual mean is that the pointer holds the starting address of the array.
int a[5]; // Array Declaration
int *p=&a; //Pointer to an array
Example2: Program to illustrate the pointer to an array.
Program:
Output:
Explanation: In the above example, the pointer p holds the starting address of the array. As the iteration goes on the value of the address gets changed. The value present at the address will be printed. Indirectional operator(*) is used to hold the value present at the address.
Null Pointer: A pointer that is assigned null is called Null Pointer. Null is a constant with the value zero. The Null Pointer is used in the case when you don't have any address to be specified at the time of declaration.
Declaration of Null Pointer:
int *p = NULL;
The NULL should be in uppercase. This is because of the NULL is a macro with the value zero. The names of macros in the C/C++ should be uppercase.
Example3: Program to print the value of Null Pointer.
Program:
Output:
Advantages of Pointers:
1. It makes you able to access any memory location in the computer's memory.
2. We can able to return multiple values from the function using the pointer.
3. Pointer reduces the code and improves the performance of code.
Applications Of Pointers:
1. Dynamic Memory Allocation: In the C language, we can dynamically allocate the memory for the pointer using malloc() and calloc() functions. These functions will be discussed later.
2. Arrays, Functions, and Structures: Pointers in C Language is widely used in arrays, functions, and structures to reduce the code and improves performance.
In this tutorial, we have learned about the C pointer, which is a special variable store the address of another variable. And also the relation between array and pointer, and how to manipulate an array through a pointer.
prev<---strings in C Next Topic--->Functions in C
No comments:
Post a Comment