Pointers

GetGoing: Introduction to Golang Data types and control structure
3 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

Hi guys, in this video, we're going to be talking about pointers. So a pointer is just a variable, it stores the address of another variable. So let's see that in action. So let's say m one is equal to two. Now my pointer is going to be the address of m one. And we're simply going to print out the pointer.

So let's see what happens if we printed out the memory address. So what happened was that we initialize this variable to do we store the address of m one in our variable pointer, and we printed out the pointer. This is a hexadecimal representation of the address of where m one is actually stored. Now, the and symbol is called the referencing symbol. It's also called the reference operator, which is used to get the address of a variable to get the value the data which is stored inside and address, you dereference that pointer. So by default, dereference reaming star So if you didn't start Vidya is going to print it.

So let's see how in action, we can actually leverage this functionality. So I'm going to be showing you an example of pass by value and pass by reference. So we do a function swap function swap takes two variable variables, m one m two, both of type int. So we can do an M one comma m to start and because both of these are of the type size and then we declare a temporary variable to hold the values temporarily, of type int. So we can do a temp is equal to start off in one. Start of him is going to be equal to start a family and then start from one is going to be equal to now what we can do is we can declare two variables and when Do my name to each other equal to two and three respectively.

So I'm going to be printing m One, two. Let's swap with both of these variables. So by if you want to pass a pointer to a function, you can simply reference it using add. So add m one, and m one and m two. And then you simply do FMT dot print line. And we're going to, so let's see what happens.

Oh, yeah, we had two and three and later, we had three and two. So we actually went to the address of where m one and m two were stored, and we swapped out both of these variables. So you might not see pointers in languages like Python. In Python, they only have one this pointer, but in this case, pointers are widely used in code. Thank you.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.