ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 7: Structures.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C Programming
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
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.
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.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)
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.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 5: Pointers.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 4: Arrays and Strings.
C Slides and captured lecture (video and sound) are available at:
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
Programming in C Pointer Basics.
1 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.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Functions, Pointers, Structures Keerthi Nelaturu.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Advanced Data types and Sorting
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
CSC Programming for Science Lecture 36: Structures.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 5.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
chap11 Chapter 11 Structure and Union Types.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
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,
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
C Programming Tutorial – Part I
Lecture 9 Structure 1. Concepts of structure Pointers of structures
הגדרת משתנים יום שלישי 27 נובמבר 2018
Variables and Their scope
Programming in C Pointer Basics.
EECE.2160 ECE Application Programming
Structures vol2.
Programming in C Pointer Basics.
Structure and Union Types
Programming in C Pointer Basics.
Chapter 10 C Structures and Unions
Chapter 11 Structure and Union Types.
Structures, Unions, and Enumerations
Presentation transcript:

ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 7: Structures

ספטמבר 04Copyright Meir Kalech2 What is a Structure? How can we define something like an array, where each element contains all the information about student: name, age, telephone, address, etc. What is the type of this sort of array? We can define multiple data types, but for some students we have to define multiple parallel arrays - for each type. It turns out that what we need is a structure. A structure is a data aggregate, consisting of elements of different types (these elements are called members or fields).

ספטמבר 04Copyright Meir Kalech3 Structure Usage Structures are widely used. For instance: fname lname address salary priority total time status id name device sector Block open mode Person ProcessFile

ספטמבר 04Copyright Meir Kalech4 Structure Declaration As mentioned above, structure is a type defined by the programmer. In order to use a structure, we should declare it. Then we can use it as any other primitive type. Declaration: struct structure_name { members; }; For the declaration, no memory is allocated. Syntax comment: don ’ t forget the semicolon after the struct block (like for any declaration).

ספטמבר 04Copyright Meir Kalech5 Structure Declaration struct Student { char name[20]; int id; }; In this example we declared a new type named Student and provided information on its structure. Now we can define new variables of Student type, exactly as we define variables of primitive types: intx, y,*pInt = NULL; struct Student std1,std2,*pStudent = NULL; Each one of the variables (std1, std2) contains 2 fields.

ספטמבר 04Copyright Meir Kalech6 Structure Initialization struct Student { char name[20]; int id; }; Initialization can be achieved via definition: struct Student std = {“arie”, 222}; The initialization parameter list corresponds to the structure ’ s (member) fields.

ספטמבר 04Copyright Meir Kalech7 Structure Member Operator. We can ’ t refer to the structure as a whole. However, the structure member operator. is used to refer to each one of the structure ’ s fields. Reference a field via the variable std1 using operator. : std1.name std1.id Reference a field via the pointer pStudent: (*pStudent).name (*pStudent).id Question : Why are the parentheses necessary? Another, more aesthetic way for referencing a field via a pointer is using the (shorthand for (*pointer). ) operator -> (arrow): pStudent->name pStudent->id

ספטמבר 04Copyright Meir Kalech8 Example struct Student { char name[20]; int id; }; void main() { struct Student std1, std2, *pStd; printf(“enter name and id of std1 and std2\n”); scanf(“%s %d”, std1.name, &(std1.id)); scanf(“%s %d”, std2.name, &(std2.id)); pStd = &std1; printf(“%s %d”, pStd->name, pStd->id); } name: 100:____ id: 120:____ std1: 100 name: 250:____ id: 270:____ std2: pStd: 300

ספטמבר 04Copyright Meir Kalech9 Array of Structures Definition : struct struct_name array_name[num_of_items] Example: struct Student students[3]; students[2].id = 222; (*(students+2)).id = 333; (students+2)->id = 444; Initialization via definition: struct Student students[3] = {{“ami”, 222}, {“dana”, 333}, {“haia”, 444}}; If internal braces are missing, what will happen??? name: 100:____ id: 120:____ students: 100 name: 124:____ id: 144:____ name: 148:____ id: 168:____

ספטמבר 04Copyright Meir Kalech10 Pointer as Field A field may be a pointer also. We can refer to it by operator *(). or by ->, and then assign it to any address. For example: typedef struct { char *name; int id; } Student; void main() { Student std1, std2, *pStd; std2.name = “arie”; pStd = &std1; pStd->name = (char *) malloc(5); strcpy(pStd->name, ”arie”); } name: 100: 2700 id: 104:____ std1: 100 name: 250: 340 id: 254:____ std2: pStd: 300 \0eira 340 \0eira 2700 heapstack

