Array

Motivation

  1. Deal with multiple variables with similar properties and structures, it can help us eliminate the tedious work
  2. Definition: An array is a sequence of items of the same type

Array Declaration and Initialization

int myArray[4]
int myArray = {1,2,3,4}

  1. Declaration:
    The array declaration should include the data type of the array and the size of the array.
  2. Property:
    Notice: the name of the array is a pointer which points to the first element of the array. And it is not a lvalue which means we can’t change where it points.
  3. Initialization:
    Include the initial values in the curly braces
    Notice:
  4. Extensions: Apply similar syntax to initialize structs

Accessing an Array

  1. Index the Array
    MyArray [4] can simply access the fifth element of the array, and it can be used as either a lvalue or a rvalue.
    Notice: Array index is zero-based and Access out of range is an error that the complier will not detect.
  2. Pointer Arithmetic
  3. Passing Arrays as Parameters:

Array Caveats

  1. Dangling Pointer(悬空指针):
  2. Array Size:

Struct

Data structure

  1. concept:
    1. A logical grouping of several piece of data
    2. Some operations that manipualte those data
  2. Ways to build datastructre:
  3. Principles to develop a structure:
    1. specify the fields contained in the struct
    2. specify the file-scope variables
    3. specify some operations to the structure that can be implemented by functions

Struct

  1. Definition:
    Struct can define a structure type, it does not create instances of the struct. We apply the same way to declare variables in struct as in other places.
    Example

    1
    2
    3
    4
    5
    
    typedef struct book_t {
        char author[50];
        char title[10];
        int32_t edition;
        }book;
    
  2. Memory order:
    The fields are stored in memory the same way declared in the definition.
  3. sizeof operator:
    sizeof (structA) evaluates to the number of bytes occupied by the variable structA
    Pitfall: avoid calculating the size by hand
    Most ISAs impose alignment requirements that load and store of N bytes use addresses that are multiples of N. And the complier will align fields to their size and structures to the maximum alignment needed by any field.
  4. typedef prototype:
    typedef <base type> <list of types>;
    Note that: the structure definition need not appear before these definitions.
    And a structure definiiton can be used as a base type.
  5. Field access:
    Apply field operator
    . is the field access operator that can help to access the fields defined in the structure.
    Without typedef, every time you want to declare a struct variable you have to code like struct book_t bookA
    Apply —> operator to access fields after dereferencing
    —> can dereference and access a field. (*s).top = s->top
  6. Pass as a parameter:
    Pass the pointer to the structure as a parameter instead of the whole copy, which will take up much more space.
    Which way to push a parameter worths thinking.

Enumeration

  1. Concept:
    Enumeration is a list of things with some common features numbered consecutively.
  2. Rule:
    In C, enumeration start with 0 but can be overridden.
    Example: enum {FALSE, TRUE} FALSE has value 0 and TRUE has value1
    And the values are numbered and re-numbered automatically.
  3. Usage:
    Impart a list of integer with significant names to make it more convenient and readable.