Skip to content

Instantly share code, notes, and snippets.

@Theartbug
Created January 18, 2020 00:08
Show Gist options
  • Save Theartbug/d3607be5f69cb597b24561e406344b5c to your computer and use it in GitHub Desktop.
Save Theartbug/d3607be5f69cb597b24561e406344b5c to your computer and use it in GitHub Desktop.
c++ structured arrays

Structured Arrays (multidimensional arrays)

arrays

  • when declared int x[10], you cannot overwrite x with another value
    • array variables are considered constants
  • int array[3][2] is an array of size three with integer arrays size two
[
    [int, int],
    [int, int],
    [int, int],
]
  • when passed as a parameter, will be function(int **y)

subscript operator

  • can work on a pointer expressions
    • x[i] == *(x + i)
  • is a binary operator (requires two operands)
    • operand1[operand2]
  • the address is computed by the subscript operator and takes into account the size of the elements in the array
    • therefore the index is independent of the actual address of the element
  • pointer and array math are interchangeable, since arrays are essentially pointers

sizeof operator

  • entirely compile-time
    • replaces all the calls with the given number
  • sizeof(array)
  • the number of elements can be determined by sizeof(array)/sizeof(array[i])
  • since a pointer is a memory address, when doing pointer[1] or pointer + 1 you are actually doing memoryLocation + (1 * sizeof(dataType))
  • strictly based on the type
    • even if the type is a pointer to another type, still is pointer type
    • any expression passed in will not be evaluated

memory

  • word is the size of a single place of storage / memory in a computer
    • typically 32bit or 64bit
  • structs and classes are larger than the primitive data types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment