In this article, we’re going to learn about basic data types, and we’re going to start with Pointers. So, let’s go straight to these Pointers and understand step by step.
What is a Pointer?
The pointer is an address to some data in memory. So, every data variable is located in memory somewhere, in some variable data staying in memory somewhere. Also, functions and so on, they are all in memory somewhere. The pointer is an address of that in memory.
So, with the Pointers, there are two primary operators that are associated with Pointers. The (&)ampersand operator returns the address of the variable or the function, and the (*) star operator which dereferencing, does the opposite of the ampersand.
It returns the data at the address. So, if you use the ampersand operator in front of a variable, the name of a variable, that will return you the address of that variable. The star operator goes the other way. If you put that in front of a Pointer, to some address, put that in front of an address, it will return you the data at that address. So, the ampersand operator and the star operator are opposites of one another.
So, we’ll give you an example, look at this following code.
var A int =1
var B int
var PI *int // PI is pointer to int
PI = &A // PI now points to about
B = *P // B is now 1
we define our variable a is an integer, it’s equal to one. Then, b is an integer and it’s not initialized, so that would mean it would be by default initialized to zero. Then, a var PI. So, PI is not declared to be an int, it’s a star int.
It means PI is actually a Pointer since there’s a star operator in front of the int, PI is declared to be a pointer to an integer. So, PI is the Pointer, but it is not an actual integer, is a Pointer to an integer. So then, if we go on, PI = &a. a is actually an integer. Integer whose value is one. So, there is a number one sitting in memory somewhere, and a is a reference to it, the name of that.
The address &a in memory where we can find that value one. So, PI is now equal to that address. So, whatever the address of that one, is rather, of A, whatever that address is, PI is the Pointer to that address, it is the address. Then, in the next line,
B = *PI
PI is actually a Pointer and the data at that address that PI is pointing to is the value one. Now, star PI, the star does dereference. the star returns the value, the data at that address.
Actually, b is now equal to the data at the address that PI is pointing to. Now, if you remember for a line for PI is pointed to what a is pointing to. So, PI is pointing to the value one, that data in memory. So B, if b = *PI , b =1. So, this now sets b equal to one. It is just a small example here, just trying to show how the ampersand and the star operators work opposite to one another.
So, these are Pointers, and Pointers exist. These Pointers are basically if you now see the same type of implementation. Now, there’s another function called New.
- It’s another way to create a variable.
- New(), instead of returning a variable, it returns a pointer to the variable. after that, the new function creates a variable and it returns a pointer to that variable. So, this is unlike, if we’re just declaring a variable, That also creates a variable, but New() explicitly returns a Pointer to a variable.
- The variable is initialized to zero by default with New. So, for instance here,
ptr := new (int)
*pr = 3
If we say our Pointer equals new int, and then star Pointer equals three. Pointer declare as new int, new int returns me a Pointer to an integer, and that ptr is equal to that Pointer. Then, we can set the value of that integer by referring to *ptr. Because *ptr is the value that ptr is pointing to. If we say *ptr=3, then the value three is placed at the address specified by ptr.