Dereferencing in C
Summary
TLDRThis video script delves into the concepts of referencing and dereferencing operators, as well as the array subscript operator in C programming. It explains how to create a pointer to a variable, use the dereference operator to access and modify the value at a memory address, and clarifies the distinction between the dereference operator and the type definition. The script also explores the array subscript operator, demonstrating its equivalence to pointer arithmetic and the peculiarities of its commutative property. The tutorial aims to demystify pointers and their operations for viewers, encouraging questions and further discussion.
Takeaways
- 📌 The dereferencing operator is used to access the value at the memory address a pointer is referencing, as opposed to the address itself.
- 🔍 To dereference a pointer in C, you use the * operator, which tells the compiler to get the value stored at the location the pointer is pointing to.
- 💡 The address-of operator (&) is used to obtain the memory address of a variable, which can then be assigned to a pointer.
- 👉 When defining a pointer, the * symbol is part of the type declaration, indicating that the variable is a pointer to the specified type.
- 🛠 The dereference operator can also be used to modify the value at the memory address a pointer is pointing to, by assigning a new value to it.
- 🚫 Attempting to dereference a pointer that points to an invalid or uninitialized address can lead to a runtime error, such as a read access violation.
- 🔄 The array subscript operator [] is essentially a shorthand for pointer arithmetic and dereferencing, allowing for easy access to array elements.
- 🔢 In C, arrays decay into pointers to their first element, which means you can use pointer arithmetic to access other elements in the array.
- 🤔 The commutative property of addition can lead to some unusual expressions, like 1 + array, which may seem strange but is valid in C due to how the array subscript operator works.
- 📚 Understanding the difference between l-values and r-values in C is important when working with pointers and dereferencing, as it affects what operations can be performed.
- 🗣️ The video encourages viewers to ask questions about pointers or any related topics on the comments section or the discord server for further clarification.
Q & A
What is the purpose of the dereference operator in C?
-The dereference operator in C is used to access the value stored at a memory address pointed to by a pointer. It allows you to get the value at that address rather than the address itself.
How do you create a pointer to a variable in C?
-You create a pointer to a variable in C by declaring a pointer of the same type as the variable and then assigning it the address of the variable using the address-of operator (&).
What is the difference between the dereference operator (*) and the type specifier (*) when defining a pointer?
-The dereference operator (*) is used to access the value at the memory address a pointer is pointing to. The type specifier (*) when defining a pointer indicates the type of data the pointer points to, such as an int pointer.
How can you print the value of a variable using its pointer in C?
-You can print the value of a variable using its pointer by using the dereference operator (*) with the printf function and the appropriate format specifier, such as %d for integers.
What happens if you try to dereference a pointer without first assigning it to a valid address?
-Attempting to dereference a pointer without first assigning it to a valid address can result in undefined behavior, which often leads to a runtime error such as a segmentation fault or access violation.
How can you change the value at the memory address pointed to by a pointer?
-You can change the value at the memory address pointed to by a pointer by using the dereference operator (*) and assigning a new value to it, like so: *pointer_name = new_value;
What is the relationship between the array subscript operator and pointers in C?
-The array subscript operator in C is essentially a shorthand for adding an offset to a pointer and then dereferencing it. It decays the array into a pointer to its first element and allows access to elements at specific offsets.
Why does the expression '1 + pointer' sometimes yield the same result as 'pointer + 1' in C?
-The expression '1 + pointer' and 'pointer + 1' can yield the same result in C because addition is commutative. The array subscript operator adds the index to the pointer and then dereferences it, which is the same as what happens when you add 1 to the pointer first and then dereference.
What is the significance of the term 'L-values' in the context of pointers and dereferencing in C?
-In C, 'L-values' refer to expressions that can appear on the left side of an assignment. When discussing pointers, an L-value is necessary to use the dereference operator to modify the value at the memory address pointed to by the pointer.
Can you use the dereference operator to access the first element of an array without using the array subscript operator?
-Yes, you can use the dereference operator to access the first element of an array by dereferencing the pointer to the array, which is equivalent to using the array subscript operator with index 0.
What is the potential issue with using '1 + pointer' instead of 'pointer + 1' in code?
-While '1 + pointer' and 'pointer + 1' can yield the same result due to the commutative property of addition, using '1 + pointer' can be less clear and may lead to confusion or errors, especially when the context is not immediately obvious.
Outlines
🔍 Understanding Pointers and Dereferencing
This paragraph introduces the concept of pointers and dereferencing in programming. It explains that to dereference a pointer, one must first have a pointer, which is created by taking the address of a variable. The paragraph demonstrates how to print the value of a variable using a pointer and the dereference operator. It clarifies the difference between the dereference operator and the pointer type declaration, and shows how to use the dereference operator to both read and modify the value at a memory address. The importance of distinguishing between l-values and r-values in C is also mentioned.
📚 Array Subscripting and Pointer Arithmetic
The second paragraph delves into the array subscript operator and its relationship with pointer arithmetic. It illustrates how accessing elements in an array is akin to pointer arithmetic, where the array identifier decays into a pointer to the first element, and how adding an offset to this pointer allows access to subsequent elements. The paragraph also highlights the shorthand nature of the array subscript operator, which is essentially pointer addition followed by dereferencing. It discusses the commutative property of addition in the context of pointers and arrays, and how it leads to some unusual but valid expressions in C.
🗣️ Closing Remarks and Invitation for Interaction
In the final paragraph, the speaker wraps up the video with a summary of the key points covered and invites viewers to engage with them for further questions or discussions about pointers. They provide information on where and how to ask questions, either in the comments section or on a Discord server, and assure that they are frequently online to respond promptly. The speaker bids farewell to the viewers, expressing gratitude for their time and attention.
Mindmap
Keywords
💡Dereferencing Operator
💡Pointer
💡Address-of Operator
💡Array
💡Array Subscript Operator
💡Pointer Arithmetic
💡L-values and R-values
💡Memory Address
💡Type Casting
💡Access Violation
Highlights
Introduction to the concept of referencing and dereferencing operators and the array subscript operator in C programming.
Explanation of how to create a pointer to a variable and the use of the address-of operator.
Clarification of the difference between a pointer's type and the address it holds.
Demonstration of using the dereference operator (*) to access the value a pointer points to.
Illustration of the mistake of printing a pointer's address instead of its value.
How to correct the mistake by using the dereference operator to print the actual value.
The ability to use the dereference operator to set the value at a specific memory address.
Discussion on the importance of understanding l-values and r-values in C.
Explanation of the array subscript operator as a shorthand for pointer arithmetic and dereferencing.
How the array identifier decays into a pointer to the first element of the array.
Pointer arithmetic to access elements at different offsets in an array.
The equivalence of array subscript notation and pointer arithmetic with dereferencing.
The peculiarity of the commutative property of addition affecting pointer arithmetic.
The practical implications of using 1 + array versus array + 1 in C.
Highlighting the importance of not confusing the dereference operator with the type declaration.
The potential for misuse of pointer arithmetic and the risks of invalid memory access.
Invitation for viewers to ask questions and engage with the presenter on Discord for live support.
Transcripts
in today's video we'll take a look at
the referencing the dereferencing
operator and the erase subscript
operator as well so let's start off with
the referencing what does it mean so so
to be able to dereference a pointer we
first have to have a pointer right so
let's start with that here I have a
defined this simple variable and I want
a pointer to it so I'm going to say here
and define here a pointer another int
pointer call it P and that's going to be
equal to the address of a I'm just going
to point to a but we know that this is
the type of our variable P right it's an
int pointer and we know that a is of
type int and the address of a that's
what this operator represents is of type
pointer to int so now that we have that
let's try to work with it so first
things first I try to print the value of
a on the screen using P how do we do
that well we get here a printf simple
percent Li because we are working with
signed integers then all we have to do
is say P right so if I try to run this
I'm going to get let's see it doesn't
seem right does it
hmm no no because a 16 it's more getting
here a very small number what's the
issue well the issue is that P here in
this context P is actually on address so
what we're printing here is the address
if the value of this pointer and the
value of this pointer is the address of
a right so what we were seeing there is
the address inside our memory for the
variable a now what we want to do is
actually get the value from there so so
how can we tell the language that what
we want is not just the address we want
the value at that address
simple enough using the dereference
operator like so so now what c is going
to do is going to find
P going to look at what address is
stolen there well the actual value the
stored in there is the address of a okay
so go there go there in memory and find
me an integral and it's going to go
there and return the value of a all
right so now if I try to run this you'll
notice I get 16 right so this is how the
dereference operator works right it
takes a memory address goes there
dereferences it looks at that memory
itself what value it stores and returns
back that value right so this is what we
did here not to be confused with this
asterisk there this guy is part of the
type part of the type of P here when
defining it so really this is completely
different from this right so try to do
not get these two confused with each
other the cool part is you can use the
same structure this asterisk P this
dereference p structure to set the value
at that memory address simply by saying
dear friends p equals let's say here 17
but if i try to run this now i'm going
to get 17 because simple with
dereference p here so now we have the
actual value and we are setting it to 17
but you're gonna have to look more into
the L values and our values inside C but
that's basically the gist of it if I
want to do just P equals 17 that would
set the actual address that P is
pointing to so here we're setting it to
the others away and then here we would
be setting it to the address 17 which is
probably an invalid address so if I were
to run this you'll notice I get read
access violation which makes sense
because at that a very low added
we already already read protected by the
operating system right so you cannot you
cannot actually read address at such low
values right so that's why we are using
the dereference operator here so this is
again the dereference operator this is
the dereference operator this is not the
direction of operator now a second
operator is the array subscript operator
which also is in dereferencing values so
let's first initialize here an array so
now that I have an array let's say I
want to print the second value set
already what do we do that's simple
enough we just do printf and % D because
it's an integral and we do array of one
right so if I try to run this I'm going
to get 13 on the screen you know that in
most cases this array identifier
actually decays into a pointer a pointer
to the beginning of the array to
actually the first element of the array
we also know that if we add to the top
pointer we're really just offsetting
that pointer that pointers memory value
by the number that you add times the
size of the type that it's pointing to
so in our case really to access 13 what
we would have to do is for example get a
get array right and simply add the value
1 to it since array is going to decay to
an to an int pointer and if we add one
we actually add 1 times size of int if
we dereference this thing we're actually
going to get on the number 13 so we can
try it
I'm just array 1 we have to dereference
this whole thing if I try to run this
you'll notice I still get 13 mm-hmm so
really this array subscript operator is
just a shorthand for that operation of
adding and then dereferencing right so
and really inside the specific
patient's themselves this or a subscript
operator that's basically all it does it
adds those two and any differences no so
this is why you would see some people
use for example here if I were to
actually want to print the first element
of the array just array of zero I would
do that right so that's alright zero
that's five that's fine and dandy but
here's why you would see some people use
dereference of ARR over a I try to run
this I'm gonna get still five that's
because array of zero is actually array
plus zero dereferenced but really or a
plus zero is still array so we can just
get rid of this and then get rid of
parentheses so that's why it's the same
exact result right now there's a weird
side effect due to the fact that the
array subscript operator simply adds
those two values and that is well you
can have for example I can have here if
I want the second element I can say
right array plus 1 and then dereference
the whole thing right that we know that
it works nice thirteen but we can also
say 1 plus already right it's the same
thing I mean it's just an addition but
here's where the weird part comes in so
we know that array plus 1 is equivalent
to array of 1 but 1 plus array
dereferenced is equivalent what one of
array huh it doesn't seem right
like what are you doing are you just
like what is it if one a pointer and
then you're trying to get the wave
element inside one that doesn't make any
sense does it
but when we try to run this we get 13
without an issue as you can see here so
that's just due to the fact that
addition is commutative but you can have
a plus B you can have B plus a the same
thing we we can have array plus 1 you
can have one plus already in our case
this is really 1 plus array 0
so this is why this type of thing
actually works
it's strange of course but no worries it
does do the job all right so to recap
first things first the dereferencing
operator is not the guy that you see
near the type when defining a variable
this is not the direction anything
operator this is a whole other thing
that denotes a pointer right that we're
defining a pointer here and that's it
second this notation right array of one
is equivalent to the referencing of
array plus one it's basically the same
thing inside a compiler it boils down to
just that it's just easier to write it
like so okay so keep that in mind and
because of that the third thing that's
why one overlay also works which is a
bit strange granted I'm not going to see
anybody do it in a production code or
I'm not gonna even allow it but it's
possible if you truly want to hack the
system right so I hope you got something
out of this video thank you guys so much
for watching if you do have any
questions regarding this or pointers in
general do leave them down the comments
below or on our discord server you can
join there I'm online most of the time
so you can ask questions live but I'm
going to probably respond very very fast
all right so take care and see you guys
next time bye
Weitere ähnliche Videos ansehen
C_71 Pointers in C - part 1| Introduction to pointers in C | C Programming Tutorials
C_76 Pointers in C- part 6 | Pointer Arithmetic (Subtraction) with program
C_73 Pointers in C- part 3 | Pointer Assignment
C_75 Pointers in C-part 5 | Pointer Arithmetic (Addition) with program
C_80 Void Pointer in C | Detailed explanation with program
std::move and the Move Assignment Operator in C++
5.0 / 5 (0 votes)