C_81 Null Pointer in C | C Programming Tutorials

Jenny's Lectures CS IT
26 Aug 202121:34

Summary

TLDRThis video script discusses null pointers in C programming, explaining their definition, usage, and importance. It differentiates null pointers from void and uninitialized pointers, emphasizing the risks of dereferencing a null pointer and the benefits of initializing pointers with null for error handling. Practical examples are provided to illustrate the concepts.

Takeaways

  • πŸ˜€ A null pointer in C is a special pointer that does not point to any valid object or memory location.
  • πŸ” Null pointers are typically initialized with the value `NULL` (which is zero in C), ensuring they do not point to any valid memory address.
  • 🚫 It is crucial not to dereference a null pointer, as doing so will lead to undefined behavior and potentially crash the program.
  • πŸ’‘ The primary use of null pointers is for error handling, especially in scenarios where dynamic memory allocation might fail.
  • πŸ“š Null pointers are defined in standard C header files like `<stdio.h>` and `<stddef.h>`, making them a standard part of the language.
  • πŸ”„ When a pointer is not initialized, it contains a garbage value, making it a wild pointer, which is risky to use without proper checks.
  • πŸ”‘ Initializing a pointer with `NULL` is safer than leaving it uninitialized, as it avoids undefined behavior and potential crashes.
  • πŸ” Before performing operations on a pointer, it is essential to check if it is null to prevent dereferencing a null pointer.
  • πŸ”— Null pointers are different from void pointers, which are pointers that can point to any type of data, while null pointers specifically do not point to any valid object.
  • πŸ”„ Comparing two null pointers will always result in equality because they both contain the value zero, indicating they do not point to any valid memory location.

Q & A

  • What is a null pointer?

    -A null pointer is a special pointer in C that does not refer to any valid object or address. It is initialized with the value zero. In the context of pointers, null is a macro defined in the standard library headers that represents a null pointer constant.

  • Why is it important to initialize a pointer with null?

    -Initializing a pointer with null is important to avoid undefined behavior. An uninitialized pointer can contain garbage values, which may point to any random memory location, potentially leading to crashes or incorrect program behavior. Using null ensures that the pointer does not accidentally dereference an invalid memory address.

  • What happens if you dereference a null pointer?

    -Dereferencing a null pointer will result in a program crash because a null pointer does not point to any valid memory location. Attempting to access the memory location it points to (which is zero) will lead to undefined behavior, typically causing the program to terminate.

  • How is a null pointer different from an uninitialized pointer?

    -A null pointer is explicitly initialized to zero and is a valid pointer constant that does not point to any object. An uninitialized pointer, on the other hand, has not been assigned any value and may contain a garbage value, which is unpredictable and could point to any random memory location.

  • What is the purpose of using a null pointer in programming?

    -The primary purpose of using a null pointer is for error handling and to indicate that a pointer does not point to a valid memory location. It is commonly used in scenarios where memory allocation might fail, or when a function needs to indicate that no valid address is being passed.

  • Can you compare two null pointers for equality?

    -Yes, you can compare two null pointers for equality. Since both null pointers are initialized to zero, comparing them will always result in true, indicating that they are equal.

  • What is the difference between a null pointer and a void pointer?

    -A null pointer is a pointer that does not point to any valid object and is initialized to zero. A void pointer, on the other hand, is a generic pointer type that can point to any type of data. The null pointer is a specific value that indicates no valid address, whereas a void pointer is a type that can be used to point to any data type.

  • How can you check if a pointer is null before dereferencing it?

    -You can check if a pointer is null by using an if statement to compare the pointer to null. For example, `if (ptr == NULL) { /* pointer is null, handle accordingly */ }`. This check helps prevent dereferencing a null pointer, which would lead to a program crash.

  • Why is it risky to use an uninitialized pointer?

    -Using an uninitialized pointer is risky because it may contain a garbage value that points to an arbitrary memory location. Dereferencing such a pointer can lead to undefined behavior, including program crashes or corruption of data, as the program might access or modify memory that it should not.

  • How does a null pointer affect dynamic memory allocation?

    -In the context of dynamic memory allocation, a null pointer is used to indicate that memory allocation has failed. If a function like `malloc` or `calloc` cannot allocate the requested memory, it returns a null pointer. Checking for a null pointer after a memory allocation attempt is crucial to handle such failures gracefully.

