CS 108 Computing Fundamentals April 7, 2015. GHP # 10 and GHP 11 GHP 10 will be posted later today ­ Due: Saturday, April 11, at midnight GHP 11 will.

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.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Programming in C Chapter 10 Structures and Unions
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
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.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
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.
Structures in C.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Lecture No.01 Data Structures Dr. Sohail Aslam
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
Functions, Pointers, Structures Keerthi Nelaturu.
Introduction to Data Structures Systems Programming.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
© 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.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Structures Lesson CS1313 Spring Structures Lesson Outline 1.Structures Lesson Outline 2.Beyond Arrays 3.A Company and Its Employees #1 4.A Company.
Introduction to Data Structures Systems Programming Concepts.
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.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Pointers *, &, array similarities, functions, sizeof.
Structures and Enumerations Introduction Structure definitions Nested structure Referring and initializing structure elements Passing structures to a function.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 12 – C: structs, linked lists, and casts.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Arrays.
1 CS161 Introduction to Computer Science Topic #15.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Module 1: Array ITEI222 - Advance Programming Language.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
CSC Programming for Science Lecture 37 : Course Review.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
CS 108 Computing Fundamentals Notes for Thursday, February 18, 2016.
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.
CS 108 Computing Fundamentals April 28, Defining Variables As we’ve discussed many times, defining a variable does all of the following –Gives us.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
User-Written Functions
Computer Science 210 Computer Organization
CSE 374 Programming Concepts & Tools
CS 108 Computing Fundamentals Tuesday, November 14, 2017.
Computer Science 210 Computer Organization
C Structures, Unions, Bit Manipulations and Enumerations
Week 9 - Monday CS222.
Presentation transcript:

CS 108 Computing Fundamentals April 7, 2015

GHP # 10 and GHP 11 GHP 10 will be posted later today ­ Due: Saturday, April 11, at midnight GHP 11 will be posted on Thursday ­ Due: Thursday, April 16 at 8 AM

Structures Thus far we have used variables which have been atomic in nature ­ single data types (int, double, char, etc…) ­ these atomic variables cannot be "broken down" into anything simpler and still maintain identity/meaning C provides us with the capability to create composite data types and use them to create composite variables A composite data type is called a structure (struct) A structure can contain both "built-in" data types (int, double, char, etc…) and/or other structures

Structures Structures are the basis for more advanced data structures (more advanced than arrays or multi-dimensional arrays) Structures combined with pointers can be used to build linked data structures such as lists, stacks, queues, trees, graphs, etc… (CS 240 and beyond) We need these more advanced data structures to allow us to model more complex systems and solve more difficult problems Structures are important in computing… without them it would extremely difficult (if not impossible) to implement concepts like relational databases, file processing, and object-orientation

Structures Structures allow us to much more accurately describe (or model) an object Think of an object as a noun… a person, place, thing, or event Examples of objects: students, courses, majors, flights, doctors, concerts, musical groups, teams, players, etc…

Structures An object can be described or modeled by listing the unique characteristics of the object A course can be described by title, credit hours, prerequisites, description, meeting time, instructor, etc… A student can be described by first name, last name, ssn, major, gpa, date-of-birth, etc… Notice that the "characteristics" must have different data types We can use a structure to create a model of an object

Structures Creating a structure is a two-step process 1) create a structure definition ("defines" a structure framework) 2) create an instance of a defined structure Memory is not actually allocated until an instance is created Syntax of a structure definition: struct structure_name { data_type name_of_element_1 ; data_type name_of_element_2 ;... data_type name_of_element_n ; } ;

Structures Example of a structure definition: struct student { char first_name[20] ; char last_name[20] ; char ssn[10] ; char major[50] ; float gpa ; } ; Each listed characteristic of a structure definition is called an element

Structures Syntax for creating an instance of a defined structure: struct structure_name instance_name ; Example of creating an instance of a defined structure : struct student student_1 ; student_1 is a variable name that is an instance of the student structure… this means that student_1 has all the characteristics and properties of the student structure definition

Structures Because a variable that is the instance of a structure may contain many elements, each element must be reachable… we use "dot notation" via the ". " operator struct student { char first_name[20]; char last_name[20]; char ssn[10]; char major[50]; float gpa ; } ; struct student student_1 ; student_1.first_name = "Chris" ; student_1.ssn = " " ; student_1.gpa = 2.0 ;

