Pointer Arithmetic (Not Allowed in Go)

List Topics
February 16, 2025
No Comments
4 min read

In Go, pointer arithmetic (performing operations like addition or subtraction on pointers) is not allowed. This differs from languages like C or C++, where you can manipulate pointers using arithmetic.

👉 The key reason pointer arithmetic is not allowed in Go is to prevent unsafe memory manipulation. Go prioritizes memory safety, and by disallowing pointer arithmetic, it ensures that memory is handled in a controlled and predictable manner.

Step-by-Step Explanation:

1ī¸âƒŖ What is Pointer Arithmetic?

Pointer arithmetic involves modifying a pointer’s value to point to a different memory location by adding or subtracting from the memory address it holds.

🔹 Example in C (where pointer arithmetic is allowed):

C
int arr[] = {1, 2, 3};
int *ptr = arr;
ptr++;  // Moves the pointer to the next memory address

In the above C example, ptr++ moves the pointer to the next element in the array. This is not allowed in Go.

2ī¸âƒŖ Why Pointer Arithmetic is Not Allowed in Go

Go restricts pointer arithmetic to protect memory safety. Allowing arithmetic on pointers could lead to:

  • Accessing invalid memory locations (which can lead to program crashes or security vulnerabilities).
  • Unpredictable behavior by modifying pointers beyond their intended range.

Go prefers to keep pointer usage simple and controlled, ensuring that you cannot manipulate memory addresses in unsafe ways.

3ī¸âƒŖ What You Can Do With Pointers in Go

In Go, you can:

  • Declare pointers (a variable that stores the address of another variable).
  • Get the address of a variable using the & operator.
  • Dereference a pointer using the * operator to access the value stored at that memory address.

🔹 Example:

Go
package main

import "fmt"

func main() {
    var x int = 10
    var ptr *int = &x  // Pointer to x

    fmt.Println("Value of x:", x)       // Prints: 10
    fmt.Println("Pointer value:", ptr)  // Prints: Memory address of x

    *ptr = 20  // Modify the value at the pointer address
    fmt.Println("Updated value of x:", x)  // Prints: 20
}

🔹 Output:

Bash
Value of x: 10
Pointer value: 0xc000012090  // Example memory address
Updated value of x: 20

4ī¸âƒŖ What You Cannot Do in Go

In Go, you cannot perform pointer arithmetic like adding or subtracting from pointers, such as ptr++ or ptr--.

🔹 Example (Invalid in Go):

Go
package main

import "fmt"

func main() {
    var x int = 5
    var ptr *int = &x

    // This would cause a compile-time error in Go
    // ptr++  // Invalid: Go does not allow pointer arithmetic

    fmt.Println("Pointer value:", ptr)
}

🔹 Output:

Bash
Invalid operation: ptr++ (Go does not support pointer arithmetic)

Summary:

  • Go does not allow pointer arithmetic (such as ptr++, ptr--), unlike C or C++.
  • This design decision ensures memory safety and prevents unpredictable behavior in Go programs.
  • You can still declare pointers, get the address of a variable, and dereference pointers in Go, but arithmetic on pointers is restricted.

Pointers in Go are made simple and safe to use, avoiding potential risks associated with direct memory manipulation.


Pointer Arithmetic (Go āϤ⧇ āĻ…āύ⧁āĻŽā§‹āĻĻāĻŋāϤ āύāϝāĻŧ)

Go āϤ⧇, pointer arithmetic (āϝ⧇āĻŽāύ: pointer āĻāϰ āωāĻĒāϰ āϝ⧋āĻ— āĻŦāĻž āĻŦāĻŋāϝāĻŧā§‹āĻ— āĻ•āϰāĻž) āĻ…āύ⧁āĻŽā§‹āĻĻāĻŋāϤ āύāϝāĻŧāĨ¤ āĻāϟāĻŋ C āĻŦāĻž C++ āĻāϰ āĻŽāϤ⧋ āĻ­āĻžāώāĻžāϰ āϤ⧁āϞāύāĻžāϝāĻŧ āφāϞāĻžāĻĻāĻž, āϝ⧇āĻ–āĻžāύ⧇ pointer arithmetic āϏāĻžāϧāĻžāϰāĻŖāĻ­āĻžāĻŦ⧇ āĻŦā§āϝāĻŦāĻšā§ƒāϤ āĻšāϝāĻŧāĨ¤

