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.

Slides:



Advertisements
Similar presentations
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
Advertisements

StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
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.
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.
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,
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
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.
1 Types The type of a variable represents a set of values that may be assigned to the variable. For example, an integer variable is one that may take the.
Introduction to C Programming CE Lecture 10 Data Structures typedef and struct.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Enumerated Data Type. An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
Chapter 9 Structured Data: Structs and ADTs (Data Base Programs with C++) Mr. Dave Clausen La Cañada High School.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 11: Structure and Union 1.
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Array, Structure and Union
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Structures Combining data types into a logical groupings.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Struct 1. Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Structures (L30) u Syntax of a struct Declaration u Structure Variables u Accessing Members of Structures u Initialize Structure Variables u Array of Structures.
+ 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.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear.
CPS120: Introduction to Computer Science Lecture 15A Structures.
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
CPS120: Introduction to Computer Science Data Structures.
Computer Programming for Engineers
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
CO1401 Program Design and Implementation
CS1010 Programming Methodology
Chapter 10-1: Structure.
C Programming Tutorial – Part I
Visit for more Learning Resources
Structures.
DATA HANDLING.
Buy book Online -
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures Lesson xx In this module, we’ll introduce you to structures.
Using Arrays in C Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning.
Structure ការណែនាំអំពី Structure
Derived types.
CSCE 206 Lab Structured Programming in C
Structures, Unions, and Enumerations
Presentation transcript:

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 that they allow many variables of different types grouped together under the same name. For example you can create a structure called person which is made up of a string for the name and an integer for the age.

Person struct Here is how you would create that person structure in C: struct person { char name[30]; int age; };

Creating a variable of struct type The above is just a declaration of a type. You must still create a variable of that type to be able to use it. Here is how you create a variable called p of the type person:

Program #include struct person { char name[30]; int age; }; main() { struct person p; }

Accessing the elements of a structure To access the string or integer of the structure you must use a dot between the structure name and the variable name. #include struct person { char name[30]; int age; }; main() { struct person p; p.name = "John Smith"; p.age = 25; printf("%s",p.name); printf("%d",p.age); }

Struct format The format for defining a structure is struct Tag { Members }; Where Tag is the name of the entire type of structure and Members are the variables within the struct. e.g. struct example { int x; }; example is the Tag, and x is a member.

To actually create a single structure the syntax is struct Tag name_of_single_structure; struct example an_example; an_example is an instance of struct example

To access a variable of the structure it goes name_of_single_structure.name_of_variable; E.g. an_example.x = 33;

Putting it all together //declare the struct struct example { int x; }; Create variables of type struct example struct example an_example; //Treating it like a normal variable type// // Access its members an_example.x = 33;

Another example Here is an example program:

struct database { int id_number; int age; float salary; }; main() { database employee; //There is now an employee variable that has modifiable variables inside it. employee.age = 22; employee.id_number = 1; employee.salary = ; }

Employee Record ID_numberAgeSalary

Type definitions You can give your own name to a variable using a type definition. Here is an example of how to create a type definition called intptr for a pointer to an integer. #include typedef int *intptr; int main() { intptr ip; return 0; } Type definitions for a structure If you don't like to use the word struct when declaring a structure variable then you can create a type definition for the structure. The name of the type definition of a structure is usually all in uppercase letters. #include typedef struct person { char name[30]; int age; } PERSON; int main() { PERSON p; p.name = "John Smith"; p.age = 25; printf("%s",p.name); printf("%d",p.age); return 0; }

Exercise Create a person struct with fields Name, Address, Student no. Grade. Create 2 variables of type person Assign values to each of their fields. Print out who has the highest grade.

#include struct person {char name[40]; char address[80]; char student_no[10]; int grade; }

main() { person p1,p2; p1.name = “fred” p1.address = “kevin st” p1.student_no = “c102445” p1.grade = 72;

p2.name = “mary” p2.address = “bolton st” p2.student_no = “b10775” p2.grade = 87; if (p1.grade > p2.grade) {printf(“fred’s mark %d is greater than mary’s %d”, p1.grade,p2.grade);} else {printf(“mary’s mark %d is greater than fred’s %d”, p2.grade,p1.grade);} }

Exercise Create a struct which represents a book.

struct book struct book { chartitle; char author; printf("enter for book %d",i +1); gets(collection[i]. publisher; char isbn; int year; char genre; char illustrator; }

Can have an array of these structs e.g. catalog is an array of 100 books struct book catalog[100];

Exercise 2 Using book struct create a list of 5 books and read in their details

#include struct book { char title[40]; char author[40]; char publisher[40]; char isbn[40]; int year; char genre[40]; char illustrator[40]; }

main() { struct book collection[5]; int i; for(i = 0; i < 5,i++) { printf("enter title for book %d",i +1); gets(collection[i].title); printf("enter author for book %d",i +1); gets(collection[i].author); printf("enter publisher for book %d",i +1); gets(collection[i]. publisher); printf("enter year for book %d",i +1); scanf(“%d”, collection[i].year); printf("enter isbn for book %d",i +1); gets(collection[i]. isbn); printf("enter genre for book %d",i +1); gets(collection[i]. genre; printf("enter illustrator for book %d",i +1); gets(collection[i]. illustrator); }

Note We can also define pointers to structs struct book *book1;