We can initialize the instance of a structure while we are creating it: struct student student_1 = {"Chris", "Urban", " ", "Computer Science", 2.0} ; Notice that it is possible to use the = operator on a structure variable, but only when creating a structure instance. When the = sign is used, each element of the structure at the right hand side is copied into the structure at the left hand side "in order" … if you mess-up the order then your input will be useless trash (if it doesn’t crash). An example of a mess: struct student student_1 = { 2.0, " ", "Computer Science", "Chris", "Urban" } ; Structures

#include // 1.c #include struct student { char first_name[20] ; char last_name[20] ; char ssn[10] ; char major[50] ; float gpa ; } ; int main ( void ) { struct student student_1; strcpy(student_1.major, "Computer Science" ); // The order is not strcpy(student_1.first_name, "Chris" );// important when strcpy(student_1.last_name, "Urban" );// directly assigning strcpy(student_1.ssn, " " ); // element values. student_1.gpa = 2.0; printf("\n\nLast name: %s", student_1.last_name); printf("\nSSN: %s \n\n\n", student_1.ssn); return (0) ; }

typedef typedef is used to create aliases for structure definitions by providing a structure tag name A structure tag name (alias) can be used to assign a specific structure to a variable (create an instance of a structure) Using the structure tag name (alias) provides a mechanism for declaring an instance of a structure that is very similar to the mechanism we have used to declare variables of standard data types Essentially, typedef allows us to create our own data types

typedef Creating a structure using typedef is (also) a two-step process: ­ 1) use typedef and struct to create a structure definition with a structure tag name (alias) ­ 2) create an instance of a typedef defined structure using struct and a structure tag name Syntax of a structure definition using typedef and a structure tag name: typedef struct structure_name { data_type name_of_element_1; data_type name_of_element_2;... data_type name_of_element_n ; } tag_name ;

typedef Example of a structure definition using typedef and a structure tag: typedef struct student { char first_name[20] ; char last_name[20] ; char ssn[10] ; char major[50] ; float gpa ; } stdnt ; Again, each listed characteristic is called an element The name of the structure and the tag name may be the same… I typically don't make them the same to emphasize when I'm using the structure name or the structure tag

typedef Syntax for creating an instance of a structure that has been defined using typedef and a structure tag: structure_tag_name instance_name ; Example of creating an instance of a defined structure : stdnt student_1 ; student_1 is a variable name that is of type stdnt which means that student_1 is an instance of the student structure Using typedef makes passing structures to functions a little easier (more on this in a few slides)

#include //2.c #include typedef struct student { char first_name[20]; char last_name[20]; char ssn[10]; char major[50]; float gpa ; } stdnt ; int main ( void ) { stdnt student_1 ; strcpy(student_1.major, "Computer Science"); // The order is not strcpy(student_1.first_name, "Chris");// important when strcpy(student_1.last_name, "Urban");// directly assigning strcpy(student_1.ssn, " ");// element values. student_1.gpa = 2.0; printf("\n\nLast name: %s", student_1.last_name); printf("\nSSN: %s \n\n\n", student_1.ssn); return (0) ; }

#include //2a.c… the structure name and tag are the same #include typedef struct student { char first_name[20]; char last_name[20]; char ssn[10]; char major[50]; float gpa ; } student ; int main ( void ) { student student_1 ; // I'm defining this variable using the structure tag strcpy(student_1.major, "Computer Science"); // The order is not strcpy(student_1.first_name, "Chris");// important when strcpy(student_1.last_name, "Urban");// directly assigning strcpy(student_1.ssn, " ");// element values. student_1.gpa = 2.0; printf("\n\nLast name: %s", student_1.last_name); printf("\nSSN: %s \n\n\n", student_1.ssn); return (0) ; }

#include //2b.c… the structure name and tag are the same #include typedef struct student { char first_name[20]; char last_name[20]; char ssn[10]; char major[50]; float gpa ; } student ; int main ( void ) { struct student student_1 ; // I'm defining this variable using the structure name strcpy(student_1.major, "Computer Science"); // The order is not strcpy(student_1.first_name, "Chris");// important when strcpy(student_1.last_name, "Urban");// directly assigning strcpy(student_1.ssn, " ");// element values. student_1.gpa = 2.0; printf("\n\nLast name: %s", student_1.last_name); printf("\nSSN: %s \n\n\n", student_1.ssn); return (0) ; }

