Dereferencing in C

CodeVault
4 Sept 201910:29

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

00:00

🔍 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.

05:02

📚 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.

10:03

🗣️ 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

The dereferencing operator, represented by an asterisk (*), is used in programming to access the value stored at a memory address pointed to by a pointer. In the video, it is explained as the operator that allows programmers to get the value from the address stored in a pointer, such as '*p' to get the value of 'a' when 'p' points to 'a'. It is crucial for working with pointers in C, as it demonstrates how to read and modify the value at a specific memory location.

💡Pointer

A pointer in programming is a variable that stores the memory address of another variable. The video script introduces the concept of pointers by defining an 'int pointer' named 'p' that points to the address of a variable 'a'. Pointers are essential for low-level memory manipulation and are a fundamental concept in the C programming language.

💡Address-of Operator

The address-of operator, denoted by the ampersand (&), is used to obtain the memory address of a variable. In the script, it is used to assign the address of variable 'a' to the pointer 'p', as in 'p = &a'. Understanding the address-of operator is key to working with pointers, as it allows the programmer to link a pointer to a specific variable's location in memory.

💡Array

An array is a data structure that consists of a collection of elements, each identified by an index. In the video, an array is initialized and used to demonstrate how the array subscript operator works, allowing access to specific elements like 'array[1]' to get the second element. Arrays are a fundamental concept in programming, allowing for the storage and manipulation of multiple items of the same type.

💡Array Subscript Operator

The array subscript operator, represented by square brackets ([]), is used to access elements of an array using their index. The script explains that 'array[1]' is a shorthand for '*(array + 1)', which demonstrates the equivalence between the array subscript and pointer arithmetic. This operator is vital for accessing and manipulating array elements in C.

💡Pointer Arithmetic

Pointer arithmetic involves performing mathematical operations on pointers to access different elements in an array or block of memory. The video script illustrates this with 'array + 1', which points to the second element of the array. It is a way to navigate through memory by adding or subtracting the size of the data type that the pointer is pointing to.

💡L-values and R-values

L-values (locator values) and R-values (result values) are terms used in C to distinguish between expressions that can be assigned to (L-values) and those that are the result of an expression (R-values). The script briefly mentions the need to understand these concepts when working with pointers and dereferencing, as they affect how values are stored and retrieved in memory.

💡Memory Address

A memory address is the location in a computer's memory where a particular piece of data is stored. The video script uses the term to explain how pointers hold the memory address of a variable, and how the dereferencing operator is used to access the value stored at that address. Understanding memory addresses is fundamental to grasping how pointers work.

💡Type Casting

Type casting in programming is the process of converting one data type to another. Although not explicitly mentioned in the script, the concept is implied when discussing pointers and dereferencing, as the dereferenced value must be of the correct type to be used properly. Type casting ensures that the value retrieved from a memory address is interpreted as the intended data type.

💡Access Violation

An access violation, also known as a segmentation fault, occurs when a program tries to read or write in a memory space that it is not allowed to access. In the script, an example is given where assigning 'p = 17' leads to an access violation because '17' is not a valid memory address. This term is important for understanding the consequences of improper pointer usage.

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

play00:00

in today's video we'll take a look at

play00:01

the referencing the dereferencing

play00:03

operator and the erase subscript

play00:06

operator as well so let's start off with

play00:09

the referencing what does it mean so so

play00:11

to be able to dereference a pointer we

play00:14

first have to have a pointer right so

play00:17

let's start with that here I have a

play00:19

defined this simple variable and I want

play00:20

a pointer to it so I'm going to say here

play00:22

and define here a pointer another int

play00:24

pointer call it P and that's going to be

play00:27

equal to the address of a I'm just going

play00:30

to point to a but we know that this is

play00:33

the type of our variable P right it's an

play00:35

int pointer and we know that a is of

play00:39

type int and the address of a that's

play00:42

what this operator represents is of type

play00:45

pointer to int so now that we have that

play00:50