Outlines

00:00

πŸ“Œ Introduction to Null Pointers

This paragraph introduces the concept of null pointers in programming. It explains that null pointers are special pointers that do not point to any valid memory location. The speaker discusses the importance of understanding null pointers, as they are often a topic in interviews and are crucial for avoiding undefined behavior in programs. The paragraph also touches on the difference between uninitialized pointers, which can point to any random memory location, and null pointers, which are explicitly set to point to no location. The speaker emphasizes the need to initialize pointers to null to prevent potential crashes or errors in the program.

05:01

🚫 Avoiding Dereferencing Null Pointers

The speaker continues the discussion on null pointers, focusing on the dangers of dereferencing them. It is highlighted that dereferencing a null pointer will lead to a program crash because null pointers do not point to any valid object. The paragraph also covers the initialization of pointers with null values, explaining that this practice is safer than leaving pointers uninitialized. The speaker provides an example of how to check if a pointer is null before dereferencing it, which is a common practice in error handling in C. The importance of this check is underscored by the potential for memory allocation failures, which could result in a pointer remaining null.

10:02

πŸ” Uses and Purposes of Null Pointers

In this paragraph, the speaker delves into the specific uses and purposes of null pointers in programming. It is explained that null pointers are often used in data structures and during dynamic memory allocation to indicate that a pointer does not currently point to a valid address. The speaker also mentions that null pointers can be used to pass no valid address to a function, which will be discussed in more detail in future videos. The paragraph emphasizes the importance of initializing pointers with null to avoid undefined behavior and the need to check pointers before using them to ensure they are not null.

15:07

πŸ”¬ Practical Demonstration of Null Pointers

The speaker provides a practical demonstration of null pointers in this paragraph. They show how to declare and initialize pointers in C, and how to compare them. The demonstration includes printing the values pointed to by different pointers, including a null pointer and an uninitialized pointer. The speaker also illustrates the consequences of attempting to dereference a null pointer, which results in a program crash. The practical example serves to reinforce the theoretical concepts discussed earlier in the video.

20:07

🏁 Conclusion and Upcoming Topics

In the final paragraph, the speaker concludes the discussion on null pointers and provides a brief overview of what will be covered in the next video. They summarize the key points about null pointers, emphasizing that they do not refer to any valid object and should not be dereferenced. The speaker also invites viewers to ask questions in the comments if they have any doubts. The next topic to be discussed is the concept of dangling pointers, which will be explored in the subsequent video.

Mindmap

Keywords

πŸ’‘Pointers

Pointers are variables in programming that store the memory address of another variable. In the video, pointers are a central theme, as they discuss various types of pointers, such as null pointers, void pointers, and uninitialized pointers. Pointers are crucial in C programming for dynamic memory allocation and accessing data stored at specific memory locations.

πŸ’‘Null Pointer

A null pointer is a special pointer that does not point to any valid object or memory location. In the video, it is explained that a null pointer is initialized with the value zero and is used to indicate that a pointer does not hold a valid address. It is important to check for null pointers before dereferencing them to avoid undefined behavior or program crashes.

πŸ’‘Void Pointer

A void pointer is a generic pointer type in C that can point to any data type. It is mentioned in the video that void pointers are different from null pointers, as void pointers are a type that can be used to point to any type of data, whereas null pointers specifically do not point to any valid object.

πŸ’‘Uninitialized Pointer

An uninitialized pointer is a pointer that has not been assigned a value or an address. In the video, it is highlighted that uninitialized pointers can contain garbage values and may point to invalid memory locations, leading to undefined behavior if dereferenced. It is suggested to initialize pointers to null to avoid such issues.

πŸ’‘Dangling Pointer

A dangling pointer is a pointer that points to a memory location that has been freed or is no longer valid. Although not deeply discussed in the provided script, the concept is mentioned as another type of special pointer that can cause issues in programming if not handled properly.

πŸ’‘Dynamic Memory Allocation

Dynamic memory allocation refers to the process of allocating memory at runtime, rather than at compile time. In the video, the use of functions like malloc and calloc for dynamic memory allocation is mentioned. It is in this context that null pointers are used to indicate whether memory has been successfully allocated or not.

