What is Pointer?

ยท

1 min read

What is Pointer?

What is this ๐Ÿ‘‡?

var favoriteNumber int = 7

a variable of the name "favoriteNumber" of a type integer that stores 7.

similarly

var favoriteColor string = "black"

a variable of the name "favoriteColor" of a type string that stores "black".

pretty simple right? ๐Ÿคœ

How does computer store variables?

image.png

Computer store variable in the memory as block and block has an address and value. we can access and manipulate that value by address and here comes the pointer in the picture.

What is Pointer?

Pointer is a type of variable that contains the address of another variable.

// integer type variable
var num int = 7

// address of num stored in pointer p
var p *int = &num

fmt.Printf(p) // output: 0xc000018030
// yours will different

// access value of num by address
fmt.Printf(*p) // output: 7

// manipulate the value of num by address
*p = 18

fmt.Printf(num) // output: 18

For me, the pointer was a mysterious ๐Ÿ‘๏ธโ€๐Ÿ—จ๏ธ topic. Share your experience in the comments

ย