0% found this document useful (0 votes)
16 views21 pages

Pointer in C

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)
16 views21 pages

Pointer in C

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/ 21

By,

LINGESHWARAN.R
Pointer in C:
A pointer in C is a variable that stores the memory address of another variable. Instead of
holding data like regular variables, a pointer holds the location where data is stored in memory.
How To Initialize Pointers In C?
Example:
Types of Pointers in C
Common types of pointers :
Six common types of pointers that have special behavior in a
C program.

➢ Null Pointers In C
➢ Void Pointers In C
➢ Wild Pointers In C
➢ Dangling Pointers In C
➢ Pointer To Pointer/ Double Pointers In C
➢ Constant Pointers In C
➢ Pointer To Constant
1)Null Pointers in C:
A null pointer in C is a pointer that points to nothing. It's represented by NULL,
which is defined as ((void*)0).

Syntax Of Null Pointers In C:


data_typ *pointer_name = NULL; (or) pointer_name = NULL

Null Pointer Example:

int *ptr = NULL;


if (ptr == NULL)
{
printf("Pointer is null\n");
}
Example:
2)Void Pointers in C:
A void pointer (void*) is a special pointer that can store the address of any
data type.
Key Points:

1.General Purpose:
Can point to any data type (e.g., int, float, char).
EX:
int a = 10;
void *ptr = &a; // Points to an int

2.Requires Typecasting:
You must cast it to the correct type before dereferencing.
EX:
printf("%d", *(int*)ptr); // Cast to int* before use

3.No Arithmetic:
Pointer arithmetic like ptr++ is not allowed because the size of the type is unknown.
Example1:
Example2:
3)Wild Pointers in C:
A wild pointer is an uninitialized pointer that points to a random memory location. Using it
leads to unpredictable behavior, such as crashes or corrupted data.

Key Points:

1.Uninitialized:
Declared but not assigned any value.
EX:
int *ptr; // Wild pointer

2.Undefined Behavior:
Dereferencing a wild pointer can cause crashes or garbage output.

3.Not NULL:
Wild pointers are different from null pointers, as they point to invalid memory, not NULL.
How to Avoid Wild Pointers:

1.Initialize to NULL:
EX:
int *ptr = NULL;

2.Allocate memory:
EX:
int *ptr = (int*)malloc(sizeof(int));

3.Free safely:
EX:
free(ptr); ptr = NULL;
Wild Pointer Example:
4)Dangling Pointer in C:

A dangling pointer is a pointer that points to memory that has been freed or deallocated. Using
a dangling pointer can lead to undefined behavior, such as accessing invalid memory.

Key Points:

1.Cause: Happens when you free memory but don't set the pointer to NULL.
EX:
int *ptr = (int*)malloc(sizeof(int)); free(ptr); // Memory is freed, but ptr still points to the freed memory

2.Problem: Dereferencing a dangling pointer can cause crashes or unexpected behavior.

3.Fix: Set the pointer to NULL after freeing memory.


EX:
free(ptr); // Free memory ptr = NULL; // Avoid dangling pointer
Dangling Pointer Example:
5) Pointer to Pointer / Double Pointers in C:

A pointer to pointer (or double pointer) is a pointer that holds the address of another
pointer.
➢ This is useful when you need to modify the original pointer or deal with dynamically allocated
memory.

Syntax:
int **ptr;
➢ ptr is a pointer that points to another pointer that in turn points to an integer.
Example:
6) Constant Pointers in C:

A constant pointer is a pointer that cannot point to a different memory location after initialization,
but the value it points to can still be changed.

Syntax:
int *const ptr;

➢ ptr is a constant pointer to an integer, meaning ptr can't change its address once assigned.
Example:
7) Pointer to Constant:
A pointer to constant is a pointer that points to a constant value. You cannot modify the
value that the pointer is pointing to, but you can change the address the pointer holds.

Syntax:
const int *ptr;
➢ ptr is a pointer to a constant integer, meaning you cannot change the value at the memory location it
points to.
Example:

You might also like