Pointers in Carbon Language

pointers in carbon language

Pointers in Carbon language are similar to C++. The type of pointers-to-values-of-type-P is written as P*. Carbon pointers do not support pointer arithmetic.

What operations are supported by Pointers in Carbon?

Carbon pointers support Dereferencing and Address-of

  1. Dereferencing – given a pointer p, *p gives the value p points to as an l-value. p->m same as (*p).m.
  2. Address-of – given an l-value x, &x returns a pointer to x.

Note: There are no null pointers in Carbon. To represent a pointer that may not refer to a valid object, use the type Optional(P*).

Code Example

var p: i32 = 45;
var pp: auto = *p;
var ppp: auto = &pp;