πŸ’‘Dereference

Dereference is the act of accessing the value at the memory location pointed to by a pointer. The video emphasizes the importance of not dereferencing a null pointer, as it does not point to a valid object, and doing so would result in a program crash or undefined behavior.

πŸ’‘Error Handling

Error handling in the context of the video refers to the practice of checking pointers before using them to prevent runtime errors. It is mentioned that initializing pointers to null and checking them before dereferencing is a common error handling technique in C programming.

πŸ’‘Garbage Value

A garbage value is an undefined or unintended value that an uninitialized pointer might contain. The video script warns about the risks of using pointers with garbage values, as they can lead to unpredictable behavior or program crashes if dereferenced.

πŸ’‘Header Files

Header files in C are used to declare functions, macros, and other definitions that are needed by the program. The video mentions that null is defined in header files like stdio.h and stdlib.h, which are essential for using null pointers in C programs.

πŸ’‘Memory Address

A memory address is a specific location in a computer's memory where data is stored. The video discusses how pointers store memory addresses and how uninitialized pointers can point to arbitrary or invalid memory addresses, potentially causing issues when accessed.

Highlights

Introduction to null pointers and their importance in programming.

Difference between uninitialized pointers and null pointers.

Explanation of what a null pointer is and why it is used.

Null pointer is a special pointer in C with a value of zero.

Null pointers are used to prevent undefined behavior caused by uninitialized pointers.

Null pointers are used in error handling in C.

Null pointers are used when you don't want to pass any valid address in functions.

Null pointers are initialized with 'null' or '0', which is defined in C header files.

Null pointers point to a reserved memory location that cannot be dereferenced.

Practical demonstration of initializing pointers with null.

Difference between null pointers and void pointers.

Explanation of why you cannot dereference a null pointer.

Demonstration of the consequences of dereferencing a null pointer.

Importance of checking if a pointer is null before dereferencing.

Discussion on the use of null pointers in dynamic memory allocation.

Practical example of checking if a pointer is null before accessing its value.

Difference between uninitialized pointers and null pointers in terms of memory address.

Conclusion and summary of the key points about null pointers.

Transcripts

play00:00

so in the series of learning programming

play00:01

in see we are discussing pointers in say

play00:03

in the previous video I have discussed

play00:04

void pointer in say in this video I'll

play00:05

talk about null pointer in because there

play00:07

are some special pointer in see also

play00:09

like void null wild pointer and dangling

play00:12

pointer these these concepts are also

play00:14

really very helpful and generally in

play00:16

interview also they ask these type of

play00:18

questions right so we'll see everything

play00:20

about null pointer in this video like

play00:21

what is null pointer why we use it that

play00:23

is very important the application of

play00:25

null pointer all the important points

play00:27

about null pointer we will see with the

play00:28

help of a proper program we'll discuss

play00:30

here first of all then I'll show you

play00:31

practically on my laptop and we'll also

play00:33

see how this pointer is different from

play00:35

void pointer as well as if you don't

play00:37

initialize any pointer right how this

play00:40

pointer is different from that pointer

play00:42

that that an uninitialized pointer right

play00:45

everything about null pointer we will

play00:46

discuss in this video so now let's see

play00:48

what is null pointer see I hope you know

play00:51

uh how to initialize how to declare a

play00:54

pointer suppose I'm wenting int star

play00:58

PTR this is this is what a pointer to

play01:01

integer you can say an integer pointer

play01:04

right here we are not initializing this

play01:07

pointer

play01:08

means here is some

play01:11

PTR in memory right it's a special

play01:14

variable it will contain address of any

play01:16

other variable but we are not assigning

play01:18

any address till now right so maybe it

play01:21

is pointing to some some address in

play01:23

memory which is which is

play01:26

garbage because here we are having some

play01:29

garbage

play01:30

value we are not initializing it so it

play01:33

is pointing to any memory location right

play01:35

we don't know the address of that memory

play01:37

location so that at that memory location

play01:41

maybe this memory location is using by

play01:44

operating system itself for some

play01:46

important task or maybe some other

play01:48

process is using this memory location

play01:50

