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 –
-
Dereferencing– given a pointerp,*pgives the valueppoints to as an l-value.p->msame as(*p).m. -
Address-of– given an l-valuex,&xreturns a pointer tox.
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;
Carbon Language Series
- Introduction to Carbon Language.
- How to define variables in Carbon.
- Primitive Types – Bool, Int, Float, String
- Tuple in Carbon
- Struct in Carbon
- Pointers in Carbon
- Operators in Carbon Language
- Conditions & Control Flow in Carbon
- Ternary Operator (if expression) in Carbon
- Switch conditional in Carbon using Match
- Loops in Carbon
- Functions in Carbon