C_80 Void Pointer in C | Detailed explanation with program

Jenny's Lectures CS IT
24 Aug 202118:05

Summary

TLDRThis video script delves into the concept of void pointers in C programming. It explains that a void pointer, akin to an empty glass that can be filled with any liquid, is a generic pointer without an associated data type. The script teaches how to declare and use void pointers, emphasizing the need for type casting when dereferencing to access values of different data types. The practical demonstration shows using a single void pointer to access variables of various types, highlighting its utility in simplifying code and handling dynamic memory allocation.

Takeaways

  • 😀 The video discusses special pointers in C programming, focusing on void pointers.
  • 🔍 A void pointer in C is a generic pointer that has no associated data type, similar to an empty glass that can be filled with any liquid.
  • 📚 The term 'void' generally means 'empty' or 'nothing', and a void pointer is used to point to any data type.
  • 💡 Declaring a void pointer involves using the 'void' keyword in the pointer declaration, such as `void *vp;`.
  • 🚫 A void pointer cannot be dereferenced directly; it must be typecast to a specific data type before being used.
  • 🔄 The flexibility of a void pointer allows it to be converted into any other pointer type, making it useful for various programming scenarios.
  • 💻 The video demonstrates how to use a void pointer in a program, showing how it can store the address of different data types like int, float, and char.
  • 🔑 Typecasting a void pointer is essential for accessing the value it points to, as the compiler needs to know the data type to correctly interpret the memory.
  • 📈 The practical use of void pointers is highlighted in the video, showing how they can be used to access multiple data types with a single pointer.
  • 🔍 The video also mentions that void pointers are used in dynamic memory allocation functions like malloc and calloc, which return void pointers to the allocated memory.

Q & A

  • What is a void pointer in C?

    -A void pointer in C is a pointer that has no associated data type. It is essentially a generic pointer that can point to any data type. It is declared using the 'void' keyword and can be typecast to any other pointer type.

  • Why are void pointers useful in programming?

    -Void pointers are useful because they can be used as a generic pointer to any data type. This allows a single pointer to be used to access different data types, which can be convenient in situations like dynamic memory allocation where the data type is not known at compile time.

  • How do you declare a void pointer in C?

    -A void pointer is declared in C using the 'void' keyword followed by an asterisk and the pointer name. For example, `void *vp;` declares a void pointer named 'vp'.

  • Can you directly dereference a void pointer?

    -No, you cannot directly dereference a void pointer. Before dereferencing, you must typecast the void pointer to the appropriate data type pointer (e.g., `int *`, `float *`, `char *`) to ensure the compiler knows how many bytes to access.

  • What is the significance of the term 'void' in the context of a void pointer?

    -The term 'void' in the context of a void pointer signifies that the pointer does not have an associated data type. It can be thought of as an 'empty' or 'generic' pointer that can be filled with any type of data.

  • How does a void pointer differ from other pointers like integer pointer, float pointer, or character pointer?

    -A void pointer differs from other pointers in that it does not have a specific data type associated with it. Other pointers, such as integer pointers (`int *`), float pointers (`float *`), or character pointers (`char *`), are associated with specific data types and can only point to variables of those types.

  • What is the purpose of typecasting a void pointer?

    -Typecasting a void pointer is necessary to specify the data type of the variable it points to. This is required before dereferencing the pointer so that the compiler knows how many bytes to access and can correctly interpret the data.

  • Can a void pointer store the address of any variable?

    -Yes, a void pointer can store the address of any variable, regardless of its data type. However, before accessing the data at that address, the void pointer must be typecast to the appropriate pointer type corresponding to the data type of the variable.

  • What are some common uses of void pointers in C?

    -Void pointers are commonly used in situations where the data type is not known until runtime, such as in dynamic memory allocation with functions like `malloc` and `calloc`. They are also used in generic functions or APIs where the function needs to handle data of various types.

  • How does the concept of a void pointer relate to dynamic memory allocation in C?

    -In C, functions like `malloc` and `calloc` return a void pointer. This void pointer is then typecast to the appropriate pointer type by the programmer to store and access dynamically allocated memory. This allows for flexible memory management where the data type can be specified at runtime.

Outlines

00:00