right or maybe this memory location is

play01:52

not valid at

play01:53

all anything can be possible so this is

play01:57

what uninitialized pointer we are not

play01:59

initializing this pointer it is

play02:01

containing any garbage value we don't

play02:02

know to which memory location is it is

play02:04

pointing that memory location may be

play02:06

valid may not be valid so when you use

play02:10

this pointer when you D reference now

play02:12

this pointer like print F percentage D

play02:15

and I want to De reference it a

play02:17

PTR by mistake I haven't initialized it

play02:21

but by mistake in program I want to find

play02:24

out the value that is pointed by this

play02:26

pointer I want to Der reference means

play02:28

value at this address

play02:30

but address in this pointer is any

play02:32

garbage value because we haven't

play02:33

initialized this yet so what you will

play02:36

get here we we cannot say it will show

play02:38

some undefined

play02:40

Behavior maybe program will crash or

play02:43

maybe it will give some garbage value we

play02:45

don't know it

play02:47

depends right so it will show some

play02:50

undefined Behavior so to you know remove

play02:54

this thing generally if you you don't

play02:56

initialize the pointer that is known as

play02:58

wild pointer so these are risky to use

play03:02

right so better to initialize it with

play03:08

null this null all caps letters n u l

play03:13

this is itself a pointer in C and the

play03:16

value is zero the corresponding value of

play03:18

this null is zero this zero is so in C

play03:22

you can write down null or

play03:25

zero but this zero we are using in

play03:29

context of pointers because this is

play03:31

pointer it is different from that

play03:33

integer zero right so better to use here

play03:37

null rather than zero n u l ultimately

play03:42

it would be zero so if you print here

play03:45

now if you print here like PTR in PTR

play03:49

now we have what null that is zero right

play03:53

it is already defined right in those

play03:57

Hile stdi in STD f.h in STD li. multiple

play04:02

header files it it has been defined

play04:05

right and its value is corresponding

play04:07

value of N is a zero so in pointer now

play04:08

we have

play04:09

zero right so it is pointing

play04:14

to to what because memory location is

play04:19

zero so you can say it is a special

play04:23

value that is pointing to that does not

play04:26

point to any valid object or you can say

play04:29

it is pointing to zeroth memory location

play04:32

which is reserved which is reserved you

play04:35

cannot dereference that memory

play04:38

location this thing is very important

play04:40

you cannot dreference

play04:43

it right or you can say it is pointing

play04:47

to it it doesn't point to any valid

play04:51

object any valid

play04:53

address if you write here if you write

play04:56

this s p is equal to none right so if

play05:00

you print here PTR it will give zero and

play05:03

if you try to dreference

play05:06

it your program will

play05:08

crash this thing is very important you

play05:11

cannot dereference a null pointer so see

play05:14

this null n this is itself a pointer in

play05:17

C

play05:19

right the value of this is corresponding

play05:21

value of this this is what zero this is

play05:23

null pointer in see and we can also use

play05:27

this pointer to initialize another

play05:28

pointer variable because PTR is a

play05:30

pointer variable I'm declaring a pointer

play05:33

of type int right and I'm initializing

play05:36

this with this pointer null pointer so

play05:38

now PTR also becomes a null pointer

play05:40

right so in simple terms you can say

play05:43

null pointer null pointer does not refer

play05:46

to any valid address or it does not

play05:49

refer to any valid

play05:51

object so if this is the case then what

play05:54

is the purpose of using null

play05:56

pointer there are some special purpose

play05:59

because that is why we use null pointer

play06:02

in our programming language and what are

play06:04

those purpose

play06:06

see if by

play06:08

chance I'm not initializing the pointer

play06:10

right now right because I want this

play06:14

pointer should point to a memory

play06:16

location which I would allocate

play06:17

dynamically using Malo and Cog function

play06:20

right suppose I'm uh using this pointer

play06:23

is equal

play06:24

to I'm using Malo function and I'm using

play06:28

here five into size of

play06:32

int dynamically at run time I want to

play06:35

allocate memory these Malo and Cog these

play06:37

functions are built-in functions which

play06:39

are used to allocate memory dynamically

play06:41

this thing we have discussed in data

play06:42

structure videos that you can you can

play06:44