👉 Go āĻ­āĻžāώāĻžāϟāĻŋ āĻŽā§‡āĻŽā§‹āϰāĻŋ āύāĻŋāϰāĻžāĻĒāĻ¤ā§āϤāĻž āύāĻŋāĻļā§āϚāĻŋāϤ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ pointer arithmetic āύāĻŋāώāĻŋāĻĻā§āϧ āĻ•āϰ⧇āϛ⧇āĨ¤

Step-by-Step āĻŦā§āϝāĻžāĻ–ā§āϝāĻž:

1ī¸âƒŖ Pointer Arithmetic āϕ⧀?

Pointer arithmetic āĻŦāϞāϤ⧇ āĻŦā§‹āĻāĻžāϝāĻŧ āĻāĻ•āϟāĻŋ pointer āĻāϰ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ āĻŋāĻ•āĻžāύāĻž āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰāĻž, āϝ⧇āĻŽāύ C āĻŦāĻž C++ āĻ āφāĻĒāύāĻŋ ptr++ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ pointer āϕ⧇ āĻĒāϰāĻŦāĻ°ā§āϤ⧀ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ āĻŋāĻ•āĻžāύāĻžāϝāĻŧ āύāĻŋāϝāĻŧ⧇ āϝ⧇āϤ⧇ āĻĒāĻžāϰ⧇āύāĨ¤

🔹 āωāĻĻāĻžāĻšāϰāĻŖ (C āϤ⧇, āϝ⧇āĻ–āĻžāύ⧇ pointer arithmetic āĻ…āύ⧁āĻŽā§‹āĻĻāĻŋāϤ):

C
int arr[] = {1, 2, 3};
int *ptr = arr;
ptr++;  // Pointer āϕ⧇ array āĻāϰ āĻĒāϰāĻŦāĻ°ā§āϤ⧀ āωāĻĒāĻžāĻĻāĻžāύ⧇ āύāĻŋāϝāĻŧ⧇ āϝāĻžāϝāĻŧ

āϤāĻŦ⧇ Go āϤ⧇, āĻāϟāĻŋ āĻ…āύ⧁āĻŽā§‹āĻĻāĻŋāϤ āύāϝāĻŧāĨ¤ āφāĻĒāύāĻŋ ptr++ āĻŦāĻž ptr-- āĻāϰ āĻŽāϤ⧋ āĻ•ā§āϰāĻŋāϝāĻŧāĻž āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŦ⧇āύ āύāĻžāĨ¤

2ī¸âƒŖ Go āϤ⧇ Pointer Arithmetic āύāĻŋāώāĻŋāĻĻā§āϧ āϕ⧇āύ?

Go āĻ­āĻžāώāĻžāϝāĻŧ pointer arithmetic āύāĻŋāώāĻŋāĻĻā§āϧ āĻ•āϰāĻž āĻšāϝāĻŧ⧇āϛ⧇ āĻŽā§‡āĻŽā§‹āϰāĻŋ āύāĻŋāϰāĻžāĻĒāĻ¤ā§āϤāĻž āύāĻŋāĻļā§āϚāĻŋāϤ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝāĨ¤ Pointer arithmetic āĻ…āύ⧁āĻŽā§‹āĻĻāĻŋāϤ āĻšāϞ⧇:

  • āĻ…āĻ•āĻžāĻ°ā§āϝāĻ•āϰ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ…āĻŦāĻ¸ā§āĻĨāĻžāύ⧇ āĻ…ā§āϝāĻžāĻ•ā§āϏ⧇āϏ āĻ•āϰāĻžāϰ āϏāĻŽā§āĻ­āĻžāĻŦāύāĻž āĻĨāĻžāϕ⧇, āϝāĻž āĻĒā§āϰ⧋āĻ—ā§āϰāĻžāĻŽā§‡āϰ āĻ•ā§āĻ°ā§āϝāĻžāĻļ āĻŦāĻž āĻ…āύāĻŋāĻ°ā§āĻĻ⧇āĻļā§āϝ āφāϚāϰāĻŖ āϏ⧃āĻˇā§āϟāĻŋ āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧇āĨ¤
  • āĻ•āĻ āĻŋāύ āĻ¤ā§āϰ⧁āϟāĻŋ āĻāĻŦāĻ‚ āύāĻŋāϰāĻžāĻĒāĻ¤ā§āϤāĻž āĻā§āρāĻ•āĻŋ āĻŦāĻžāĻĄāĻŧāĻŋāϝāĻŧ⧇ āϤ⧋āϞ⧇āĨ¤