Arrays of Structures You can think of a single instance of a structure as a record from a database An array of structures is (conceptually) a collection of records… essentially a database table typedef struct student { char first_name[20]; char last_name[20]; char ssn[10]; char major[50]; float gpa ; } stdnt; stdnt student_records [ 10 ];

#include //3.c #include typedef struct student { char first_name[20] ; char last_name[20] ; } stdnt ; int main ( void ) { stdnt student_records [ 3 ]; int index = 0; strcpy(student_records[0].first_name, "Jane"); strcpy(student_records[0].last_name, "Jones"); strcpy(student_records[1].first_name, "Chris"); strcpy(student_records[1].last_name, "Urban" ); strcpy(student_records[2].first_name, "Peggy"); strcpy(student_records[2].last_name, "Flemming" ); for (index = 0; index < 3; index = index + 1) printf("\nFirst: \t %s \t\t Last: \t %s \n", student_records[index].first_name, student_records[index].last_name) ; printf("\n\n\n") ; return (0) ; }

Passing Structures By Value A structure must exist (we must create it) before a structure can be passed to a function Prototype syntax when typedef is not used to create the structure: function_return _type function_name ( struct structure_name instance_name ) ; // italics means "optional" Example of a PCF prototype if typedef is not used to create the structure: void printer (struct student ); // Notice that I did not use an // instance name in this prototype

Passing Structures By Value Prototype syntax when typedef is used to create the structure: function_return _type function_name ( structure_tag_name instance_name ) ; // italics means "optional" Example of a PCF prototype if typedef is used to create the structure: void printer ( stndt ); Note: stndt looks very similar to passing an int or char (for example) by value

#include //4.c #include typedef struct student { char first_name[20] ; char last_name[20] ; } stdnt ; void printer ( stdnt ) ; int main ( void ) { stdnt student_1; strcpy(student_1.first_name, "Jackie"); strcpy(student_1.last_name, "Jones"); printf("\n\n%s %s \n\n", student_1.first_name, student_1.last_name) ; printer (student_1); printf("%s %s \n\n", student_1.first_name, student_1.last_name) ; return (0); } void printer ( stdnt zoo ) { strcpy(zoo.first_name, "Sarah" ) ; printf("%s %s \n\n", zoo.first_name, zoo.last_name) ; }

Passing Structures By Reference Before learning to pass structures by reference, we must introduce another means of accessing elements of a structure: the structure pointer operator -> Syntax for using the structure pointer operator -> pointer_of_structure_type -> element_name Example of using the structure pointer operator -> student_ptr -> gpa = 2.0 ;

Passing Structures By Reference Example of using the structure pointer operator -> student_ptr -> gpa = 2.0 ; This can be very useful when a pointer of a structure type is created and we need to indirectly reference an element’s value

#include //5.c #include typedef struct student { char first_name[20]; char last_name[20]; } stdnt ; int main ( void ) { stdnt student_1; stdnt * st_ptr ; st_ptr = &student_1; strcpy( st_ptr->first_name, "Jackie"); strcpy( st_ptr->last_name, "Jones"); printf("\n\n%s %s \n\n", st_ptr->first_name, st_ptr->last_name) ; return (0); }

Passing Structures By Reference When you understand the structure pointer operator then passing structures by reference is not too difficult Essentially, it's the same as passing another variable by reference You need to include in the function prototype and definition that a pointer of structure type is going to be used Also need to use the structure pointer operator

#include //6.c #include typedef struct student { char first_name[20]; char last_name[20] ; } stdnt ; void printer ( stdnt * ); int main ( void ) { stdnt student_1; stdnt *st_ptr ; st_ptr = &student_1; strcpy(student_1.first_name, "Jackie"); strcpy(student_1.last_name, "Jones"); printf("\n\n%s %s \n\n", student_1.first_name, st_ptr->last_name) ; // Demo'ing use printer (st_ptr); // of structure printf("%s %s \n\n", st_ptr->first_name, student_1.last_name) ; // pointer return (0) ; // operator } void printer ( stdnt *zoo ) {strcpy(zoo->first_name, "Sarah"); printf("%s %s \n\n", zoo->first_name, zoo->last_name) ; return ; }

