CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Structures Gabriel Hugh Elkaim Spring 2013.

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Programming in C Chapter 10 Structures and Unions
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
Structures Often we want to be able to manipulate logical entities as a whole For example, complex numbers, dates, student records, etc Each of these must.
C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Structure.
Structures Spring 2013Programming and Data Structure1.
Data Types C built-in data types –char, int, float, double, int*, etc. User-defined data types: the programmer can define his/her own data types which.
Structures, Unions, and Typedefs CIS 1057 Fall Structures, Unions, and Typedefs CIS 1057 Computer Programming in C Fall 2013 (Many slides based on/borrowed.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
CMPE 293Gabriel Hugh Elkaim 1. CMPE 293Gabriel Hugh Elkaim 2.
CMPE 293Gabriel Hugh Elkaim 1. CMPE 293Gabriel Hugh Elkaim 2.
1 Structures. 2 C gives you several ways to create a custom data type. –The structure, which is a grouping of variables under one name and is called an.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Functions Gabriel Hugh Elkaim Spring 2012.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Modules and Scope Gabriel Hugh Elkaim Spring 2013.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Pointers Gabriel Hugh Elkaim Spring 2012.
Array, Structure and Union
1 6.1 Basics of Structures 6.2 Structures and Functions 6.3 Arrays of Structures 6.4 Pointers to Structures 6.5 Self-referential Structures + Linked List.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Structures Combining data types into a logical groupings.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Unions and Bitfields Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Computer Systems and “C” Programming Gabriel Hugh Elkaim Spring 2013.
The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Enumerations Gabriel Hugh Elkaim Spring 2013.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Exercises in MPLAB-X Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Function Pointers Gabriel Hugh Elkaim Spring 2013.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
CE-2810 Dr. Mark L. Hornick 1 “Classes” in C. CS-280 Dr. Mark L. Hornick 2 A struct is a complex datatype that can consist of Primitive datatypes Ints,
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Chapter 10-1: Structure.
Buy book Online -
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Using Arrays in C Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning.
Structures.
Structure ការណែនាំអំពី Structure
הגדרת משתנים יום שלישי 27 נובמבר 2018
C Programming Lecture-8 Pointers and Memory Management
C Programming Lecture-13 Structures
Structures.
Structure (i.e. struct) An structure creates a user defined data type
CSCE 206 Lab Structured Programming in C
Presentation transcript:

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Structures Gabriel Hugh Elkaim Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 DefinitionStructures Structures: –May contain any number of members –Members may be of any data type –Allow group of related variables to be treated as a single unit, even if different types –Ease the organization of complicated data Structures are collections of variables grouped together under a common name. The variables within a structure are referred to as the structure’s members, and may be accessed individually as needed.

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax Structures How to Create a Structure Definition // Structure to handle complex numbers struct complex { float re; // Real part float re; // Real part float im; // Imaginary part float im; // Imaginary part} struct structName { type 1 memberName 1 ; type 1 memberName 1 ; type n memberName n ; type n memberName n ;} Members are declared just like ordinary variables

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax struct structName { type 1 memberName 1 ; type 1 memberName 1 ; type n memberName n ; type n memberName n ; } varName 1,...,varName n ; Structures How to Declare a Structure Variable (Method 1) // Structure to handle complex numbers struct complex { float re; float re; float im; float im; } x, y; // Declare x and y of type complex

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax struct structName varName 1,…,varName n ; Structures How to Declare a Structure Variable (Method 2) struct complex { float re; float re; float im; float im;}... struct complex x, y; // Declare x and y of type complex If structName has already been defined:

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax Structures How to Use a Structure Variable structVariableName.memberName struct complex { float re; float re; float im; float im; } x, y; // Declare x and y of type complex int main(void) { x.re = 1.25; // Initialize real part of x x.re = 1.25; // Initialize real part of x x.im = 2.50; // Initialize imaginary part of x x.im = 2.50; // Initialize imaginary part of x y = x; // Set struct y equal to struct x y = x; // Set struct y equal to struct x......

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax Structures How to Create a Structure Type with typedef // Structure type to handle complex numbers typedef struct { float re; // Real part float re; // Real part float im; // Imaginary part float im; // Imaginary part } complex; typedef struct structTag optional { type 1 memberName 1 ; type 1 memberName 1 ; type n memberName n ; type n memberName n ; } typeName;

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax typeName varName 1,…,varName n ; Structures How to Declare a Structure Type Variable typedef struct { float re; float re; float im; float im; } complex;... complex x, y; // Declare x and y of type complex If typeName has already been defined: The keyword struct is no longer required!

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax Structures How to Initialize a Structure Variable at Declaration typeName varName = {const 1,…,const n }; typedef struct { float re; float re; float im; float im; } complex;... complex x = {1.25, 2.50}; // x.re = 1.25, x.im = 2.50 If typeName or structName has already been defined: struct structName varName = {const 1,…,const n }; - or -

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Structures Nesting Structures typedef struct { float x; float x; float y; float y; } point; typedef struct { point a; point a; point b; point b; } line; int main(void) { line m; line m; m.a.x = 1.2; m.a.x = 1.2; m.a.y = 7.6; m.a.y = 7.6; m.b.x = 38.5; m.b.x = 38.5; m.b.y = 17.8; m.b.y = 17.8; a b (x a, y a ) = (1.2, 7.6) (x b, y b ) = (38.5, 17.8) x y m line point

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Strings: –May be assigned directly to char array member only at declaration –May be assigned directly to a pointer to char member at any time Example: StructureExample: Initializing Members Structures Arrays and Pointers with Strings struct strings { char a[4]; char a[4]; char *b; char *b; } str; int main(void) { str.a[0] = ‘B’; str.a[0] = ‘B’; str.a[1] = ‘a’; str.a[1] = ‘a’; str.a[2] = ‘d’; str.a[2] = ‘d’; str.a[3] = ‘\0’; str.a[3] = ‘\0’; str.b = “Good”; str.b = “Good”;

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Syntax Example 1Example 2 Structures How to Declare a Pointer to a Structure typeName *ptrName; typedef struct { float re; float re; float im; float im; } complex;... complex *p; If typeName or structName has already been defined: struct structName *ptrName; - or - struct complex { float re; float re; float im; float im;}... struct complex *p;

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example: DefinitionsExample: Usage Syntax Structures How to Use a Pointer to Access Structure Members ptrName->memberName If ptrName has already been defined: typedef struct { float re; float re; float im; float im; } complex; //complex type... complex x; //complex var complex *p; //ptr to complex int main(void) { p = &x; p = &x; //Set x.re = 1.25 via p //Set x.re = 1.25 via p p->re = 1.25; p->re = 1.25; //Set x.im = 2.50 via p //Set x.im = 2.50 via p p->im = 2.50; p->im = 2.50;} Pointer must first be initialized to point to the address of the structure itself: ptrName = &structVariable;

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax Structures Creating Arrays of Structures typeName arrName[n]; typedef struct { float re; float re; float im; float im; } complex;... complex a[3]; If typeName or structName has already been defined: struct structName arrName[n]; - or -

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax Structures Initializing Arrays of Structures at Declaration typedef struct { float re; float re; float im; float im; } complex;... complex a[3] = {{1.2, 2.5}, {3.9, 6.5}, {7.1, 8.4}}; typeName arrName[n] = {{list 1 },…,{list n }}; If typeName or structName has already been defined: struct structName arrName[n] = {{list 1 },…,{list n }}; - or -

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example: DefinitionsExample: Usage Syntax Structures Using Arrays of Structures typedef struct { float re; float re; float im; float im; } complex;... complex a[3]; int main(void) { a[0].re = 1.25; a[0].re = 1.25; a[0].im = 2.50; a[0].im = 2.50;......} arrName[n].memberName If arrName has already been defined:

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Structures How to Pass Structures to Functions typedef struct { float re; float re; float im; float im; } complex; void display(complex x) { printf(“(%f + j%f)\n”, x.re, x.im); printf(“(%f + j%f)\n”, x.re, x.im);} int main(void) { complex a = {1.2, 2.5}; complex a = {1.2, 2.5}; complex b = {3.7, 4.0}; complex b = {3.7, 4.0}; display(a); display(a); display(b); display(b);}

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Lab Exercise 14 Structures

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 14 Structures Open the project’s workspace: 1 1 Open MPLAB ® and select Open Workspace… from the File menu. Open the file listed above. If you already have a project open in MPLAB, close it by selecting Close Workspace from the File menu before opening a new one. /Examples/Lab14.zip -> Load “Lab14.mcw” On the class website

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 14 Structures /*############################################################################ # STEP 1: Calculate the difference between maximum and minimum power in # circuit 1 using the individual power structures (i.e. variables # PMax1 & PMin1). Algebraic Notation: # Pdiff = (Vmax * Imax) - (Vmin * Imin) ############################################################################*/ powerDiff1 = (PMax1.v * PMax1.i) - (PMin1.v * PMin1.i); powerDiff2 = (PMax2.v * PMax2.i) - (PMin2.v * PMin2.i); powerDiff3 = (PMax3.v * PMax3.i) - (PMin3.v * PMin3.i); /*############################################################################ # STEP 2: Calculate the difference between maximum and minimum power in # circuit 1 using the structure of structures (i.e. variable PRange1). # Algebraic Notation: Pdiff = (Vmax * Imax) - (Vmin * Imin) ############################################################################*/ powerDiff1 = (PRange1.max.v * PRange1.max.i) - (PRange1.min.v * PRange1.min.i); powerDiff2 = (PRange2.max.v * PRange2.max.i) - (PRange2.min.v * PRange2.min.i); powerDiff3 = (PRange3.max.v * PRange3.max.i) - (PRange3.min.v * PRange3.min.i); Solution: Steps 1 and 2

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 14 Conclusions Structures make it possible to associate related variables of possibly differing types under the same name Structure members (using the dot notation) may be used anywhere an ordinary variable would be used Pointers to structures make it possible to copy one entire structure to another very easily

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Lab Exercise 15 Arrays of Structures

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 15 Arrays of Structures Open the project’s workspace: 1 1 Open MPLAB ® and select Open Workspace… from the File menu. Open the file listed above. If you already have a project open in MPLAB, close it by selecting Close Workspace from the File menu before opening a new one. /Examples/Lab15.zip -> Load “Lab15.mcw” On the class website

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 15 Arrays of Structures /*############################################################################ # STEP 1: Multiply the real (re) part of each array element by 10 # HINT: Use *= ############################################################################*/ //Multiply re part of current array element by 10 x[i].re *= 10; /*############################################################################ # STEP 2: Multiply the imaginary (im) part of each array element by 5 # HINT: Use *= ############################################################################*/ //Multiply im part of current array element by 5 x[i].im *= 5; Solution: Steps 1 and 2

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 15 Conclusions Arrays of structures allow groups of related structures to be referenced by a common name Individual structures may be referenced by the array index Individual structure members may be referenced by the dot notation, in conjunction with the array name and index

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Questions?

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013