📚 Introduction to Special Pointers in C

This paragraph introduces the concept of special pointers in C programming. The speaker discusses various types of pointers such as word pointer, generic pointer, null pointer, and dangling pointer. The focus is on void pointers, explaining what they are, their purpose, and how they are used. The void pointer is described as a type of pointer that does not have an associated data type, making it versatile for various uses. The speaker also mentions that they will demonstrate the use of void pointers with practical examples on their laptop.

05:00

🔍 Understanding and Using Void Pointers

The speaker delves deeper into void pointers, explaining that they are essentially pointers without a specific data type. They use the analogy of an empty glass that can be filled with any liquid to illustrate the concept. The paragraph covers how to declare a void pointer and how it can be converted into any other type of pointer through type casting. The speaker emphasizes the need for type casting when dereferencing a void pointer, as the compiler needs to know the data type to correctly access the memory. An example is given where the void pointer is assigned the address of an integer variable, and the speaker explains the error that occurs when trying to dereference it without type casting.

10:02

🔄 Type Casting and Practical Use of Void Pointers

This paragraph discusses the practical applications of void pointers, highlighting their utility in accessing different data types with a single pointer. The speaker explains that while void pointers do not have an associated data type, they can be type cast into any other pointer type, allowing for flexibility in accessing various data types. Examples are provided where the void pointer is used to access integer, float, and character variables, demonstrating how a single void pointer can be used to handle multiple data types. The speaker also mentions the use of void pointers in dynamic memory allocation functions like malloc and calloc, which return void pointers.

15:15

💻 Demonstrating Void Pointers in Code

The speaker presents a practical demonstration of using void pointers in C code. They create a file named 'void pointer do c' and declare a single void pointer. The code example shows how the void pointer is used to access variables of different data types (integer, float, and character) by first storing their addresses and then dereferencing the pointer after type casting. The speaker runs the code to show that the values of the variables can be accessed correctly using the void pointer, emphasizing the benefit of using a single pointer to handle multiple data types.

Mindmap

Keywords

💡Pointers

Pointers are a fundamental concept in C programming, allowing programmers to manipulate memory addresses. In the video, pointers are introduced as variables that store the memory address of other variables. They are essential for dynamic memory allocation and accessing data stored at specific locations in memory.

💡Void Pointer

A void pointer in C is a generic pointer type that does not have an associated data type. It can point to any data type, making it versatile for various operations. The video explains that a void pointer is like an 'empty glass' that can be filled with any type of data, emphasizing its flexibility.

💡Data Type

Data types in programming define the kind of data a variable can hold, such as integers, floats, or characters. The video discusses how pointers, like void pointers, can be associated with different data types, which is crucial for understanding how they can be used to access and manipulate data in memory.

💡Type Casting

Type casting is the process of converting one data type to another. In the context of void pointers, type casting is necessary to specify the data type when dereferencing the pointer. The video demonstrates how to cast a void pointer to an integer pointer, a float pointer, or a character pointer to access the correct data type.

💡Dangling Pointer

A dangling pointer is a pointer that points to a memory location that has been freed or is otherwise invalid. While not extensively discussed in the script, the concept is mentioned as one of the special pointers that will be covered in later videos, indicating the importance of managing pointers carefully.

💡Null Pointer

A null pointer is a pointer that does not point to any valid memory location. It is often used to indicate the absence of a valid address. The video script mentions null pointers as a type of special pointer that will be discussed in subsequent videos, highlighting the need to understand different pointer behaviors.

💡Generic Pointer

While not explicitly defined in the script, the term 'generic pointer' could be inferred to refer to a pointer that can handle multiple data types, similar to a void pointer. The video emphasizes the versatility of void pointers, which can be seen as a form of generic pointer.

💡Memory Address

Memory addresses are unique identifiers for locations in a computer's memory where data is stored. The video script frequently refers to memory addresses when discussing pointers, as pointers store these addresses to access and manipulate the data stored at those locations.

💡Dereferencing

Dereferencing a pointer involves accessing the data stored at the memory address the pointer holds. The video explains that dereferencing a void pointer requires type casting because the compiler needs to know the data type to correctly access the appropriate amount of memory.

💡Dynamic Memory Allocation

