Microcontroller Systems. ELET 3232 Topic 8: Structures, Arrays, & Pointers

Similar documents
Memory, Data, & Addressing II CSE 351 Spring

Computer Organization & Systems Exam I Example Questions

INTRODUCTION 1 AND REVIEW

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

Type Checking. Prof. James L. Frankel Harvard University

Binghamton University. CS-211 Fall Pointers vs. Arrays

CSE351: Memory, Data, & Addressing I

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

The syntax of structure declaration is. struct structure_name { type element 1; type element 2; type element n;

primitive arrays v. vectors (1)

CS561 Manju Muralidharan Priya Structures in C CS200 STRUCTURES. Manju Muralidharan Priya

Where we are going (today)

Input And Output of C++

3.3 Structures. Department of CSE

Hardware: Logical View

Memory, Data, & Addressing II

C Programming Language

Multi-Dimensional arrays

Module 6: Array in C

IV Unit Second Part STRUCTURES

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Survey. Motivation 29.5 / 40 class is required

ONE DIMENSIONAL ARRAYS

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Where we are going (today)

Representation of Information

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

9. Arrays. Compound Data Types: type name [elements]; int billy [5];

CMPE-013/L. Introduction to C Programming

Structures Unions and Enumerated Datatypes 224

Binary Encodings for JavaScript Object Notation: JSON-B, JSON-C, JSON-D

Final Exam. 12 December 2018, 120 minutes, 26 questions, 100 points

Structure, Union. Ashishprajapati29.wordpress.com. 1 What is structure? How to declare a Structure? Explain with Example

Binghamton University. CS-211 Fall Pointers vs. Arrays

Practice problems Set 2

A First Book of ANSI C Fourth Edition. Chapter 12 Structures

Review of the C Programming Language for Principles of Operating Systems

Lecture 5: Arrays. A way to organize data. MIT AITI April 9th, 2005

SC2004MBS 20x4 Characters MODBUS RTU Slave LCD

Computer Programming

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Chapter 7: Structuring the Data. Lecture7 1

Today's Topics. CISC 458 Winter J.R. Cordy

a data type is Types

Analyzing the command string for switch to input #

Prof. Carl Schultheiss MS, PE. CLASS NOTES Lecture 12

CS C Primer. Tyler Szepesi. January 16, 2013

UW CSE 351, Winter 2013 Final Exam

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Review of the C Programming Language

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Data Representation and Storage

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Accelerating Information Technology Innovation

Data Representation and Storage. Some definitions (in C)

Overview. The C programming model. The C programming model. The C programming model. The C programming model 1/23/2009. Real-time Systems D0003E

Application Note. Introduction AN2471/D 3/2003. PC Master Software Communication Protocol Specification

C++ assign array values. C++ assign array values.zip

Programming. Elementary Concepts

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

Example: Structure, Union. Syntax. of Structure: struct book { char title[100]; char author[50] ]; float price; }; void main( )

CE4DMID01 COMMUNICATION PROTOCOL CONTENTS 1.0 INTRODUCTION

COMP2121: Microprocessors and Interfacing. Instruction Set Architecture (ISA)

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

CSCI 356 Fall 2017 : Practice Exam I DO NOT OPEN EXAM PACKET UNTIL INSTRUCTED TO DO SO YOU MAY FILL IN INFORMATION ON THE FRONT NOW

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

EB289. Motorola Semiconductor Engineering Bulletin. C Macro Definitions for the MC68HC11F1 By John Bodnar Austin, Texas. Freescale Semiconductor, I

Tutorial 1: C-Language

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

ME 461 C review Session Fall 2009 S. Keres

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

bytes per disk block (a block is usually called sector in the disk drive literature), sectors in each track, read/write heads, and cylinders (tracks).

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Protocol of SOJI Fuel level sensor FJ-RS232/FJ-RS485 Version 1.0.2

SLCD Technical Note TN-100. Programming the SLCD bitmap / macro flash memory in an embedded system

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

Memory, Data, & Addressing I

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

BASIC ELEMENTS OF A COMPUTER PROGRAM

Student Name: (in Capital Letters) CSE Introduction to Programming for Engineers and Scientists. Final Exam

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Introduction to C++ with content from

C Programming Review CSC 4320/6320

Order this document by EB285/D Motorola Semiconductor Engineering Bulletin EB285 C Macro Definitions for the MC68HC(7)11E20 By John Bodnar

C++ Structures Programming Workshop 2 (CSCI 1061U)

LEXICAL 2 CONVENTIONS

Week 3 Lecture 2. Types Constants and Variables

IF96017 MODBUS COMMUNICATION PROTOCOL

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

EB287. Motorola Semiconductor Engineering Bulletin. C Macro Definitions for the MC68HC(7)11E9/E8/E1/E0. Freescale Semiconductor, I.

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

CS 241 Data Organization Binary

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Chapter-14 STRUCTURES

Transcription:

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