watch that video videos or we'll see

play06:46

this thing later right if you you you

play06:49

are not getting this syntax leave it the

play06:52

the the meaning of this is simple

play06:53

meaning of this is what here I'm

play06:55

allocating some memory right and that

play06:59

memory this pointer should

play07:02

contain memory address that memory

play07:04

address right so suppose at run time I'm

play07:07

allocating this memory and address of

play07:09

this is th000 so it means this th000

play07:13

should store here so now this PTR is

play07:15

pointing to this memory address now this

play07:17

is valid memory address right but in

play07:21

starting I'm not initializing this poter

play07:24

at run time I want to initialize but by

play07:26

chance because of some

play07:28

problems because of some problems it is

play07:31

not possible to allocate memory using

play07:33

myow it is not possible to allocate

play07:35

memory during this time so ultimately in

play07:39

pointer in PTR null would be assigned

play07:43

null means zero would be

play07:45

assigned right so now at this point we

play07:49

think that we have written this syntax

play07:52

and memory has been allocated to this

play07:53

PTR and now this pointer is pointing to

play07:55

any valid memory location and further in

play07:58

the program I'm using something like I'm

play08:01

I'm supposed de referencing this pointer

play08:03

estri

play08:06

PTR right whatever here I I want to

play08:08

access this right but because of some

play08:11

problems this Malo has not been executed

play08:14

successfully and the memory has not been

play08:17

allocated to this this PTR the memory

play08:20

has not been allocated so now in PTR by

play08:23

default we have

play08:24

null and if you now dreference this

play08:28

pointer it will show

play08:29

what your program will crash because we

play08:32

cannot dereference a null

play08:35

pointer right so better to before before

play08:40

de referencing this better to check

play08:43

if PTR equal to equal to

play08:48

null if PTR is null you have to be very

play08:52

careful you cannot de reference that

play08:54

pointer or you cannot do anything

play08:55

because pointer is this pointer is

play08:57

pointing to invalid memory object

play08:59

invalid object right so we cannot do

play09:02

anything just you can

play09:04

print what memory has not been allocated

play09:08

else whatever you want to do you can do

play09:11

if memory has been allocated

play09:14

successfully right so now at starting

play09:18

please don't leave the pointer

play09:20

uninitialized better to initialize it

play09:22

with null so ultimately at starting we

play09:26

have null in this pointer and now if

play09:28

memory has been allocated successfully

play09:30

it's okay maybe the memory address is

play09:33

th000 so here in PTR now we have th000

play09:36

and it is pointing to this memory

play09:38

address right so better to check now

play09:42

before doing something with this pointer

play09:44

better to check if pointer is still null

play09:47

memory has not been allocated if not n

play09:49

null then you can do something with this

play09:51

pointer right so it is what you can say

play09:54

it is used in error handling in

play09:58

C because by mistake if pointer is not

play10:02

null like you haven't initialized this

play10:04

pointer and maybe by mistake you want to

play10:07

deference that pointer in the program so

play10:10

your program will

play10:12

crash right so to get rid of the

play10:15

situation to handle this error we use

play10:18

null pointer we initialize it with null

play10:21

this is the main thing this is the main

play10:24

purpose of using null pointer many times

play10:26

in data structure we use null pointers

play10:29

you can watch out my data structure

play10:30

playlist there I have used this null

play10:32

this keyword null this null pointer many

play10:34

times generally we use uh when we use

play10:38

dynamic memory allocation in that case

play10:40

we use null pointers that is the main

play10:42

purpose of using null pointer right and

play10:44

also one more thing like in function if

play10:47

you don't want to pass any valid address

play10:49

suppose I'm using any function and here

play10:52

uh at sometimes we don't I don't want to

play10:54

pass any valid address so you can pass

play10:56

null pointer here right so that we'll

play10:59

discuss when we will discuss the this

play11:01

function concept at that time we'll

play11:03

discuss this thing right so in brief

play11:06

what we can say about null pointer is

play11:07

what null pointer does not refer to any

play11:09

valid object

play11:11

secondly better to initialize a pointer

play11:15

with null rather than leaving it

play11:18

uninitialized right to get rid of the

play11:20

situation of that undefined Behavior

play11:23

