CS 108 Computing Fundamentals Tuesday, November 14, 2017.

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.
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.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
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.
'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.
CS 108 Computing Fundamentals March 31, Grades not as good as I hoped I drop the lowest of the first 3 exams Exam #3 and #4 will cover much of the.
Agenda Object Oriented Programming Reading: Chapter 14.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
CS 108 Computing Fundamentals April 7, GHP # 10 and GHP 11 GHP 10 will be posted later today ­ Due: Saturday, April 11, at midnight GHP 11 will.
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.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
Arrays.
1 CS161 Introduction to Computer Science Topic #15.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
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 April 28, Defining Variables As we’ve discussed many times, defining a variable does all of the following –Gives us.
Lecture 1 Data Structures Aamir Zia. Introduction Course outline Rules and regulations Course contents Good Programming Practices Data Types and Data.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
C Structures and Memory Allocation
LINKED LISTS.
Lesson #8 Structures Linked Lists Command Line Arguments.
‘C’ Programming Structures and Commands
User-Written Functions
CS1010 Programming Methodology
Chapter 10-1: Structure.
C Programming Tutorial – Part I
Quiz 11/15/16 – C functions, arrays and strings
CS 108 Computing Fundamentals November 2, 2017.
CSE 374 Programming Concepts & Tools
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Module 2 Arrays and strings – example programs.
Prepared By: G.UshaRani B.Pranalini A.S.Lalitha
Structured Data Types A structure can be used to combine data of different types into a single (compound) data value. Like all data types, structures must.
Introduction to Data Structures
Computer Science 210 Computer Organization
Lecture 9 Structure 1. Concepts of structure Pointers of structures
C Structures, Unions, Bit Manipulations and Enumerations
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Built-In (a.k.a. Native) Types in C++
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Dr. Bhargavi Dept of CS CHRIST
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
CPS120: Introduction to Computer Science
From C to C++: Summary of weeks 1 - 4
By C. Shing ITEC Dept Radford University
By Yogesh Neopaney Assistant Professor Department of Computer Science
Lec17 Structs.
Chapter 9: Pointers and String
Exam #1 February 23rd (Next Friday)
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
C Structures and Memory Allocation
Chapter 9: Records (structs)
Structures, Unions, and Enumerations
Structures Declarations CSCI 230
Week 9 - Monday CS222.
Presentation transcript:

CS 108 Computing Fundamentals Tuesday, November 14, 2017

GHP #19 Let's look at a student example

Review Spring 17 Exam #3

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 advanced 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, eye color, recipient of financial aide, 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_tag { data_type name_of_element_1 ; data_type name_of_element_2 ; . . . data_type name_of_element_n ; } ;

Structure Definitions Example of creating 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

Structure Instances Syntax for creating an instance of a defined structure: struct structure_tag instance_name ; Example of creating an instance of a defined structure: struct student student_1 ; student_1 is a variable 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 ; strcpy(student_1.last_name, "Urban" ) ; strcpy(student_1.ssn, "111223333" ) ; student_1.gpa = 2.0 ;

Structures We can initialize the instance of a structure while we are creating it: struct student student_1 = {"Chris", "Urban", "111223333", "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, "111223333", "Computer Science", "Chris", "Urban" } ;

#include <stdio.h> // 11.c #include<string.h> 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, "111223333" ); // 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 a structure name for structure definitions A structure name is essentially a new data type and can be used to assign a specific structure to a variable (create an instance of a structure) Using the structure name (which is essentially a new data type) 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 name (new data type) 2) create an instance of a typedef defined structure using struct and a structure name Syntax of a structure definition using typedef and a structure name: typedef struct structure_tag { data_type name_of_element_1; data_type name_of_element_2; . . . data_type name_of_element_n ; } structure_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 that 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_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 which means that student_1 has all the characteristics of the student structure Using typedef makes passing structures to functions a little easier (more on this in a few slides)

#include <stdio.h> //12.c #include<string.h> 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, "111223333"); // 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 <stdio. h> //12a #include <stdio.h> //12a.c… the structure name and tag are the same #include<string.h> 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 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, "111223333"); // 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 <stdio. h> //12b #include <stdio.h> //12b.c… the structure name and tag are the same #include<string.h> 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 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, "111223333"); // 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 <stdio.h> //13.c #include<string.h> 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 Structure Instances 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_tag 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_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 <stdio.h> //14.c #include<string.h> 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 <stdio.h> //15.c #include<string.h> 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 <stdio.h> //16.c #include<string.h> 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 ;