Dynamic memory allocation refers to the process of allocating memory during runtime, typically using functions like malloc and calloc in C. The video mentions that these functions return void pointers, which can be used to access the allocated memory, demonstrating the practical application of void pointers.

💡Compiler

The compiler is a software tool that translates source code into machine code. In the context of the video, the compiler's role is highlighted in understanding how it uses data types associated with pointers to determine how much memory to access when dereferencing.

Highlights

Introduction to special pointers in C programming, including void pointer, word pointer, generic pointer, null pointer, and dangling pointer.

Void pointer is a generic pointer type that can point to any data type.

Void pointer has no associated data type, making it versatile for various uses.

Void pointer can be converted into any other pointer type through type casting.

Declaration of a void pointer involves using the 'void' keyword.

Demonstration of how to declare and use a void pointer in a program.

Explanation of why void pointers cannot be dereferenced directly.

Void pointers require type casting before dereferencing to specify the data type.

Illustration of accessing an integer variable using a void pointer by type casting it to an integer pointer.

Example of accessing a float variable using a void pointer by type casting it to a float pointer.

Demonstration of accessing a character variable using a void pointer by type casting it to a character pointer.

Void pointers are used in dynamic memory allocation functions like malloc and calloc.

Practical example of using a void pointer to access variables of different data types in a single program.

Void pointers allow for accessing multiple data types with a single pointer, reducing the need for multiple pointers.

Explanation of the practical applications and benefits of using void pointers in programming.

Void pointers are essential in scenarios where the data type is unknown or needs to be flexible.

Final demonstration of accessing and printing values of different data types using a single void pointer in a C program.

Transcripts

play00:00

so in the series of learning programming

play00:02

in c we are discussing pointers in C in

play00:04

pointers also there are some special

play00:06

pointers like word pointer generic

play00:08

pointer null pointer dangling pointer

play00:10

and these pointers are also very helpful

play00:13

right so we are going to discuss these

play00:15

pointers one by one in this video I'll

play00:17

discuss void pointer everything about

play00:19

void pointer like what is void pointer

play00:21

the need of void pointer why we use void

play00:23

pointer how to declare this pointer how

play00:25

to use this pointer right with the help

play00:27

of a program right and I'll show you

play00:29

that program also on my laptop practical

play00:31

also we'll see right everything we'll

play00:33

discuss about this pointer in this video

play00:35

and one by one by one in later videos

play00:37

you will discuss dangling pointer null

play00:38

pointer all the these special pointers

play00:40

we'll discuss right so you'll also see

play00:43

how this pointer is different from the

play00:45

other pointers we have discussed like

play00:47

integer pointer character pointer float

play00:49

pointer everything about this pointer we

play00:51

will discuss in this video every single

play00:53

detail right so now let's discuss what

play00:55

is void pointer I hope you know the

play00:56

meaning of this void in general term in

play00:59

English term like K or empty right so

play01:03

it's like if there's a glass and that is

play01:05

empty that is void you can fill that

play01:07

glass with maybe water with some other

play01:11

drink or with milk anything you can you

play01:14

know put in that glass right so apply

play01:17

the same concept here also try to relate

play01:19

that thing void pointer means this

play01:23

pointer is of void type means this

play01:26

pointer is having no Associated data

play01:29

type if you will see the definition it

play01:32

that maybe on internet or in any book

play01:35

then you must you must uh see that word

play01:39

pointer is what it is not having any

play01:42

Associated data type that is in simple

play01:45

terms that is a the same example you can

play01:48

take an empty glass you can fill that

play01:51

glass with either you know with water or

play01:54

with milk or with some other drink or

play01:55

anything right so same this pointer is

play01:59

having no Associated data type with it

play02:01

so you can convert this pointer in any

play02:04

other pointer also now whatever I am

play02:06

saying what's the meaning of that thing

play02:08

see first of all if I write a pointer

play02:10

something like this int

play02:12

star

play02:14

P one is float star FP here I'm writing

play02:19

IP or here I'm writing

play02:22

CP it means there are three pointers

play02:25

this is integer pointer see data type of

play02:28

pointer is not integer it means IP is a

play02:30

pointer which is pointing to

play02:33

