CSCE 206 Lab Structured Programming in C

Slides:



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

SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
1 Lab 4 Westfield High School APCS LAB 4 Parameters, apvectors, structs.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
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.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
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.
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.
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.
 Review structures  Program to demonstrate a structure containing a pointer.
Enumerated Data Type. An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
Object Oriented Programming (OOP) Lecture No. 11.
Today’s Material Aggregate Data Types: Structures and Unions
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
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.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
CPS120: Introduction to Computer Science Lecture 15A Structures.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
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.
Structures, Unions, Enumerations
Chapter 10-1: Structure.
TMF1414 Introduction to Programming
CS1010 Discussion Group 11 Week 12 – Structured Structures.
Student Book An Introduction
Visit for more Learning Resources
EECE.2160 ECE Application Programming
DATA HANDLING.
CS 1430: Programming in C++.
Buy book Online -
CSC 253 Lecture 8.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
LINKED LISTS.
C Structures, Unions, and Enumerations
Data Types – Structures
CSC 253 Lecture 8.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Structure ការណែនាំអំពី Structure
CSCE 206 Lab Structured Programming in C
Return by Reference CSCE 121 J. Michael Moore.
Pointers Call-by-Reference CSCI 230
Local Variables, Global Variables and Variable Scope
Introduction to Programming
EECE.2160 ECE Application Programming
INC 161 , CPE 100 Computer Programming
EECE.2160 ECE Application Programming
Programming Language C Language.
C Programming Lecture-8 Pointers and Memory Management
C Programming Lecture-13 Structures
Structures.
EECE.2160 ECE Application Programming
Engineering H192 Winter 2005 Structs and Enumeration Prof. Almas Ansari Dept of Computer Science Engineering Anjuman College of Engineering & Technology..
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
Object Oriented Programming (OOP) Lecture No. 11
Structure (i.e. struct) An structure creates a user defined data type
CSCE 206 Lab Structured Programming in C
CSCE 206 Lab Structured Programming in C
CSCE 206 Lab Structured Programming in C
EECE.2160 ECE Application Programming
CSE 206 Course Review.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Programming Fundamental
Week 9 - Monday CS222.
Presentation transcript:

CSCE 206 Lab Structured Programming in C Spring 2019 Lecture 8

Structs A struct is a user-defined data type Used to group one or more variables into the same type struct student { char name[30]; int uin; int phone_num; int gpa; }; struct keyword structure tag Semicolon, do not forget to put it!

Structs: create a type typedef struct { char name[30]; int uin; int phone_num; float gpa; } Student; Use uppercase in types! (not for variables)

Use a type struct void main() { Student s1, s2; // declaration of two students with a Student type s1.gpa = 3.8; // set gpa value for the first student s2.gpa = 3.3; // set gpa for the second }

Use a type struct in functions Student highestGPA(Student s1, Student s2) { if (s1.gpa > s2.gpa) return s1; else return s2; } void main() { Student s1, s2; // declaration of two students with a Student type s1.gpa = 3.8; // set gpa value for the first student s2.gpa = 3.3; // set gpa for the second Student best = highestGPA(s1, s2);

Use a type struct inside the struct typedef struct Node { char name[30]; int uin; int phone_num; float gpa; struct Node *next; // you can only use the same type if declared in first line } Node;

Use a type struct with a pointer void main() { Student s1; Student *pS1 = &s1; //pointer to student pS1->gpa = 3.8; // when using a pointer use -> instead of ‘.’ }

Arrays of structs void main() { Student class[10]; class[0].gpa = 3.8; // modify GPA of first student of the class }

Structs that contain structs typedef struct { float test1; float test2; } Grades; int uin; Grades grades; } Student; void main() { Student class[10]; class[0].grades.test1 = 83.2; // modify first grade of first student of the class }

Accessing structs Student s; Student *ptr = &s; ptr->grades.test1 = 84.1; // through a pointer s.grades.test2 = 96.2; // through the student (*ptr).uin = 123456789; // beware of the parenthesis!