Main Menu

search

You are here

Arrays

[last updated: 2022-08-04]

Arduino IDE programming

-----

  • Arrays are a data type in the Arduino programming language.
    They represent a list/sequence/collection of variables that all have the same data type.
  • The simplest array is a one-dimensional array.
    Such an array can be created/declared by:
      int myInts[6];
      int myPins[] = {2, 4, 8, 3, 6};
      int mySensVals[5] = {2, 4, -8, 3, 2};
      char message[6] = "hello";
      String someNames[3] = { "john", "bill", "mary" };

    Notice you do not need to explicitly declare the size.
    Also note that if you are declaring an array of type char, the length must be one more than the actual number of characters (to hold the ending null char of the string).

  • Access/retrieve an element of an array by referring to its index number.
    The first element is index = 0.
    From the above examples:
      x = myPins[2] = 8
      y = message[1] = "e"

  • To set/assign a specific value to a given array element:
      message[1] = "o"
      will change message to = "hollo"

    ----------------------------------------------------------------------------------

  • A 2-dimensional array can be thought of as a table or rows and columns
      int array1[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
      int array2[1][5] = { 1, 2, 3, 4, 5 };

    ----------------------------------------------------------------------------------

  • A structure is a user-defined data type. The declaration of an array of structures is the same as an array of the primitive data types but uses the structure as its elements' data typ
    https://www.delftstack.com/howto/c/c-array-of-structs/
  • A structure array is a data type that groups related data using data containers called fields. Each field can contain any type of data. Access data in a field using dot notation of the form structName.
    https://www.mathworks.com/help/matlab/ref/struct.html
    The variables within a structure are called members.

  • Declare/create a struct:
    struct structName {
      item1_type item1_name;
      item2_type item2_name;
      .
      .
      itemN_type itemN_name

    }

  • Examples:
    struct Student {
      int idNumber;
      char studentName[10];
      float GPA;

    };

  • Declare an array of structures:
      struct Student studentRecord[5];

    The studentRecord is an array of 5 elements where each element is of type struct Student.
    The individual elements are accessed using the index notation ([]), and members are accessed using the dot . operator.
    The . (dot) notation can be used for both reading the elements of a struct, or changing them.


    The studentRecord[0].idNumber refers to the idNumber member of the 0th element of the array.

    ------------------------------------------------------------------------------

  • typedef struct
    https://www.norwegiancreations.com/2017/10/getting-started-with-programm...
    • The syntax for typedef is:
      typedef int bob;
      bob my_variable = 0;

      bob is now a different name for int. my_variable is a variable of the data type int. Then why use typedef at all in this code? Good question! That’s why the code is silly.

      Another example:
      typedef unsigned long *ulongptr;
      ulongptr a;

      The above code makes a an unsigned long pointer variable.


    Many tend to use struct and typedef in tandem like this:

    typedef struct{
    int R;
    int G;
    int B;
    } color;

    color LED_color = {255, 0, 0};

    One advantage by doing this is that you don’t have to write struct every time you declare a variable of this type (like we did in the last chapter on code snippet line 7 and 9). Some consider this a bad practice since a lot of typedefing can clutter already busy namespaces in larger programs. This is however rarely an issue with small to medium-sized projects.
    --------------------------

    typedef struct example:

    In plain C, structs are identified by "struc tags", which do not live in the same namespace as type names. Thus, if you declare

    struct RGB {
    ...
    };

    a variable of this type should be declared as

    struct RGB led;

    In order to avoid the inconvenience of repeatedly typing struct, a common idiom is to define it as a type:

    typedef struct {
    ...
    } RGB;

    RGB led;

    The Arduino environment, however, is based on the C++ programming language. In C++ struct tags are types, and you thus do not need the typedef:

    struct RGB {
    ...
    };

    RGB led;

    This is the recommended idiom in C++: just remove the typedef.

    ---------------------------------------------------------
    To use your typedef you should put the struct "name" after the braced parenthesis:

    typedef struct
    {
    double r;
    double g;
    double b;
    } RGB;

    -----------------------------------------

.

.

.

eof