https://www.youtube.com/watch?v=zuegQmMdy8Mhttps://www.youtube.com/watch?v=zuegQmMdy8M&t=610s

Written by:

untilhamza - Overview

Full Stack Software Engineer. Passionate about leveraging my diverse backgrounds to solve challenging problems while creating delightful digital experiences - untilhamza

github.com

introduction to pointers.

pointers in c++ code academy-3

blocks in computer memory are labelled

if you declare var A, the computer will allocate memory for that variable based on its type.

For a var A, which is an integer, a computer may allocate 4 bytes based on its architecture

pointers in c++ code academy-7

the achitecture of the computer determines how many bytes are given for an interger, a char or a float, or any data type supported in the language.

for a typical achitecture

int4 bytes
float4 bytes
char1 byte
double8 bytes

pointers in c++ code academy-12

a computer will allocate memory blocks starting at 204 (just an example memory address) to variable A if you declare it in your program.so

  • so even before you store data at the variable, it has reserved memory based on its type
pointers in c++ code academy-15

the address given to the variable will be stored in a look-up table. (this is a kind of memo so that the compiler can know where each variable is kept at)

So it will always check this look-up table, to get the address of A, then go there to read its data

pointers in c++ code academy-18

if you store data in var A, it has to look up its address in the look-up table, then write you data, here the number "5" into that address of variable A

pointers in c++ code academy-20

if you increment A, with a++

it find address of A from the look up table

it then increments the value of the integer stored at A from 5 to 6

pointers in c++ code academy-24

Pointers are just variables that store the address of other variables. (A pointer is also a variable like an integer so it exists in memory at some address)

pointers in c++ code academy-27
pointers in c++ code academy-28

we have another variable p (a pointer)

this has now stored the address of integer variable A

pointers in c++ code academy-31

we can run some operator on pointer P (which has the address of A) and reach A

pointers in c++ code academy-33

P is stored at address 64 in this example.

P is also just a variable at an address but it just happens to be a pointer

pointers in c++ code academy-36

now we have changed P to point at the next variable in memory, here variable B

pointers in c++ code academy-38

we usually declare a normal variable using its type like this integer A

pointers in c++ code academy-40

to declare a pointer, we have to put an * before the name of the poitner variable we are making

* is like something at says -> pointing to

so here we are making a pointer that points to an integer ( int *)

pointers in c++ code academy-44

P is now a pointer var that points to an int

it is a variable, that can store the address of an integer

pointers in c++ code academy-47

to store the address of A into pointer P

we get it using the & character

now we have stored the address of A into the pointer P

pointers in c++ code academy-51

here we have saved the value 5 to integer variable A

pointers in c++ code academy-53

if we print pointer P

it prints the address of variable A (204 address location)

pointers in c++ code academy-56

if we print &A, we still print the address of interger variable A

pointers in c++ code academy-58
pointers in c++ code academy-59

if we put * infront of a pointer -> like (* pointer ) we get the value at the address it is pointing to

pointers in c++ code academy-61

we are derefrencing because we have stored an address at P and now we are getting the value at the address

  1. store the address of A at P (by int *P = & A)
  1. access the data stored at A using P (by derefrencing P using * P syntax)

(* P) can be read as value at address stored in P

or simply as value at p

pointers in c++ code academy-68

we can even change the value stored at A using the same syntax

we using *p = 8;

we are changing the value stored at address in P to 8

so a is now going to be 8

pointers in c++ code academy-74
pointers in c++ code academy-75

p -> is the address

*p -> is the value at address

programmatically A and *P are now the same thing

pointers in c++ code academy-79

if we print P without *, we will print the address it has stored in it (here the address of A)

if we print P with * like *P we will print the value stored at A


code snippets 

pointers in c++ code academy-84
pointers in c++ code academy-85

you declare at an integer with int a

you declare a pointer with int * p

pointers in c++ code academy-88

you can declare a character var with char c;

you can declare a pointer to a character using char * Pc

so you can declare a pointer to any data type

pointers in c++ code academy-92

here we show how to declare a double and also how to declare a pointer to a double

pointers in c++ code academy-94

you can even point to a user defined structure. (we shall see this later)

the & before a variable name gives you its address in memory

pointers in c++ code academy-97

if we have the address of A stored at P

we can read the value of A by derefrencing the pointer P

pointers in c++ code academy-100

time to see it in a real c program

pointers in c++ code academy-102

we can not print P yet as we have not yet stored anything in that pointer.

A pointer needs to point to some address or we should save 'null' into it so if we print here we get an error

pointers in c++ code academy-105

this is the error for printing an empty pointer

pointers in c++ code academy-107

now we store the address of A at pointer P

pointers in c++ code academy-109

printing this returns the address of A

we can prove that is is a really the address of A by printing &A

& -> is like an operator on a variable that returns its address

pointers in c++ code academy-113

this syntax should return the value stored at address P