let's try to work with it so first

play00:54

things first I try to print the value of

play00:57

a on the screen using P how do we do

play01:00

that well we get here a printf simple

play01:03

percent Li because we are working with

play01:05

signed integers then all we have to do

play01:09

is say P right so if I try to run this

play01:13

I'm going to get let's see it doesn't

play01:17

seem right does it

play01:17

hmm no no because a 16 it's more getting

play01:22

here a very small number what's the

play01:25

issue well the issue is that P here in

play01:28

this context P is actually on address so

play01:31

what we're printing here is the address

play01:33

if the value of this pointer and the

play01:37

value of this pointer is the address of

play01:39

a right so what we were seeing there is

play01:42

the address inside our memory for the

play01:48

variable a now what we want to do is

play01:51

actually get the value from there so so

play01:55

how can we tell the language that what

play01:57

we want is not just the address we want

play02:00

the value at that address

play02:03

simple enough using the dereference

play02:05

operator like so so now what c is going

play02:10

to do is going to find

play02:13

P going to look at what address is

play02:15

stolen there well the actual value the

play02:19

stored in there is the address of a okay

play02:23

so go there go there in memory and find

play02:26

me an integral and it's going to go

play02:29

there and return the value of a all

play02:33

right so now if I try to run this you'll

play02:34

notice I get 16 right so this is how the

play02:37

dereference operator works right it

play02:39

takes a memory address goes there

play02:43

dereferences it looks at that memory

play02:46

itself what value it stores and returns

play02:51

back that value right so this is what we

play02:55

did here not to be confused with this

play02:58

asterisk there this guy is part of the

play03:02

type part of the type of P here when

play03:07

defining it so really this is completely

play03:13

different from this right so try to do

play03:15

not get these two confused with each

play03:17

other the cool part is you can use the

play03:20

same structure this asterisk P this

play03:23

dereference p structure to set the value

play03:26

at that memory address simply by saying

play03:30

dear friends p equals let's say here 17

play03:34

but if i try to run this now i'm going

play03:37

to get 17 because simple with

play03:40

dereference p here so now we have the

play03:43

actual value and we are setting it to 17

play03:49

but you're gonna have to look more into

play03:51

the L values and our values inside C but

play03:55

that's basically the gist of it if I

play03:57

want to do just P equals 17 that would

play03:59

set the actual address that P is

play04:03

pointing to so here we're setting it to

play04:06

the others away and then here we would

play04:09

be setting it to the address 17 which is

play04:12

probably an invalid address so if I were

play04:14

to run this you'll notice I get read

play04:19

access violation which makes sense

play04:21

because at that a very low added

play04:26

we already already read protected by the

play04:29

operating system right so you cannot you

play04:31

cannot actually read address at such low

play04:35

values right so that's why we are using

play04:40

the dereference operator here so this is

play04:42

again the dereference operator this is

play04:44

the dereference operator this is not the

play04:47

direction of operator now a second

play04:49

operator is the array subscript operator

play04:52

which also is in dereferencing values so

play04:56

let's first initialize here an array so

play05:01

now that I have an array let's say I

play05:03

want to print the second value set

play05:06

already what do we do that's simple

play05:08

enough we just do printf and % D because

play05:11

it's an integral and we do array of one

play05:14

right so if I try to run this I'm going

play05:18

to get 13 on the screen you know that in

play05:21

most cases this array identifier

play05:23

actually decays into a pointer a pointer

play05:25

to the beginning of the array to

play05:28

actually the first element of the array

play05:30

we also know that if we add to the top

play05:33

pointer we're really just offsetting

play05:36

that pointer that pointers memory value

play05:40

by the number that you add times the

play05:43

size of the type that it's pointing to

play05:47

so in our case really to access 13 what

play05:51

we would have to do is for example get a

play05:54

get array right and simply add the value

play05:59

1 to it since array is going to decay to

play06:04

an to an int pointer and if we add one

play06:07