right so better to initialize it with

play11:25

null and before doing something with

play11:27

this pointer better to check if pointer

play11:29

is null don't do anything because the

play11:32

third point is very important third

play11:33

point is very important you cannot

play11:35

dreference a null pointer because it is

play11:37

not pointing to any valid object so if

play11:40

you will dreference it program will

play11:43

crash right I'll show you practical also

play11:46

of this null pointer and this is

play11:48

different from void pointer as well as

play11:50

uninitialized pointer this also you need

play11:52

to take care of because null is what a

play11:54

value

play11:56

zero right although in the pointer

play11:59

context and vo is what a type so both

play12:01

pointer are different and one more thing

play12:04

if you declare something like this int

play12:06

pointer this pointer 1 is is equal to

play12:09

null and ptr2 is equal to null

play12:15

definitely these pointers would be same

play12:18

right if you compare these pointer would

play12:20

be same if you compare like if ptr1

play12:24

equal to equal to PTR 2 and here you

play12:27

will print both pointer P1 and uh

play12:30

pointer ptr1 and ptr2 are same this

play12:34

would definitely be executed this

play12:36

because this would always be same

play12:37

because in both we are storing null null

play12:39

means zero so both pointer are pointing

play12:42

to that zero memory location or you can

play12:43

say any that is reserved right we cannot

play12:46

deference it fine so both would be same

play12:49

but if you write something like this in

play12:51

ptr1 and if you don't initialize this

play12:54

and

play12:55

this so maybe you think you are not

play12:58

initializing these pointer so these

play12:59

pointer are also same no ptr1 now will

play13:04

contain any garbage value PTR

play13:07

2 will also contain some garbage value

play13:11

but it is not guaranteed that this

play13:13

garbage value and this garbage value are

play13:16

same but if you initialize with null

play13:19

that is for sure both will contain zero

play13:22

and zero null and

play13:24

null right so I hope you got this point

play13:28

uh this

play13:29

about the null pointer and uninitialized

play13:31

pointer and if you declare something

play13:32

like this in ptr1 is equal to null so in

play13:35

ptr1 we have null that is zero right so

play13:40

it is guaranteed that this is not this

play13:43

value is not equal to any other pointer

play13:46

which is pointing to any valid object

play13:48

right because this will this is

play13:50

containing zero that is

play13:52

null right if you take any other pointer

play13:55

this ptr2 this is uninitialized maybe

play13:58

this will contain sometimes zero I we

play14:00

cannot say we cannot say it depends or

play14:04

maybe it will contain some other garbage

play14:06

value but this is what obviously this is

play14:08

what invalid

play14:10

pointer uninitialized pointer this is

play14:13

what invalid pointer right and if here

play14:16

I'm storing address of like a I'm taking

play14:18

a variable int a is equal to 5 now this

play14:21

is what obviously a valid pointer

play14:24

because it is it is containing address

play14:25

of a is a variable somewhere in memory

play14:28

right so this null pointer it is

play14:31

guaranteed that it is not equal to any

play14:33

other pointer which is pointing to any

play14:34

valid

play14:36

object right so now let me show you this

play14:39

thing practically let me create a

play14:51

file we use header files then main

play14:54

function and here I'm declaring a

play14:57

pointer uh suppose I'm I'm

play14:59

taking

play15:01

ptr1 right and uh see uh if I take a

play15:07

variable in a is equal to suppose 3 and

play15:12

ptr1 is equal to address of

play15:17

a simply what you can

play15:20

do percentage D and you can print you

play15:25

can do what EST

play15:27

ptr1 so it should print value of a that

play15:31

is

play15:32

three see it is printing

play15:35

three right now if I write

play15:39

here only a strict PTR I'm not going to

play15:42

initialize

play15:43

it and now D reference it or suppose I'm

play15:49

I'm initializing this with

play15:51

null and

play15:56

L and one more poter I'm taking

play16:00

ptr2 and that is uninitialized pointer

play16:03

so whatever in ptr1 I'm going to print

play16:05

that thing and one

play16:14

more whatever value in ptr2 that also we

play16:18

are going to print so in PTR 1 obviously

play16:20

we will have see we are not using D

play16:22

referencing operator we are printing the

play16:24