a int data type or maybe this is storing

play02:37

an address of a variable whose data type

play02:39

is integer or simply we call it as in

play02:42

integer pointer or pointer to integer

play02:44

this is float pointer or you can say a

play02:46

pointer to float this is pointer to

play02:49

character right so they they are having

play02:52

their Associated data type the data type

play02:55

of this is the data type which is

play02:56

associated with this pointer is int with

play02:59

this float with this care but word

play03:02

pointer is having no Associated data

play03:04

type so how to declare this pointer see

play03:07

the synx is same obviously if it is a

play03:11

pointer you have to use EST this will

play03:12

tell that it is a pointer name here you

play03:15

will write pointer name suppose I'm

play03:18

writing pointer name word pointer that

play03:20

is WEP so what you will write here at

play03:22

the place of this data type simply we

play03:24

will write what this void this keyword

play03:28

void so this this is what a declaration

play03:30

of void pointer right I hope you

play03:35

understand what is void pointer it is it

play03:37

it is not having any Associated data

play03:38

type right so you can convert it into

play03:42

any other data type you can type cast it

play03:47

right here it means this can store

play03:50

address of a variable like if a address

play03:53

variable is in a another variable I'm

play03:56

taking float B one variable I'm taking

play03:59

care

play04:01

C so this pointer can store IP is equal

play04:05

to address of

play04:06

a only this is possible this can store

play04:10

address of a only if you write IP is

play04:12

equal to address of B this would be

play04:15

wrong and what if we can write FP is

play04:17

equal to address of B and this pointer

play04:21

CP is equal to address of

play04:24

C right but here this pointer can store

play04:28

address of

play04:31

this int variable also float variable

play04:34

also care variable

play04:36

also right so it's like a generic

play04:39

pointer same in Empty Glass we can fill

play04:43

maybe water some other drink or maybe

play04:47

milk anything we can typ cast it so we

play04:50

can typ cast this pointer for that you

play04:52

have to typ cast this pointer directly

play04:56

if you write suppose here I'm writing

play05:00

VP is equal to address of

play05:03

a it means now this word pointer is

play05:06

storing address of

play05:08

this variable a and suppose in a I'm

play05:11

having a is equal to 5 but if you will

play05:15

print like print F percentage D and

play05:20

whatever in a I want to print in a I

play05:23

have five and this WEP this

play05:27

pointer this is pointer

play05:30

this is pointing to a suppose address is

play05:33

th000 so in VP we have address of a that

play05:35

is

play05:37

1,000 so if you want to access this

play05:40

using this wordid pointer what you will

play05:41

write D reference you will D reference

play05:44

this pointer I hope you know this

play05:46

because we have discussed these Concepts

play05:49

I know uh in our previous videos of

play05:51

pointers right it means estri VP value

play05:57

at this address here we have th so so it

play05:59

should give

play06:00

five but it is wrong you cannot

play06:03

dreference aoid pointer it will give you

play06:08

error first of all you have to typ cast

play06:12

it because see when you deference then

play06:18

it will see suppose I'm D referencing

play06:20

this IP I'm writing here IP IP is equal

play06:24

to address of a suppose this is not VP

play06:26

this is IP this pointer right

play06:30

and EST IP so it will give you five this

play06:33

is correct because this is integer

play06:35

pointer this is not word pointer so now

play06:38

when you dreference it it means compiler

play06:42

now know add the data type of this

play06:45

pointer the associated data type with

play06:46

this pointer is integer means now

play06:49

compiler know in modern compiler

play06:53

obviously it depends on machine to

play06:54

machine but I take like in modern

play06:56

compiler integer take four bytes so now

play06:59

compiler

play07:00

[Music]

play07:02

know 1 2 3 4 suppose in memory within

play07:05

these four bytes this a the value has

play07:08

been stored five converted into binary

play07:11

form like 32 bits and then this five has

play07:14

been stored here and address of first

play07:17

bite is 1,000 then 1,1 1, and2 1,

play07:21

and3 something like this right so

play07:27

now value at this address address is

play07:30

th000 address is th000 so now from here

play07:33

we want to access now Associated data

play07:36

type is integer so when you apply D

play07:38

referencing now the compiler know

play07:41

