Pointers in C are very easy to learn a few tasks in C language are done by using pointers. And some tasks like dynamic memory allocation done only by using pointers. So it is essential to learn pointers. POINTER is a variable that stores the address of the other variable. A pointer is also used to refer to a pointer function. And pointer can be incremented or decremented that is if the pointer is incremented then it points to the next and if the pointer is decremented it points to the previous memory location. The goal of the pointer is to save memory space and perform faster execution. And the size of the pointer in C is 8 bytes but on a 32-bit machine, they take up to 4 bytes.
Int Y= 1;
Y is equal to one now.
"&Y"
Example:
#include<stdio.h>
int main()
{
int Y=1;
printf(“%d”, &Y);
return 0;
}
Output: 123756948 and if you run the same code for the second time the output may be different.
In simple terms, variable store values and pointers store the address of the variable.
Like variables, pointers should be declared before using it in the program. We can name pointers anything as long as they obey C’s naming rules.
Syntax: Data_type * pointer_variable_name;
Example: int*a;
After declaring a pointer, we have to initialize the pointer with the standard variable address.
If pointers are not initialized then there may be a problem in the output.
Syntax: pointer= &variable;
Example: p= &a;
Types of Pointers in C |
A pointer that points to nothing is called a Null pointer. Some advantages of Null pointer are:
We can initialize a pointer variable when that pointer variable is not assigned any actual memory address.
We can pass a null pointer to a function argument when we are not willing to pass any actual memory address.
Example 1:
int * aInt = NULL;
Example 2:
int fun(int *ptr)
{
return 15;
}
fun(NULL);
Example 3:
if (aINT != NULL)
{ //some code}
else
{//some code}
Related Article: Characteristics of C Language |
The void pointer within C is a pointer that is not allied with any data types. This points to some data location within the storage means points to that address of variables. It is also known as a general-purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.
Example:
int x= 10;
char y= ‘a’;
void *p= &x //void pointer contains address of int x
p = &y //void pointer holds of char y
Pointers that are not initialized are called wild pointers. This pointer may be initialized to a non-NULL garbage value which may not be a valid address.
Example:
int main()
{
int *p; // wild pointer
*p= 10;
}
Remember if a pointer p points to any known variable,
then it is not a wild pointer. In the below program p is a wild pointer until it points to x.
int main()
{
int = *p; // wild pointer
int x= 20;
p= &x // p is not a wild pointer now
}
Also, Check out our blog on C Tutorial |
A pointer that points to a memory location that has been deleted is called a dangling pointer.
Example 1:
Deallocation of memory:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int));
free(ptr);
ptr = NULL;
}
Example 2:
Function call:
#include<stdio.h>
int *fun()
{
int y= 15;
return &y
}
int main()
{
int *p = func()
fflush(stdin)
printf(“%d”, *p);
return 0;
}
Before knowing how to read complex pointers then you should first know associativity and precedence.
Associativity: Order operators of equal precedence within an expression are employed.
Precedence: Operator precedence describes the order in which C reads expressions.
Operator |
Precedence |
Associative |
(),[] |
1 |
Left to Right |
*,Identifier |
2 |
Right to Left |
Data Type |
3 |
– |
[]: this is an array subscript operator.
*: this is a pointer operator.
Identifier: this is the name of a pointer.
Data type: this is the type of variable.
Example:
int (*p)(int (*)[3], int (*)void))
Example:
#include<stdio.h>
int main()
{
int a= 300;
int near* ptr;
ptr= &a;
printf(“%d”, size of ptr);
return 0;
}
Output: 3
Example:
#include<stdio.h>
int main()
{
int a= 10;
int far *ptr;
ptr=&a;
print(“%d”, sizeof ptr);
return 0;
}
Example:
#include<stdio.h>
int main()
{
char huge *far *a;
printf(“%d%d%d”, sizeof(a), size(*a), sizeof(**a));
return 0;
}
Output: 4 4 1.
Pointers make it possible to pass the address of the structure rather than the entire structure to the functions.
For an in-depth understanding of Pointers click on:
You liked the article?
Like: 11
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.