data-structures Getting started with data-structures Intro to Data Structures


Example

A data structure is a way of organizing and storing information.

Let a "Hello, World!" string be the information that we need to organize and store in byte-addressable memory.

Each ASCII character requires 7 bits of storage. Most systems reserve 8 bits (1 byte) for each character, so each character in "Hello, World!" is stored in an individual byte-sized unit of memory, one after another, consecutively.

We need a single reference to our string even though it spans multiple memory addresses, so we use the address of the first character in the string, 'H'. Every other character can be accessed at the address of 'H' + the index of that character using zero-indexed characters.

We want to print our string, "Hello, World!" We know its address in memory, which we supply to the print function, but how does the print function know to stop printing consecutive memory locations? One common approach is to append the null character, '\0', to the string. When the print function encounters the null character, it knows that it has reached the end of the string.

We have defined a way of organizing and storing our string, i.e. a data structure! This very simple data structure is a null-terminated character array, which is one way to organize and store a string.