Passing Arrays of Structures Just like passing an array, passing an array of structures is automatically passing by reference Supply the function prototype and declaration with a pointer to the structure

#include //7.c … using only array notation #include typedef struct sq_cube { int input; int square; int cube ; } sq_c ; void populate ( sq_c [ ] ); int main ( void ) { sq_c table_1 [ 12 ] ; int index = 0; populate ( table_1 ) ; for (index = 0; index <12; index++) printf("\nInput: \t %d \t\t Square: \t %d \t\t Cube: \t %d\n", table_1[index].input, table_1[index].square, table_1[index].cube) ; return (0) ; } void populate ( sq_c ssqq_cc [ ] ) { int index = 0; for (index = 0; index <12; index++) { ssqq_cc[index].input = index ; ssqq_cc[index].square = index * index ; ssqq_cc[index].cube = index * index * index ; } return ; }

#include // 8.c… same a 7.c but using only pointer notation #include // with the exception of creating the array table_1 in main ( ) typedef struct sq_cube { int input; int square; int cube ; } sq_c ; void populate ( sq_c * ) ; int main ( void ) { sq_c table_1 [ 12 ] ; int index = 0; populate ( table_1 ) ; for (index = 0; index <12; index++) printf("\nInput: \t %d \t\t Square: \t %d \t\t Cube: \t %d\n", table_1[index].input, table_1[index].square, table_1[index].cube) ; return (0) ; } void populate ( sq_c * ssqq_cc ) { int index = 0; for (index = 0; index <12; index++) { (ssqq_cc + index)->input = index ; (ssqq_cc + index)->square = index * index ; (ssqq_cc + index)->cube = index * index * index ; } return ; }

#include //9.c… same as 7.c but using both array and pointer notation #include typedef struct sq_cube { int input; int square; int cube ; } sq_c ; void populate ( sq_c * ) ; int main ( void ) { sq_c table_1 [ 12 ] ; int index = 0; populate ( table_1 ) ; for (index = 0; index <12; index++) printf("\nInput: \t %d \t\t Square: \t %d \t\t Cube: \t %d\n", table_1[index].input, table_1[index].square, table_1[index].cube) ; return (0) ; } void populate ( sq_c * ssqq_cc ) { int index = 0; for (index = 0; index <12; index++) { ssqq_cc[index].input = index ; ssqq_cc[index].square = index * index ; ssqq_cc[index].cube = index * index * index ; } return ; }

#include // 10.c… same a 7.c but using both array and pointer notation #include // to the extreme, part 1 typedef struct sq_cube { int input; int square; int cube ; } sq_c ; void populate ( sq_c [ ] ); int main ( void ) { sq_c table_1 [ 12 ]; int index = 0; populate (table_1); for (index = 0; index <12; index++) printf("\nInput: \t %d \t\t Square: \t %d \t\t Cube: \t %d\n", table_1[index].input, table_1[index].square, table_1[index].cube) ; return (0) ; } void populate (sq_c * ssqq_cc ) { int index = 0; for (index = 0; index <12; index++) { (ssqq_cc + index)->input = index ; ssqq_cc [index].square = index * index ; (ssqq_cc + index)->cube = index * index * index ; } return ; }

#include // 11.c… same a 7.c but using both array and pointer notation #include // to the extreme, part 2 typedef struct sq_cube { int input; int square; int cube ; } sq_c ; void populate ( sq_c * ); int main ( void ) { sq_c table_1 [ 12 ]; int index = 0; populate (table_1); for (index = 0; index <12; index++) printf("\nInput: \t %d \t\t Square: \t %d \t\t Cube: \t %d\n", table_1[index].input, table_1[index].square, (table_1 + index)->cube ) ; return (0) ; } void populate (sq_c ssqq_cc [ ] ) { int index = 0; for (index = 0; index <12; index++) { (ssqq_cc + index)->input = index ; ssqq_cc [index].square = index * index ; (ssqq_cc + index)->cube = index * index * index ; } return ; }