Structs Adapted from Dr. Mary Eberlein, UT Austin.

Slides:



Advertisements
Similar presentations
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
Advertisements

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.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structures Spring 2013Programming and Data Structure1.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Structures Lesson CS1313 Spring Structures Lesson Outline 1.Structures Lesson Outline 2.Beyond Arrays 3.A Company and Its Employees #1 4.A Company.
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.
CSC Programming for Science Lecture 36: Structures.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
A Second Look At Classes And Objects Chapter 9. Static Class Members When a value is stored in a static field, it is not in an object of the class. In.
STRUCTURES. C structures: aggregate, yet scalar  aggregate in that they hold multiple data items at one time  named members hold data items of various.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
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 11/30/05CS150 Introduction to Computer Science 1 Structs.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
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.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
LINKED LISTS.
Structured Data.
Pointers and Dynamic Arrays
Structures, Unions, Enumerations
CSE 220 – C Programming Pointers.
CS1010 Programming Methodology
Chapter 11: Structured Data.
Complex Data Types: Arrays & Structs Topic 7
Visit for more Learning Resources
CS150 Introduction to Computer Science 1
Module 2 Arrays and strings – example programs.
CS 108 Computing Fundamentals Tuesday, November 14, 2017.
C++ Structs.
Buy book Online -
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Pass by Reference, const, readonly, struct
Chapter 3 Introduction to Classes, Objects Methods and Strings
Array of Structures A structure holds only one record But a record
Arrays and Arrays as Parameters
Strings A collection of characters taken as a set:
Return by Reference CSCE 121 J. Michael Moore.
Exam 4 review Copyright © 2008 W. W. Norton & Company.
Classes and Objects.
CS111 Computer Programming
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Simulating Reference Parameters in C
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
CPS120: Introduction to Computer Science
Structure.
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Structures In C Programming By Rajanikanth B.
Java Programming Language
Lec17 Structs.
Introduction to Computer Programming Lecture 16 Structures (Part 1)
Structure (i.e. struct) An structure creates a user defined data type
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Chapter 10 C Structures and Unions
Chapter 11 Structure and Union Types.
Structures, Unions, and Enumerations
Week 9 - Monday CS222.
Presentation transcript:

structs Adapted from Dr. Mary Eberlein, UT Austin

Structures struct: Collection of variables with one name the variables are called members or fields may be different types Use structs to keep related data together pass fewer arguments return multiple values as a struct

Example A struct for data related to our class: Or use a typedef: structure tag A struct for data related to our class: struct eeClass { int classNum; char meetingRoom[20]; char courseName[30]; }; Variable declaration: struct eeClass ee312; Member (or field) access: ee312.classNum = 312; Or use a typedef: typedef struct eeClass{ int classNum; char meetingRoom[20]; char courseName[30]; } EEClass; EEClass ee312; ee312.classNum = 312; strcpy(ee312.meetingRoom, "EER3");

structs Record containing related data values that are used together Fields stored in contiguous memory Like an array, except: data values in struct have names access fields with "dot" operator, not [] Suppose you are writing a class scheduling system. You'd probably need to pass the same set of data to many functions. void catalog(char courseName[], int courseNumber, int secID) {…} Instead: combine that data in a struct Short for "structured data type" Bundle set of data into a single thing called a struct.

Structures Now your function might look like this: struct UTCourse { char courseName[50]; int courseNumber; int secID; }; struct UTCourse EE312_00 = {"Software Design & Implementation I", 312, 16100}; Now your function might look like this: void catalog(struct UTCourse myClass) {…}

Field Access Use the dot operator to access fields (and the variable name) typedef struct FullName { char first[20]; char last[20]; } FullName; FullName me = {“Roger", “Priebe"}; printf("Hey %s\n", me.last);

Designated Initializers (C99) typedef struct name { char first[20]; char last[20]; } FullName; Value can be labeled with the field name: FullName person = {.last = "Presley", .first = "Elvis"}; If field omitted from initializer, set to 0

Operations on structs The . access operator has precedence over nearly all other operators Example: typedef struct { int partNum; char partName[30]; int onHand; } Part; Part part1 = {.partNum = 311, .partName = "usb"}; scanf("%d", &part1.onHand); // . operator higher precedence than & Assigning one struct to another makes a copy: part2 = part1; // copy each field of part1 into part2 Note: structs cannot be compared with == or !=

Passing structs Function call: printName(person); Output: void printName(struct Name p) { printf("First name: %s\n", p.first); printf("Last name: %s\n", p.last); } Function call: printName(person); Output: First name: Elvis Last name: Presley Passing a structure to a function or returning a struct both require making a copy of all fields in the structure.

struct return values Function call: struct Name makeAName(char *firstName, char* lastName) { struct name elvis; strcpy(elvis.first, firstName); strcpy(elvis.last, lastName); return elvis; } Function call: struct Name theKing = makeAName("Elvis", "Presley");

Example #include<stdio.h> #include<string.h> typedef struct { char first[20]; char last[20]; } FullName; void printName(FullName p); FullName makeAName(char *firstName, char *lastName); int main() { FullName onePerson = makeAName("Bruce", "Lee"); printName(onePerson); FullName someone = onePerson; printName(someone); } Output: First name: Bruce Last name: Lee Firstname: Bruce