Microcontroller Systems ELET 3232 Topic 8: Structures, Arrays, & Pointers 1
Agenda Become familiar with and apply: Arrays Structures Pointers 2
Array Arrays A data set of a particular data type All elements of an array must be the same type Declared like any other variable except that you include the number of required array elements Ex: int digits[10]; char name[20]; 3
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 }; 4
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 }; 5
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 6
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 7
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) 8
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: Columns 0 1 2 3 int matrix [3][4] = { 0, 1, 2, 3 4, 5, 6, 7 8, 9, 10, 11}; 0 Rows 1 2 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}; What is the value of matrix [2][2]? 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]? 10 11
STRUCTURES 12
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, Delmar Learning, 2 edition 13
Structure Structure declaration format struct structure_tag_name { type variable1; type variable2; : type variablex; }; 14
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]; An array of 20 employee_info data types referred to as employee. In the structure is their name (20 characters), their employee id number (9 characters), their birthdates (month, day, year). 15
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 16
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 17
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 the 1 st letter of an employee s id you would write: employee[x].id[0] 18
Nested Structures Nested Structures struct employee_info { char name[20]; char id[9]; struct BIRTH birthdate; } employee[20]; struct BIRTH { char month; char day; int year; }; 19
Nested Structures Example Example struct employee_info { char name[20]; char id[9]; struct BIRTH birthdate; } employee[20]; struct BIRTH { char month; char day; int year; }; To access the 4 th employee s year of birth you would write: employee[3].birthdate.year 20
Structures Example Example Construct a structure that holds the dimensions and the volume of a box. Declare a variable Box_1 that is of type box. 21
Structures Example Example Construct a structure that holds the dimensions and the volume of a box. Declare a variable Box_1 that is of type box. struct box { float height; float width; float depth; float volume; }; struct box Box_1; 22
Structures Example Example Initialize Box_1 so that its height is 2.3 in, width is 8.5 in and its depth is 4 in. 23
Structures Example Example Initialize Box_1 so that its height is 2.3 in, width is 8.5 in and its depth is 4 in. Box_1.height = 2.3; Box_1.width = 8.5; Box_1.depth = 4; 24
POINTERS 25
Pointers Pointers: Variables that contain the address of a variable, constant, function, or data object Pointer data type allocates enough memory to hold the address Usually a 16 or 24 bit address (depending on the amount of memory) Data may be 8-bit (char), or 16-bit (int), or 32-bit (float, long int). In this case the data is 8 bits wide. Data 0x3F 0x5A 0xF1 0x32 0x9B 0x38 0x4E Address 0x3248 0x3249 0x324A 0x324B 0x324C 0x324D 0x324E 16-bit addresses 26
Pointers Pointers: Declared with the indirection operator: * Assigned with the address operator: & Ex: Variable c may be assigned to address 0x324A, but the value of c is 0xF1 char *p; //p is a pointer to a character char c; //c is a character (unsigned 8-bit integer) c = 0xF1; //c is assigned the value (hex) 0xF1 p = &c; //p is assigned the address of c (p is pointing to c) c Data 0x3F 0x5A 0xF1 0x32 0x9B 0x38 0x4E Address 0x3248 0x3249 0x324A 0x324B 0x324C 0x324D 0x324E Address of c c is an 8-bit char, but p is a 16-bit address (a pointer) to a char 27
Pointers Example: char *p; char a, b; a =10; b =45; //p is a pointer to a character //a and b are characters (unsigned 8-bit integer) p = &a; //p is assigned the address of a (p is pointing to a) *p = b; //the memory location pointed to by p is assigned the value of b What is the value of a and b when this segment of code is finished? 28
Pointers Example: char *p; char a, b; a =10; b =45; //p is a pointer to a character //a and b are characters (unsigned 8-bit integer) p = &a; //p is assigned the address of a (p is pointing to a) *p = b; //the memory location pointed to by p is assigned the value of b What is the value of a and b when this segment of code is finished? a = b = 45; Equivalent to a = b; 29
Pointers Example: unsigned char *Port_B; Port_B = 0x1010; *Port_B = 0x38; What is the address of Port_B and what data is stored there? 30
Pointers Example: unsigned char *Port_B; Port_B = 0x1010; *Port_B = 0x38; What is the address of Port_B and what data is stored there? Port_B Address = 0x1010 Port_B Data = 0x38 31
Summary Applied: Arrays Structures Pointers 32