remember we read that as -> value at P (value that pointer P is pointing to)

pointers in c++ code academy-116
pointers in c++ code academy-117

the value at P here is A and we can see its a random number in memory because we didnot initialize it

pointers in c++ code academy-119

if we initialize A to 10;

now we can see that the value at address stored in pointer P (which is the value of A) is printed as 10

pointers in c++ code academy-122

by printing the address of A using the & operator, we see that it is the same value stored in pointer P

pointers in c++ code academy-124

read *p as value at address p <- it is used to dereference the pointer to get the value stored at the address the pointer is pointing to

we can change the value stored in variable A using its pointer P since pointer P contains the address of A

by assigning a new value to *p (value at address in P) we changed the value of A in memory to 12 (without touching the variable A in our code 😁)

This is the most beautiful thing about the c language.

pointers in c++ code academy-129

now we test to see if the value of a really changed by printing out A

pointers in c++ code academy-131

the value of A is now 12

pointers in c++ code academy-133

we make a new variable B and assign 20 to it

we change the value at address stored in P (which is A) to be the same value as b (20)

*p = b; is equivalent to a = b;

this means we are using the pointer to variable A to change its value to 20 (because B is 20)

pointers in c++ code academy-138
pointers in c++ code academy-139

now we print out the address stored in P (it is still the address of variable A)

however the value stored in A is now equal to 20

pointers in c++ code academy-142
pointers in c++ code academy-143

you can initialize the pointer variable in two lines

  1. you declare the pointer
  1. you assign an address to it (here the address of A)

you can also just declare and assign a value to it as seen below

pointers in c++ code academy-148

the space between the * and the variable name is not important

you can write this code as int* p or int *p <- it is all the same to the compiler

pointers in c++ code academy-151

Pointer arithmetic

this is basically some operators you can do on a pointer variable.

pointers in c++ code academy-155
pointers in c++ code academy-156

if you increment a pointer to an integer (a pointer that points to an integer) by one, it is as if you are moving it from the current integer address to the next integer address.

It has to increase by the size of memory of the variable type it is pointing to

it will therefore move to point to the next integer in memory (this will be 4 block/ bytes away) because an integer is 4 bytes

int4 bytes
float4 bytes
char1 byte
double 8 bytes
pointers in c++ code academy-161
pointers in c++ code academy-162

so p + 1 is not 2003, it is supposed to be 2002 + 4 = 2006

p+1=>2002+4bytes=2006 p+1 = > 2002 + 4 bytes = 2006

the slots 2002, 2003, 2004, 2005 are already accupied by the integer stored at 2002 address

because 2006 is the next integer is memory

pointers in c++ code academy-168

p+1 increments p by 4 since the size on an integer variable is 4

pointers in c++ code academy-170

if we print the size of an integer using the sizeOf operator from c

we will see that is 4 bytes

pointers in c++ code academy-173
pointers in c++ code academy-174
pointers in c++ code academy-175

if we incrment it by 2, the value gets incremented by 8 bytes (2 integers for this compiler)

if it was pointer to char, it would get incremented by one bytes

if it was a pointer to a double it would incremented by 8 bytes

pointers in c++ code academy-179

we can stil dereference or try to read the next integer stored in memory after P using the *(p + 1) syntax

we will get a gabbage value as that part of the memory was not initialized.

but as you can see once we have a pointer, we can jump around in memory and do whatever we want to do.

in you program you must be careful to always access the address you intended to, otherwise you might overwrite some other data in memory leading to unexecpted behaviours of your program

pointers in c++ code academy-184

it is important to know that you can only run operations on pointers that result into a valid address.

You can add 1 to a pointer, to return a pointer address that points to the next address in memory of the same type (like the we saw above)

you can add 2 to to a pointer.

infact you can add any integer to it, that will point to another address.

you can subtract two pointers as this may lead to a valid address (as long as you dont get a negative result).

you cannot multiply since this may lead to an invalid address result

you cannot divide pointers as well.

int *ptr1, *ptr2, *ptr3;

ptr3 = ptr1 * ptr2; // Error: Multiplication of pointers

ptr3 = ptr1 / ptr2; // Error: Division of pointers

you can however do this operations, if you are derefencing and multiplying or dividing the values this pointers are pointing to

int a = 1, b = 2, c = 3;

int *ptr1 = &a;

int *ptr2 = &b;

int *ptr3 = &c;

*ptr3 = *ptr1 * *ptr2; // No error: c = a * b

*ptr3 = *ptr1 / *ptr2; // No error: c = a / b


More on pointers 

pointers in c++ code academy-207
pointers in c++ code academy-208

pointers are strongly typed.

this means the type of variable, the pointer is pointing to, must be known by the compiler.

pointers in c++ code academy-211
pointers in c++ code academy-212

we need to know teh type of variable, so that we know the type of data we shall be derefrencing.

all data is stored as binary.

