Skip to content
IsaacShelton edited this page Mar 21, 2022 · 1 revision

ubyte and *ubyte

ubyte represents the type of an unsigned byte value. It can hold values [0, 255].

Specifications

Primitive Type Bits Size Signed-ness Possible Values C Equivalent (Roughly) Suffix
ubyte 8 bits 1 byte unsigned 0..255 unsigned char ub

Character Literal Syntax

Character literals are specified by using a single character C-string with the ub suffix

letter_a ubyte = 'A'ub

Some special characters can be created using escape sequences

'\n'    // Newline
'\r'    // Carriage Return
'\t'    // Tab
'\b'    // Backspace
'\e'    // ESC
'\0'    // Null
'\''    // Apostrophe
'\\'    // Backslash

Common Uses

Null-Terminated Strings

Most commonly, ubyte is used for null-terminated strings.

planet *ubyte = 'Earth'

Array of Characters

Another common usage is for buffers.

array *ubyte = malloc(256)

What's the difference between ubyte vs *ubyte vs **ubyte?

ubyte in an unsigned 8-bit integer. It takes up one byte of space (sizeof ubyte == 1) and can represent values from 0-255. Just like char in C, it's used as the type to hold an ascii character, but unlike in C, it is guaranteed to be unsigned.

*ubyte is pointer to a location in memory where one or more ubytes are stored. **ubyte is pointer to a pointer to a location in memory where one or more ubytes are stored.

Here's a graphic, if you have some experience with C then its basically the same as char

ubyte_pubyte_ppubyte

Just like in C where []/* are interchangeable, pointers to values in memory don't indicate how many of what they point to is there. So **ubyte could be a pointer to a C-String, or it could be a location in memory where several C-Strings are stored.

Clone this wiki locally