we actually add 1 times size of int if

play06:11

we dereference this thing we're actually

play06:13

going to get on the number 13 so we can

play06:16

try it

play06:17

I'm just array 1 we have to dereference

play06:20

this whole thing if I try to run this

play06:23

you'll notice I still get 13 mm-hmm so

play06:27

really this array subscript operator is

play06:31

just a shorthand for that operation of

play06:34

adding and then dereferencing right so

play06:37

and really inside the specific

play06:40

patient's themselves this or a subscript

play06:42

operator that's basically all it does it

play06:45

adds those two and any differences no so

play06:48

this is why you would see some people

play06:50

use for example here if I were to

play06:53

actually want to print the first element

play06:54

of the array just array of zero I would

play06:57

do that right so that's alright zero

play06:58

that's five that's fine and dandy but

play07:00

here's why you would see some people use

play07:03

dereference of ARR over a I try to run

play07:08

this I'm gonna get still five that's

play07:11

because array of zero is actually array

play07:17

plus zero dereferenced but really or a

play07:22

plus zero is still array so we can just

play07:23

get rid of this and then get rid of

play07:26

parentheses so that's why it's the same

play07:28

exact result right now there's a weird

play07:32

side effect due to the fact that the

play07:36

array subscript operator simply adds

play07:37

those two values and that is well you

play07:41

can have for example I can have here if

play07:43

I want the second element I can say

play07:45

right array plus 1 and then dereference

play07:48

the whole thing right that we know that

play07:50

it works nice thirteen but we can also

play07:54

say 1 plus already right it's the same

play07:58

thing I mean it's just an addition but

play08:01

here's where the weird part comes in so

play08:03

we know that array plus 1 is equivalent

play08:06

to array of 1 but 1 plus array

play08:10

dereferenced is equivalent what one of

play08:13

array huh it doesn't seem right

play08:18

like what are you doing are you just

play08:20

like what is it if one a pointer and

play08:23

then you're trying to get the wave

play08:27

element inside one that doesn't make any

play08:29

sense does it

play08:30

but when we try to run this we get 13

play08:35

without an issue as you can see here so

play08:39

that's just due to the fact that

play08:41

addition is commutative but you can have

play08:44

a plus B you can have B plus a the same

play08:47

thing we we can have array plus 1 you

play08:49

can have one plus already in our case

play08:51

this is really 1 plus array 0

play08:54

so this is why this type of thing

play08:57

actually works

play08:58

it's strange of course but no worries it

play09:03

does do the job all right so to recap

play09:05

first things first the dereferencing

play09:08

operator is not the guy that you see

play09:10

near the type when defining a variable

play09:13

this is not the direction anything

play09:15

operator this is a whole other thing

play09:17

that denotes a pointer right that we're

play09:19

defining a pointer here and that's it

play09:22

second this notation right array of one

play09:27

is equivalent to the referencing of

play09:32

array plus one it's basically the same

play09:36

thing inside a compiler it boils down to

play09:38

just that it's just easier to write it

play09:40

like so okay so keep that in mind and

play09:44

because of that the third thing that's

play09:47

why one overlay also works which is a

play09:50

bit strange granted I'm not going to see

play09:53

anybody do it in a production code or

play09:57

I'm not gonna even allow it but it's

play09:59

possible if you truly want to hack the

play10:02

system right so I hope you got something

play10:04

out of this video thank you guys so much

play10:06

for watching if you do have any

play10:07

questions regarding this or pointers in

play10:10

general do leave them down the comments

play10:12

below or on our discord server you can

play10:14

join there I'm online most of the time

play10:17

so you can ask questions live but I'm

play10:19

going to probably respond very very fast

play10:22

all right so take care and see you guys

play10:26

next time bye

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
C ProgrammingPointersDereferencingArray SubscriptMemory AddressCode ExamplesProgramming TutorialData TypesOperator UsageTechnical LearningC Language
Benötigen Sie eine Zusammenfassung auf Englisch?