that he has to access four bytes 1 2 34

play07:46

because data type is

play07:48

integer that is why the data type is

play07:51

needed when you dreference that pointer

play07:54

right so now it will access four

play07:57

bytes and it will give you

play08:00

value same if you access D reference

play08:03

this character pointer character will

play08:05

take one bite so now compiler know by

play08:08

seeing this data type compiler will know

play08:10

that he has to access only one by

play08:14

because dat type is

play08:16

character right same float if float is

play08:19

there means four bytes so it has to

play08:21

access four bytes but here we are using

play08:24

suppose

play08:25

VP now word is what it is not a data

play08:28

type now how compiler will know

play08:31

that it has to access one byte or 4 byte

play08:34

or how many bytes compiler doesn't know

play08:38

so how to access this void pointer how

play08:40

to dreference this void pointer how to

play08:42

get the

play08:44

value right value may be within one by

play08:47

or value may be in four bytes don't know

play08:50

it is void pointer right that is why we

play08:53

we cannot dreference this word pointer I

play08:55

hope you got the reason why you cannot

play08:57

dreference this word pointer right if

play08:59

not then you can ask me in comment box

play09:01

maybe in maybe in another video I can

play09:04

tell you the process in a separate video

play09:06

right so now that is why it will give

play09:09

error so first of all we will have to

play09:11

typ cast this

play09:13

one typ cast means now I'm storing in VP

play09:17

address of a so here we have VP I'm

play09:20

storing address of a and data type of

play09:23

this is what integer so we have to typ

play09:27

cast it into integer pointer so now how

play09:31

to type cast it here rather than using

play09:33

this what you will

play09:36

write int

play09:39

estri VP so first this word pointer

play09:44

would be typ casted or you can say

play09:46

converted into an integer pointer and

play09:49

now we apply this D referencing

play09:52

operator right now it will give you what

play09:55

value

play09:57

five because now now we have converted

play10:00

into int or we you can say typ casted

play10:02

into integer pointer so now compiler

play10:04

know if int then it has to access four

play10:07

bytes so data would be in four bytes so

play10:11

starting by is this so it will access

play10:13

1,00,000

play10:16

1,2,3 till 103 4 bytes and obviously

play10:20

this is integer type and this would be

play10:22

stored in four bytes only so definitely

play10:23

you will get five

play10:26

right now if I write if I write

play10:29

something like this VP is equal to

play10:31

address

play10:32

of CP uh sorry address of uh

play10:38

C I'm storing VP this address variable

play10:44

is what character type right so here in

play10:47

C suppose I'm storing character is uh

play10:51

zero yeah 0 is character it's not like

play10:54

that AB BCD is only character whatever

play10:55

you will write in single code that will

play10:58

be in character A Single Character you

play10:59

can write so if you write zero here so

play11:01

zero would be considered as a character

play11:04

right so now address of C now VP is

play11:08

suppose here we have this is C here we

play11:12

have zero obviously in that form like in

play11:16

uh Zer and one form that would be stored

play11:19

right and now VP this is a pointer and

play11:23

this is pointing to here suppose address

play11:26

is 2,000 so it we are storing 2000

play11:30

now now how to

play11:33

print how to print this is character we

play11:36

know this is character so we'll use

play11:37

percentage C estri first type

play11:42

cast type cast into a character pointer

play11:46

so we'll write here care s and now name

play11:50

is

play11:52

VP that's it first of all it will be typ

play11:55

casted into character pointer and then

play11:58

we'll der reference it so D reference

play12:00

means value at this point value is what

play12:03

zero so it will give zero I hope you got

play12:06

this and if you write here VP is equal

play12:09

to address of float variable so before

play12:12

printing type cast into float and that

play12:14

it will print floating value suppose in

play12:17

B I have 1.11 so it will print

play12:20

1.11

play12:22

right so simple point about word pointer

play12:26

there is nothing much about word pointer

play12:27

it is not having an data type you can

play12:30

convert it into any other pointer or you

play12:32

can typ cast it into any other pointer

play12:35

right so now what's the use maybe you

play12:38

think that rather than typ casting

play12:40

obviously ultimately we have to typ cast

play12:42

it so rather than this directly use an

play12:45