a compiler need to know waht type of data the binary at the memory so that is can process it correctly (as an integer, or. character)

pointers in c++ code academy-216
pointers in c++ code academy-217

thse are teh block of memory needed to store each of these data types

pointers in c++ code academy-219

an integer a will occupy 4 bytes

it is stored as binary data

each 8 bits is one byte

pointers in c++ code academy-223

byte 0 here is the least significant byte

pointers in c++ code academy-225

we have assigned these random by continous addresses to these bytes

each address block is one byte

pointers in c++ code academy-228
pointers in c++ code academy-229

the last bit will store the sign of this integer, as a negative number or a positive number

pointers in c++ code academy-231

we are can convert this binary number in memory back to base 10 and we will get 1025 like we started out with

pointers in c++ code academy-233
pointers in c++ code academy-234

we have a pointer P storing the address of A and thus pointing to A

it will store only the address of the first byte in memory as 200.

the only way to know the that this variable A occupies the blocks, 200, 201, 202 and 203 is to know that it is an integer variable that occupies 4 block/ bytes in memory

pointers in c++ code academy-238

so during derefrencing

we know we have to look at the address 200 until 203 to read the value of this interger

the compiler needs to know the type of data it is dealing with

pointers in c++ code academy-242
pointers in c++ code academy-243

if P was. char pointer , pointing at address 200

the machine will only check one byte of data and read only the data at 200

this is because it knows that a character variable only occupies one block of memory

if it was a float, it would still be. 4 bytes, but the way the data is stored and handled by the machine is different from how it is stored and handled if it is a float or if it is an integer, even though both occupy 4 bytes in memory

pointers in c++ code academy-248
pointers in c++ code academy-249
pointers in c++ code academy-250

we will print out hte size of an integer here

pointers in c++ code academy-252

we will print the address of P and the value at P address -> the value of A which P points to

pointers in c++ code academy-254
pointers in c++ code academy-255
pointers in c++ code academy-256

we will declare a pointer character now as p-zero

and store the address of integer A there to play a trick 😂

we will give the compiler the wrong data type of the address we are pointing to

pointers in c++ code academy-260
pointers in c++ code academy-261

here we get a compilation error as the compiler does not want to assign data of a pointer to an integer into a pointer to character

pointers in c++ code academy-263

we will force P pointer to be character pointer by type-casting it to be a pointer to character using type casting syntax

pointers in c++ code academy-265
pointers in c++ code academy-266
pointers in c++ code academy-267

we wil try to derenference the value that p-zero is pointing to

pointers in c++ code academy-269

it is reading the value here as one.

pointers in c++ code academy-271
pointers in c++ code academy-272
pointers in c++ code academy-273

during type casting, we store the address of the first byte into pointer p-zero

pointers in c++ code academy-275

that is why this was printed as a one

pointers in c++ code academy-277

we can try to add to a pointer P to see what address it changes to

* you can only add or subtract pointers (this is so that pointer arithmetic leads to a valid address in memory)

pointers in c++ code academy-280
pointers in c++ code academy-281
pointers in c++ code academy-282

p+1 has returned a gabbage integer from memory

pointers in c++ code academy-284

p-zero is still dereferencing to 1, the value of of the least signficant byte of A

pointers in c++ code academy-286
pointers in c++ code academy-287

p-zero + 1 as move moved to point to the next one-byte address which happens to be storing value 4 in binary

pointers in c++ code academy-289
pointers in c++ code academy-290

typecasting has some use cases that we will see later

pointers in c++ code academy-292

we can have a general pointer that does not point to any data type

we call it a void pointer

pointers in c++ code academy-295
pointers in c++ code academy-296

we can assign it to another address of any kind, since it is a pointer to void

we dont need to do typecasting for this

pointers in c++ code academy-299
pointers in c++ code academy-300

since it is not refereced to any data type, we cannot deference it

pointers in c++ code academy-302
pointers in c++ code academy-303

we can see that address in void pointer is the same as the address of a

pointers in c++ code academy-305

we can do arithmetic on it

pointers in c++ code academy-307

it is not possible and we get an compilation error as teh pointer does not know by how many memory block to increment this pointer

pointers in c++ code academy-309

we will use these later. lets jsut remember them


Pointers to pointers

pointers in c++ code academy-313

a poitner is jsut a variable to memory, so another pointer can point to it

pointers in c++ code academy-315

assuming this is teh view of our computers memory

each block is one bytes

each block has an address

we assign some addresses here

pointers in c++ code academy-320

if we have an integer variable as x and intialize it as x

pointers in c++ code academy-322

some memory should be allocated for x

in a typical computer architecture , it will be 4 bytes as it is an integer

pointers in c++ code academy-325

assuming address 225 is alocated to x integer

Thanks for checking this out 😊

Leave a comment on the youtube video if you would like me to make notes on the rest of this video

Last Updated:

Summarize & share videos seamlessly

Loading...