Docsity

Embedded Systems Programming: Topic 8 - Arrays, Structures, & Pointers - Prof. Stephen Kuy, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Embedded Systems Programming: Topic 8 - Arrays, Structures, & Pointers - Prof. Stephen Kuy and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Embedded Systems Programming

ETEE 3285

Topic 8: Structures, Arrays, & Pointers

Agenda

 Become familiar with and apply:

 Arrays

 Structures

 Pointers

Array Initialization

 Arrays

 An array may be initialized as it’s declared

 Place the values within braces

 Separate by commas

 Ex:

int digits[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; char name[10] = {B, e, a, u b, a, d, i, n, e };

Array Indexing

 A specific element of an array may be

referenced through its index (or subscript)

 Ex:

 The first element of digits is digits[0]

 The ninth element of name is “n”

int digits[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; char name[10] = {B, e, a, u b, a, d, i, n, e };

Array Indexing

 A specific element of an array may be

referenced through its index (or subscript)

 Ex:

 The first element of digits is digits[0]

 The ninth element of name is “n”

int digits[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; char name[10] = {B, e, a, u b, a, d, i, n, e };

Index 0 through 9

Multidimensional Arrays

 C supports multidimensional arrays

 May be two, three, four or more dimensions

 Adjacent memory locations are referenced by

the right-most index

 Example:

int matrix [3][4] = { 0, 1, 2, 3 4, 5, 6, 7 8, 9, 10, 11};

matrix [1][2] = 6 the 5 and 7 are stored in adjacent memory locations (as opposed to the 2 and the 10)

Multidimensional Arrays

 C supports multidimensional arrays

 May be two, three, four or more dimensions

 Adjacent memory locations are referenced by

the right-most index

 Example:

int matrix [3][4] = { 0, 1, 2, 3 4, 5, 6, 7 8, 9, 10, 11};

What is the value of matrix [2][2]?

Multidimensional Arrays

 C supports multidimensional arrays

 May be two, three, four or more dimensions

 Adjacent memory locations are referenced by

the right-most index

 Example:

int matrix [3][4] = { 0, 1, 2, 3 4, 5, 6, 7 8, 9, 10, 11};

What is the value of matrix [2][2]?

10

Structure

 Structure

 A group of related variables under one name

 Used to create a single data object from one or more

variables

 Variables within the structure are called members

 Structures are the fundamental element in OOP

 Structures can be thought of as an object

 Members are the properties of the object

 Members of a structure do not need to be of the same data

type

Richard H. Barnet, Sarah Cox, Larry O'Cull (2006), Embedded C Programming And The Atmel AVR, 13 Delmar Learning, 2 edition

Structure

 Structure declaration format

struct structure_tag_name { type variable1; type variable2; : type variableX; };

Structure Example

 Structure example

struct structure_name { type variable1; type variable2; : type variableX; };

struct employee_info { char name[20]; char id[9]; char month; char day; int year; } employee[20];

To access one of the members of the structure you use the member operator “.” So, to access the month of birth for each employee you would write:

employee[x] .month

Structure Example

 Structure example

struct structure_name { type variable1; type variable2; : type variableX; };

struct employee_info { char name[20]; char id[9]; char month; char day; int year; } employee[20];

To access an employee’s id you would write:

employee[x] .id

Nested Structures

 Nested Structures

struct BIRTH { char month; char day; int year; };

struct employee_info { char name[20]; char id[9]; struct BIRTH birthdate; } employee[20];

Nested Structures Example

 Example

struct BIRTH { char month; char day; int year; };

To access the 4th^ employee’s year of birth you would write:

employee[3] .birthdate.year

struct employee_info { char name[20]; char id[9]; struct BIRTH birthdate; } employee[20];