ספטמבר 04Copyright Meir Kalech11 Structure as Parameter to Function A structure can be passed to a function by value or by address. Upon passing it by value, it is copied to the function. Any changes in the copy will not affect the original. Upon passing it by address, only its address is passed. Any changes on the pointed value will affect the original. For example: typedef struct { char name[20]; int id; } Student ; void main() { Student std1; input_student(&std1); output_student(std1); } void input_student(Student *std) { scanf(“%s”, std->name); scanf(“%d”, &(std->id)); } void output_student(Student std) { printf(“%s”, std.name); printf(“%d”, std.id); }

ספטמבר 04Copyright Meir Kalech12 Nested Structures A member of a structure may be itself a structure. For example, a student could be a member of a class. typedef struct { char name[20]; int id; } Student; typedef struct { Student students[3]; int num_students; char teacher[20]; } Class; void main() { Class school[2]; } 100:_ 120:_ school: :_ 144:_ 148:_ 168:_ 172:_ 176:_ name: id: num_students: teacher: 196:_ 216:_ 220:_ 240:_ 244:_ 264:_ 268:_ 272:_

ספטמבר 04Copyright Meir Kalech13 Nested Structures Access to the fields of Class can be done via the structure member operator. or via -> as well as the access to Student ’ s fields. void main() { Class school[2]; school[1].num_students = 2; } 100:_ 120:_ school: :_ 144:_ 148:_ 168:_ 172:_ 176:_ name: id: num_students: teacher: 196:_ 216:_ 220:_ 240:_ 244:_ 264:_ 268: 2 272:_

ספטמבר 04Copyright Meir Kalech14 Nested Structures Access to the fields of Class can be done via the structure member operator. or via -> as well as the access to Student ’ s fields. void main() { Class school[2]; school[1].num_students = 2; school[1].students[1].id = 4; } 100:_ 120:_ school: :_ 144:_ 148:_ 168:_ 172:_ 176:_ name: id: num_students: teacher: 196:_ 216:_ 220:_ 240: 4 244:_ 264:_ 268: 2 272:_ What is its type? Class

ספטמבר 04Copyright Meir Kalech15 Nested Structures Access to the fields of Class can be done via the structure member operator. or via -> as well as the access to Student ’ s fields. void main() { Class school[2]; school[1].num_students = 2; school[1].students[1].id = 4; } 100:_ 120:_ school: :_ 144:_ 148:_ 168:_ 172:_ 176:_ name: id: num_students: teacher: 196:_ 216:_ 220:_ 240: 4 244:_ 264:_ 268: 2 272:_ What is its type? Student

ספטמבר 04Copyright Meir Kalech16 Nested Structures Type recognition is very important for passing a structure as argument to a function. For instance: passing the classes to input and output functions. void main() { Class school[2]; int i; for(i=0; i<2; i++) input_class(&school[i]); //input_class(school+i); for(i=0; i<2; i++) output_class(school[i]); } void input_class(Class *cls) { int i; scanf(“%s”, cls->teacher); scanf(“%d”, &(cls->num_students)); for(i=0; i num_students; i++) input_student(&(cls->students[i])); } void output_class(Class cls) { int i; puts(cls.teacher); for(i=0; i<cls.num_students; i++) output_student(cls.students[i]); }

ספטמבר 04Copyright Meir Kalech17 Nested Structures Example of dynamic allocation of structures: typedef struct { char name[20]; int id; } Student; typedef struct { Student *students; int num_students; char teacher[20]; } Class; typedef struct { Class *classes; int num_classes; } Education; void main() { int i, j; Education edu; puts(“how many classes?”); scanf(“%d”, &edu.num_classes); edu.classes = (Class *) malloc(edu.num_classes*sizeof(Class)); for(i=0; i<edu.num_classes; i++) { puts(“enter num of students”); scanf(“%d”, &(edu.classes[i].num_students)); edu.classes[i].students = (Student *) malloc (edu.classes[i].num_students*sizeof(Student)); for(j=0; j<edu.classes[i].num_students; j++) input_student(&(edu.classes[i].students[j])); //input_student(((edu.classes)+i)->students+j); }