Introduction to C Programming CE00312-1 Lecture 10 Data Structures typedef and struct.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Programming in C Chapter 10 Structures and Unions
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
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.
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Structures Spring 2013Programming and Data Structure1.
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.
Computer Science II Exam I Review Monday, February 6, 2006.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
LECTURE 4: INFORMATION REPRESENTATION (PART II) COMP26120: ALGORITHMS AND IMPERATIVE PROGRAMMING.
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.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CCSA 221 Programming in C CHAPTER 9 WORKING WITH STRUCTURES 1 ALHANOUF ALAMR.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Advanced Data types and Sorting
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Learners Support Publications Classes and Objects.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
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.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
 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.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
+ 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.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
CPS120: Introduction to Computer Science Lecture 15A Structures.
CCSA 221 Programming in C CHAPTER 9 WORKING WITH STRUCTURES 1.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
CPS120: Introduction to Computer Science Data Structures.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
1 CS161 Introduction to Computer Science Topic #15.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
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.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Programming Fundamentals Enumerations and Functions.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
Chapter Structured Data 11. Combining Data into Structures 11.2.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
Chapter 10-1: Structure.
Java Primer 1: Types, Classes and Operators
Structures Lesson xx In this module, we’ll introduce you to structures.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
Derived types.
Chapter 11: Structured Data.
Classes and Objects.
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

Introduction to C Programming CE Lecture 10 Data Structures typedef and struct

Struct Group several pieces of related information together and is a collection of variables, referred to as fields, under a single name. Fields can be of differing types and must be uniquely named within the structure As the storage needed cannot be known in advance by the compiler, a definition is first required. A structure can be defined as a new named type thus extending the number of available types. It can use other structures, arrays or pointers as some of its members, though this can get complicated unless you are careful. Uses the keyword struct

Defining a struct A structure type is usually defined near to the start of a file using a typedef statement that defines and names a new type allowing its use throughout the program. Typedef statements usually occur just after the #define and #include statements in a file. Here is an example structure definition. typedef struct { char name[64], course[128]; int age, year; }student; This defines a new type student. Variables of type student can then be declared as follows. student st_record;

Variations in declaring structures struct date { int month, day, year; } todaysdate, purchase_date; struct date { int month, day, year; } todaysdate = { 25,9,1985 }; struct date { int month, day, year ; } dates[100];

Declaring structures in this way, however, prevents later use of the structure definition in the program since the structure definition is bound to the variable name that follows the right brace of the structure definition.

Structures containing structures Structures can also contain structures. struct date { int month, day, year; }; struct time { int hours, mins, secs; }; struct date_time { struct date sdate; struct time stime; };

typedef A way of identifying a programmer-defined data type Avoids proceeding all struct declarations with the word struct Allows struct to be passed as a parameter into a function Variables can be created of a type once it has been defined using typedef

Initialising structures Consider, struct date { int month, day, year; }; Values are listed inside a pair of braces with each separated by a comma corresponding to the fields in the struct. struct date today = { 4,23,1998 };

Assigning values to structure fields Each field can be separately assigned. Assignments conform to the standard for simple variables of that type st_record.age = 19; st_record.year = 2000; st_record.year will behave just like a normal integer. However, it is referred to by st_record.name Here the dot is an operator that selects a field from a structure.

Structures as Function Arguments A structure can be passed as a function argument just like any other variable. Where we wish to modify the values of fields of the structure we must pass a pointer to that structure. This is just like passing a pointer to an int type argument whose value we wish to change. If we are only interested in one field of a structure it is probably simpler to just pass that field. This will make for a simpler function which is easier to re-use. Of course, if we wish to change the value of that field we should pass a pointer to it. When a structure is passed as an argument each member of the structure is copied. Passing and working with pointers to large structures may be more efficient in such cases.

You may:Example Initialise structure fieldsstruct student { char group; int mark; } tom = {‘A’, 65}, dick = {‘M’, 60}; access membersn = tom.mark; copy an entire structuretom = dick; take a structure addressp = &tom; printf(“%d”,(*p).mark; use structures as parameter n = function(dick); You may NOT compare structures logically e.g. if (tom = = dick) is not allowed.

Example #include typedef struct { char ID; float distance, radius; } planet; planet mars; int main() { mars.name = ‘M’); mars.distance = 1.5; mars.radius = 0.4; printf("Planetary statistics:\n"); printf("Name: %c\n",mars.name); printf("Distance from Sun in AU: %4.2f\n",mars.distance); printf("Radius in Earth radii: %4.2f\n",mars.radius); return 0; }

Data Collections Involving Multiple Types Two main methods Arrays in parallel containing differing data types Arrays where each element contains a collection of data types

Parallel Arrays Corresponding position in each array refers to a different piece of data which is an item of data belonging to the same logical entity e.g. Name (string) Age (integer) Grade (character) Number of Attendances (int)

Data Collections Involving Multiple Types Requires of an array of structures capable of holding complex data. Referred to in C as a struct. In the example below struct student may be defined Each element of the array contains a collection of data of the same format. Name Age Grade Attendances

Array of Structs name age grade attendances Name Age Grade Attendances Name Age Grade Attendances Name Age Grade Attendances An array of student structs students[2] students[0] The definition of each element is referred to as a struct student

Arrays of Structures An array called birthdays of the same data type as the structure date can be created thus: struct student students[5]; This creates an array of 5 elements each of type date. Assignments and operations can be carried out thus: students[1].age = 20; --students[1].attendances;

Arrays of Structs Variables are declared as being instances of the user-defined type in the same way that variables of pre-defined types are declared (using a slightly different notation however!!) An array can be composed of Structs to give an indexed collection of objects that consist of mixed data studentarray[1] = student (copy a struct into a position in an array) studentarray[1].age = 21(update part of an element of an array of structs

Arrays of Structures An array called birthdays of the same data type as the structure date can be created thus: struct date birthdays[5]; This creates an array of 5 elements - each of type date. Assignments and operations can be carried out thus: e.g birthdays[1].month = 12; --birthdays[1].year;