āĻāχ āĻā§āρāĻ•āĻŋāϗ⧁āϞ⧋ āĻāĻĄāĻŧāĻžāύ⧋āϰ āϜāĻ¨ā§āϝ, Go āĻ­āĻžāώāĻžāϝāĻŧ pointer arithmetic āϏāĻŽā§āĻĒā§‚āĻ°ā§āĻŖ āύāĻŋāώāĻŋāĻĻā§āϧ āĻ•āϰ⧇ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻŦā§āϝāĻŦāĻ¸ā§āĻĨāĻžāĻĒāύāĻž āύāĻŋāϰāĻžāĻĒāĻĻ āĻ“ āύāĻŋāϝāĻŧāĻ¨ā§āĻ¤ā§āϰāĻŋāϤ āϰāĻžāĻ–āĻž āĻšāϝāĻŧ⧇āϛ⧇āĨ¤

3ī¸âƒŖ Go āϤ⧇ Pointers āĻĻāĻŋāϝāĻŧ⧇ āϕ⧀ āĻ•āϰāĻž āϝāĻžāϝāĻŧ

Go āϤ⧇ pointers āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āφāĻĒāύāĻŋ:

  • āĻ­ā§āϝāĻžāϰāĻŋāϝāĻŧ⧇āĻŦāϞ⧇āϰ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ āĻŋāĻ•āĻžāύāĻž āϧāĻžāϰāĻŖ āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧇āύāĨ¤
  • Pointer āĻĻāĻŋāϝāĻŧ⧇ āĻŽā§‡āĻŽā§‹āϰāĻŋāϰ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧇āύāĨ¤

Pointer arithmetic āĻāϰ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤ⧇, Go āĻŽā§‡āĻŽā§‹āϰāĻŋāϰ āϏāĻžāĻĨ⧇ āύāĻŋāϰāĻžāĻĒāĻĻ āĻāĻŦāĻ‚ āĻ¸ā§āĻĒāĻˇā§āϟāĻ­āĻžāĻŦ⧇ āĻ•āĻžāϜ āĻ•āϰāĻžāϰ āωāĻĒāĻžāϝāĻŧ āĻĻ⧇āϝāĻŧāĨ¤ āύāĻŋāĻšā§‡ āϕ⧀ āĻ•āϰāĻž āϝāĻžāϝāĻŧ āϤāĻžāϰ āĻāĻ•āϟāĻŋ āωāĻĻāĻžāĻšāϰāĻŖ āĻĻ⧇āĻ“āϝāĻŧāĻž āĻšāϞ⧋:

🔹 āωāĻĻāĻžāĻšāϰāĻŖ:

Go
package main

import "fmt"

func main() {
    var x int = 10
    var ptr *int = &x  // x āĻāϰ āϜāĻ¨ā§āϝ pointer

    fmt.Println("x āĻāϰ āĻŽāĻžāύ:", x)       // Prints: 10
    fmt.Println("Pointer āĻāϰ āĻŽāĻžāύ:", ptr)  // Prints: x āĻāϰ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ āĻŋāĻ•āĻžāύāĻž

    *ptr = 20  // Pointer āĻāϰ āĻ āĻŋāĻ•āĻžāύāĻžāϝāĻŧ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰāĻž āĻšāϞ⧋
    fmt.Println("x āĻāϰ āφāĻĒāĻĄā§‡āĻŸā§‡āĻĄ āĻŽāĻžāύ:", x)  // Prints: 20
}

🔹 Output:

Bash
x āĻāϰ āĻŽāĻžāύ: 10
Pointer āĻāϰ āĻŽāĻžāύ: 0xc000012090  // āωāĻĻāĻžāĻšāϰāĻŖāĻ¸ā§āĻŦāϰ⧂āĻĒ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ āĻŋāĻ•āĻžāύāĻž
x āĻāϰ āφāĻĒāĻĄā§‡āĻŸā§‡āĻĄ āĻŽāĻžāύ: 20

āĻāχ āωāĻĻāĻžāĻšāϰāϪ⧇, ptr x āĻāϰ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ āĻŋāĻ•āĻžāύāĻž āϧāĻžāϰāĻŖ āĻ•āϰ⧇āĨ¤ āφāĻĒāύāĻŋ ptr āĻĻāĻŋāϝāĻŧ⧇ x āĻāϰ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŦ⧇āύ, āϤāĻŦ⧇ ptr++ āĻāϰ āĻŽāϤ⧋ arithmetic āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŦ⧇āύ āύāĻžāĨ¤

4ī¸âƒŖ Go āϤ⧇ āϕ⧀ āĻ•āϰāĻž āϝāĻžāϝāĻŧ āύāĻž

