Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null pointer in C #5953

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions content/c/concepts/pointers/null_pointer/null_pointer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
---
Title: 'Null Pointers'
Description: 'The Null Pointer is the pointer that does not point to any location but NULL.'
Subjects:
- 'Code Foundations'
- 'c-programming pointers'
- 'C-Basics'
Tags:
- 'Pointers'
- 'Memory'
- 'Variables'
- 'Arrays'
CatalogContent:
- 'learn-c'
- 'paths/computer-science'
---

**An integer constant expression with the value 0, or such an expression cast to type void \***, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to be compared unequally to a pointer to any object or function.

##SYNTAX

```
type pointer_name = NULL;
type pointer_name = 0;
```
type pointer_name = NULL;
type pointer_name = 0;


## Understanding Null Pointer

- Null pointer does not point anything.
- Null pointer constant value is 0.
- A null pointer is guaranteed to compare unequal to any pointer that points to a valid object.

## Uses of Null Pointer

Following are some most common uses of the NULL pointer in C:

1. To initialize a pointer variable when that pointer variable hasn’t been assigned any valid memory address yet.
2. To check for a null pointer before accessing any pointer variable. By doing so, we can perform error handling in pointer-related code, e.g., dereference a pointer variable only if it’s not NULL.
3. To pass a null pointer to a function argument when we don’t want to pass any valid memory address.
4. A NULL pointer is used in data structures like trees, linked lists, etc. to indicate the end.

** We can check whether null pointer value is null or not by using isequal (==) operator **

## Declaration of Null Pointer

Declaration of null pointer same as the pointer valrible

```
int* pointer = NULL;

```
NULL must be all uppercase letters


## EXAMPLE 1

```

#include <stdio.h>

int main() {
int *ptr = NULL; // ptr is a null pointer
if (ptr == NULL) {
printf("Pointer is null.\n");
}
return 0;
}

```

## output

If the pointer is null pointer print the statement in printf() otherwise nothing will be printed



## EXAMPLE 2

```
// C NULL pointer demonstration

#include <stdio.h>

int main()
{
// declaring null pointer
int* ptr = NULL;

// derefencing only if the pointer has any value
if (ptr == NULL) {
printf("Pointer does not point to anything");
}
else {
printf("Value pointed by pointer: %d", *ptr);
}
return 0;
}

```
## output

```
Pointer does not point to anything

```

## Common Issues with Null Pointers

While null pointers are useful, improper handling can lead to significant issues:

1. **Null Pointer Dereference**
Dereferencing a null pointer is an undefined behavior and often leads to program crashes or segmentation faults.
```
int *ptr = NULL;
*ptr = 10; // Undefined behavior
```
2. **Accidental Null Pointer Access**
--Sometimes, logic errors in a program can result in an attempt to access or modify data through a null pointer.

3. **Difficult Debugging**
--Null pointer errors can be hard to debug, especially in large codebases, because they often manifest as crashes without clear error messages.

## Best Practices for Handling Null Pointers
To avoid issues with null pointers, programmers should follow best practices:

1. **Explicitly Initialize Pointers**
--Always initialize pointers to a known value, preferably NULL or nullptr.

```
int *ptr = NULL;
```

2. **Check for Null Before Dereferencing**
Always check if a pointer is null before attempting to dereference it.
```
if (ptr != NULL) {
*ptr = 10;
}
```

4. **Enable Compiler Warnings**
Modern compilers can warn about potential null pointer dereferences. Enable these warnings and address them promptly.