0% found this document useful (0 votes)
3 views4 pages

Pointer

A pointer in C is a variable that stores the memory address of another variable, allowing for dynamic memory allocation and efficient data structure management. Different types of pointers include null pointers, void pointers, wild pointers, dangling pointers, and near pointers, each serving unique purposes. Pointers can also be used to create double pointers, which store the address of another pointer, facilitating complex data manipulation.

Uploaded by

Ashish Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Pointer

A pointer in C is a variable that stores the memory address of another variable, allowing for dynamic memory allocation and efficient data structure management. Different types of pointers include null pointers, void pointers, wild pointers, dangling pointers, and near pointers, each serving unique purposes. Pointers can also be used to create double pointers, which store the address of another pointer, facilitating complex data manipulation.

Uploaded by

Ashish Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Pointer

A Pointer is a variable that holds the memory address of another variable. When we declare a
variable of a specific data type we assign some of the memory for the variable where it can
store it's data. By default the memory allocation is in stack.

Using malloc function in C we can allocate memory for a variable in the heap section(also called
as dynamic memory allocation). One of the major advantages of allocating memory in heap is it
allows you to access variables globally.

For using a pointer in C , we need to use a unary operator '*' before the name of the pointer
variable to let the compiler know that this is a pointer variable.

int *b;

Now to assign address of another variable we use another unary operator that is the '&'
operator to assign the address of a variable to pointer variable.

int a = 10;
int *b = &a;

Note that the '*' operator that we used to declare a variable as pointer is also used to get the
value at that address that the pointer holds. This is also known as dereferencing.

#include <stdio.h>
int main()
{
int a= 10;
int *b = &a;
printf("%d" , *b);
}
Here in the printf function , we dereferenced the pointer variable p to get the value at the
address which the pointer variable p is holding.

Types of Pointers in C

Null Pointer in C
If we don't initialize a pointer after declaration then by default the pointer is a Null pointer . We
can also explicitly do this by assigning the NULL value at the time of pointer declaration.

Void Pointer in C
The void pointer is a generic pointer that does not have any data type associated with it. The
datatype of void pointer can be of any type and can be typecast to any type.

Wild Pointer in C
Wild pointers are basically uninitialized pointers that points to arbitrary memory location and
may cause a program to crash or behave badly.

Dangling Pointer in C
A dangling pointer is a pointer that is pointing to a memory location that has been deleted or
released.

Near Pointer in C
Near Pointer is a pointer that can access small size of data (usually 64kb) & can utilize bit
address (generally up to 16 bits) in a given section of memory that is 16 bit enabled.

Benefits of Pointers in C

One of the major advantages of using pointers is dynamic memory allocation which accelerates
the program execution when we have any task of updating the variable in different time frames
as the variable is directly accessible.

With the help of pointers we create different data structures such as Linked List, Trees etc. It
also helps to return more than one function value with the help of structures and classes.
Pointer to Pointer

a pointer is used to store the address of a variable in C. Pointer reduces the access time of a
variable. However, In C, we can also define a pointer to store the address of another pointer.
Such pointer is known as a double pointer (pointer to pointer). The first pointer is used to store
the address of a variable whereas the second pointer is used to store the address of the first
pointer.

The syntax of declaring a double pointer is given below.

int **p; // pointer to a pointer which is pointing to an integer.

Example of Pointer

#include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a;
pp = &p;
printf("address of a: %x\n",p);
printf("address of p: %x\n",pp);
printf("value stored at p: %d\n",*p);
printf("value stored at pp: %d\n",**pp);
}

Dereferencing Pointer

The pointer is declared with the " * " symbol. This operator indicates that declared variable is a
pointer variable rather than normal variable.

For example
int *num;
float *price;
char *p;
This is also known as dereferencing ( * ) operator. By using this * operator, we can access the
value of a variable through a pointer.

For example
int A=10, *num;
num=&A;
printf("%d", A); / * it prints " A " value that is 10 * /
printf("%d", *num); /* this is also print " A " value 10 through pointer " num " */

Both the above printf statements will return the same result. The 1st statement is the regular
printf statement. In the 2nd statement, * dereferencing operator fetches the address stored in
num variable and then gets the value stored in the fetched address.

You might also like