Go āϤ⧇ pointer arithmetic āϏāĻŽā§āĻĒā§‚āĻ°ā§āĻŖ āύāĻŋāώāĻŋāĻĻā§āϧāĨ¤ āφāĻĒāύāĻŋ pointers āĻāϰ āωāĻĒāϰ āϝ⧋āĻ— āĻŦāĻž āĻŦāĻŋāϝāĻŧā§‹āĻ— āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŦ⧇āύ āύāĻž, āϝ⧇āĻŽāύ ptr++ āĻŦāĻž ptr--āĨ¤

🔹 āωāĻĻāĻžāĻšāϰāĻŖ (Go āϤ⧇ āĻ…āĻŦ⧈āϧ):

Go
package main

import "fmt"

func main() {
    var x int = 5
    var ptr *int = &x

    // Pointer arithmetic āĻ•āϰāϞ⧇ āĻ•āĻŽā§āĻĒāĻžāχāϞ-āϟāĻžāχāĻŽ āĻ āĻāϰāϰ āĻĻāĻŋāĻŦ⧇
    // ptr++  // āĻ…āĻŦ⧈āϧ: Go āϤ⧇ pointer arithmetic āĻ…āύ⧁āĻŽā§‹āĻĻāĻŋāϤ āύāϝāĻŧ

    fmt.Println("Pointer āĻāϰ āĻŽāĻžāύ:", ptr)
}

🔹 Output:

Bash
Invalid operation: ptr++ (Go does not support pointer arithmetic)

Go āĻāϰ āĻāχ āύāĻŋāϝāĻŧāĻŽ āϕ⧋āĻĄāϕ⧇ āύāĻŋāϰāĻžāĻĒāĻĻ āĻāĻŦāĻ‚ āĻĒā§‚āĻ°ā§āĻŦāĻžāĻ­āĻžāϏāϝ⧋āĻ—ā§āϝ āϰāĻžāϖ⧇āĨ¤

āĻŽā§‚āϞ āĻŦāĻ•ā§āϤāĻŦā§āϝ:

  • Go āϤ⧇ pointer arithmetic (āϝ⧇āĻŽāύ: ptr++ āĻŦāĻž ptr--) āĻ…āύ⧁āĻŽā§‹āĻĻāĻŋāϤ āύāϝāĻŧ, āϝāĻž C āĻŦāĻž C++ āϤ⧇ āϏāĻžāϧāĻžāϰāĻŖāĨ¤
  • Go āĻāϰ āĻāχ āĻĄāĻŋāϜāĻžāχāύ āĻŽā§‡āĻŽā§‹āϰāĻŋ āύāĻŋāϰāĻžāĻĒāĻ¤ā§āϤāĻž āύāĻŋāĻļā§āϚāĻŋāϤ āĻ•āϰ⧇ āĻāĻŦāĻ‚ āĻ…āύāĻŋāĻ°ā§āĻĻ⧇āĻļā§āϝ āφāϚāϰāĻŖ āĻĒā§āϰāϤāĻŋāϰ⧋āϧ āĻ•āϰ⧇āĨ¤
  • āφāĻĒāύāĻŋ pointers āĻĄāĻŋāĻ•ā§āϞ⧇āϝāĻŧāĻžāϰ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŦ⧇āύ, āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ āĻŋāĻ•āĻžāύāĻž āĻĒ⧇āϤ⧇ āĻĒāĻžāϰāĻŦ⧇āύ, āĻāĻŦāĻ‚ pointer āĻĄā§‡āϰ⧇āĻĢāĻžāϰ⧇āĻ¨ā§āϏ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŦ⧇āύ, āϤāĻŦ⧇ pointer arithmetic āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŦ⧇āύ āύāĻžāĨ¤

Go āϤ⧇ pointers āϏāĻšāϜ āĻ“ āύāĻŋāϰāĻžāĻĒāĻĻ, āϝāĻž āϏāϰāĻžāϏāϰāĻŋ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ āĻŋāĻ•āĻžāύāĻž āĻŽā§āϝāĻžāύāĻŋāĻĒ⧁āϞ⧇āĻļāύ āĻ•āϰ⧇ āϤ⧈āϰāĻŋ āĻšāĻ“āϝāĻŧāĻž āϏāĻžāϧāĻžāϰāĻŖ āĻ¤ā§āϰ⧁āϟāĻŋ āĻāĻĄāĻŧāĻžāϤ⧇ āϏāĻšāĻžāϝāĻŧāĻ•āĨ¤

Š2025 Linux Bangla | Developed & Maintaind by Linux Bangla.