Skip to main content
Log in

Mojo struct

Pointer

@register_passable(trivial) struct Pointer[mut: Bool, //, type: AnyType, origin: Origin[mut], address_space: AddressSpace = 0]

Defines a non-nullable safe pointer.

For a comparison with other pointer types, see Intro to pointers in the Mojo Manual.

Parameters

  • mut (Bool): Whether the pointee data may be mutated through this.
  • type (AnyType): Type of the underlying data.
  • origin (Origin[mut]): The origin of the pointer.
  • address_space (AddressSpace): The address space of the pointee data.

Implemented traits

AnyType, CollectionElementNew, Copyable, ExplicitlyCopyable, Movable, Stringable, UnknownDestructibility

Methods

__init__

__init__(out self, *, other: Self)

Constructs a copy from another Pointer.

Note that this does not copy the underlying data.

Args:

  • other (Self): The Pointer to copy.

__getitem__

__getitem__(self) -> ref [origin, address_space] type

Enable subscript syntax ptr[] to access the element.

Returns:

A reference to the underlying value in memory.

__eq__

__eq__(self, rhs: Pointer[type, origin, address_space]) -> Bool

Returns True if the two pointers are equal.

Args:

  • rhs (Pointer[type, origin, address_space]): The value of the other pointer.

Returns:

True if the two pointers are equal and False otherwise.

__ne__

__ne__(self, rhs: Pointer[type, origin, address_space]) -> Bool

Returns True if the two pointers are not equal.

Args:

  • rhs (Pointer[type, origin, address_space]): The value of the other pointer.

Returns:

True if the two pointers are not equal and False otherwise.

address_of

static address_of(ref [origin, address_space] value: type) -> Self

Constructs a Pointer from a reference to a value.

Args:

  • value (type): The value to get the address of.

Returns:

The result Pointer.

__str__

__str__(self) -> String

Gets a string representation of the Pointer.

Returns:

The string representation of the Pointer.