value so in ptr1 we have zero and ptr2

play16:27

we have any garbage Valu because it is

play16:29

wild pointer uninitialized

play16:31

pointer maybe now I have told you see in

play16:33

first obviously we will have zero in

play16:35

ptr2 also it is printing zero it may be

play16:39

a case that it will have zero or it will

play16:43

have any garbage value we cannot say but

play16:46

null pointer will always have

play16:49

zero right so ptr1 will always have zero

play16:53

right now suppose uh here I'm uh storing

play16:58

what

play17:03

suppose I want to deference this null

play17:06

pointer

play17:08

see this is null pointer it is not

play17:10

pointing to any valid object and still I

play17:12

want to dereference it so obviously it

play17:16

program is going to crash so

play17:20

see okay so now

play17:24

see see n pointer exe has stopped

play17:27

working right because we cannot

play17:30

dreference a null pointer this you need

play17:32

to take care so you have to check before

play17:35

D referencing something like this

play17:38

if PTR equal to equal to what

play17:43

null then what you will print here you

play17:46

can simply

play17:47

[Music]

play17:48

write this

play17:51

is null pointer right means you cannot D

play17:55

reference it in lse part would you can

play18:00

say now you can D reference it right so

play18:05

now if you will run this what output you

play18:07

will

play18:09

get okay it's PTR it's not PTR it's PTR

play18:13

1

play18:16

ptr1 right so now let me run this and

play18:19

see this is null pointer because

play18:22

obviously this is null

play18:23

pointer right so better to use this one

play18:26

this we use uh for error handling in C

play18:30

right and if you take another pointer

play18:33

ptr2 is equal

play18:35

to

play18:38

null say here I'm taking two pointer

play18:40

pointer two is also null and here I'm

play18:42

checking if prtr 1 equal to equal to

play18:43

ptr2

play18:45

so this you have to print

play18:49

like both are null

play18:54

pointer otherwise you can do anything

play18:57

what you want right so obviously both

play18:59

are null so I have told you it is

play19:01

guaranteed that both would always be

play19:03

null so both are null pointer but if you

play19:06

leave it uninitialized like PTR 1 also

play19:10

and this is ptr2 I'm not going to

play19:12

initialize it and now I'm going to check

play19:16

right both are

play19:23

uninitialized otherwise we can do

play19:25

something in else part we can do

play19:26

something right so now let me run this I

play19:29

don't know what output we will

play19:32

get see it has stopped

play19:35

working why so because ptr1 is having

play19:38

any garbage value ptr2 is also having

play19:40

any garbage value we are not

play19:42

initializing these point so it is it is

play19:44

not a guarantee that that garbage value

play19:47

would be same as the garbage value in

play19:50

ptr2 right so if both are same then only

play19:54

it will print both are uninitialized if

play19:56

both are not same just in this case

play19:59

means in lse part I'm going to print I'm

play20:03

going to deference ptr1 and ptr1 is what

play20:07

we haven't initialized it and I want to

play20:10

print the value whatever the value

play20:12

whatever the address in ptr1 at that

play20:14

address what is the value but the

play20:16

address is any garbage value so maybe it

play20:19

will print anything or see it will show

play20:21

some un undefined Behavior so here in

play20:24

this case it has stopped working program

play20:26

has been crashed

play20:28

right so this can also be a

play20:32

case so uninitialized pointer is

play20:34

different from null pointer I hope now

play20:37

you got the

play20:41

difference and here also you can write

play20:43

zero that is also fine ptr1 is equal to

play20:47

zero and ptr2 also if I'm saying zero

play20:50

and if both are same then it should

play20:53

print both are null or zero so 0 0 is

play20:58

also what is considered as null in U

play21:05

C so here what it will

play21:14

print see both are

play21:17

zero right you can also write zero but

play21:21

better to write down null not zero right

play21:25

so I hope you got what is null pointer

play21:27

if you have any doubt you can ask me in

play21:29

comment box so in the next video we'll

play21:30

see dangling pointer so now I'll see you

play21:32

in the next video till then bye-bye take

play21:33

care

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Null PointerProgrammingC LanguagePointersMemory AllocationError HandlingDynamic MemoryUninitialized PointerVoid PointerDangling Pointer