INT pointer and directly deference that

play12:48

pointer what's the use of using this

play12:50

void pointer so here if in the same

play12:53

program same program if I'm using this

play12:57

int also float also so care

play12:59

also so we have to declare three

play13:02

pointers one is for INT one is for float

play13:05

one is for

play13:06

character but if you use this word

play13:08

pointer no need to declare these

play13:12

pointers you can access all these three

play13:15

values using single pointer word pointer

play13:18

only a single pointer how I know I I I

play13:23

hope you know the

play13:24

answer first store the address of a type

play13:28

card it and print then assign in this

play13:33

address of B while printing type cast it

play13:37

then assign address of C while printing

play13:39

type cast it and you can print you can

play13:41

access all the three values three values

play13:44

of different different data

play13:46

types so here we are using only one

play13:48

pointer we are using only one pointer

play13:50

and we are accessing values of three you

play13:53

can say data types integer also float

play13:56

also car also one more thing when we use

play14:00

Melo and

play14:01

kello we'll also discuss these

play14:04

things these are built-in function for

play14:06

dynamic memory allocation so they return

play14:10

what void

play14:14

pointer right when they assign they

play14:17

return memory like they return word

play14:19

pointer to that memory so in that memory

play14:21

we can store any uh value like integer

play14:24

also float also character also right so

play14:28

there also these void pointers would be

play14:30

used right in Malo and

play14:33

kog so that also discuss when we discuss

play14:35

dynamic memory location these are

play14:37

Advanced you know Concepts and see right

play14:41

so that is why we use I guess uh word

play14:43

pointer you you are clear with what is

play14:44

word pointer why we use word pointer

play14:47

like and you can say what's the need of

play14:49

word pointer now let me show you know

play14:52

practically we'll write down some code

play14:54

and we'll see use of word pointer right

play14:57

so now let me create a file here

play15:01

I'm taking name void pointer do c

play15:15

right and I'm taking only one pointer

play15:18

like void estri

play15:20

VP right and I'm taking in a is equal to

play15:24

5 and one more thing that is float

play15:29

suppose I'm taking float B is equal to

play15:32

1.56 and

play15:34

care care C at H is equal to uh anything

play15:39

you can take like I'm taking

play15:42

C right and now I'll access all the

play15:45

three variables using one pointer only

play15:47

so I'm first of all I'm writing VP is

play15:50

equal to address of a and now I want to

play15:55

dreference

play15:56

this a is equal to

play15:59

we are printing value of a and see if

play16:02

you der reference like this s will be

play16:06

simple so it will give you error let me

play16:09

run

play16:11

this here we it is giving error like d

play16:13

referencing word pointer and invalid use

play16:16

of word expression so we cannot this

play16:19

deference this word pointer something

play16:20

like this now we have to type cast it so

play16:24

I'm typ casting it into integer pointer

play16:27

first

play16:29

and see now let me run this and it will

play16:31

print a is equal to

play16:33

5

play16:37

see see it is printing the value a is

play16:39

equal to 5 right now I want to access

play16:44

that variable float so in VP I'm storing

play16:47

address of B now so now this word

play16:50

pointer is

play16:52

pointing

play16:53

what float variable so now B value is

play16:57

equal to percentage

play17:00

F and here I'm D referencing this now so

play17:04

we have to convert it into

play17:07

float we have to type cast it into float

play17:10

see let me run this and it will give you

play17:13

a equal to uh sorry this a is equal 5 b

play17:16

is equal to 1.5

play17:18

6 right and now same address of

play17:25

CH and let me print

play17:29

C = to percentage

play17:33

C and uh

play17:36

estri we now we have to type cast into

play17:42

care

play17:44

right and now let me print this sorry

play17:47

run this see it is printing a b c uh CH

play17:51

value all the three values so we are

play17:52

accessing all the three data types

play17:55

variables with a single pointer

play17:57

only so that is benefit of using word

play18:00

pointer that is why programmers use this

play18:02

word pointer right

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
C ProgrammingPointersVoid PointerData TypesType CastingMemory AddressDynamic MemoryProgramming TutorialCode ExamplesEducational Content
هل تحتاج إلى تلخيص باللغة الإنجليزية؟