Structures Spring 2013Programming and Data Structure1.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
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.
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.
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.
Spring Semester 2013 Lecture 5
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 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
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.
Programming and Data Structure
Structure.
Structures in C.
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.
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
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.
Topic 2 Pointers CSE1303 Part A, Summer Semester,2002 Data Structures and Algorithms.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Functions, Pointers, Structures Keerthi Nelaturu.
Understanding Structures tMyn1 Understanding Structures In order to describe virtually anything in the real world, you need to define several values that.
Advanced Data types and Sorting
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Structures Gabriel Hugh Elkaim Spring 2013.
Learners Support Publications Classes and Objects.
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Array, Structure and Union
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
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.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function 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.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
User Defined Data Types - Structures in C CHAPTER 4.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
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 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.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
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.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
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)
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Programming and Data Structures
CS1010 Programming Methodology
Chapter 10-1: Structure.
C Programming Tutorial – Part I
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures.
Derived types.
Sudeshna Sarkar 7th March 2017
Classes and Objects.
Structures In C Programming By Rajanikanth B.
Object Oriented Programming (OOP) Lecture No. 11
CSCE 206 Lab Structured Programming in C
Structures, Unions, and Enumerations
Presentation transcript:

Structures Spring 2013Programming and Data Structure1

What is a Structure? It is a convenient tool for handling a group of logically related data items. – Student name, roll number, and marks – Real part and complex part of a complex number This is our first look at a non-trivial data structure. – Helps in organizing complex data in a more meaningful way. The individual structure elements are called members. Spring 2013Programming and Data Structure2

Defining a Structure The composition of a structure may be defined as: struct tag { member 1; member 2; : member m; }; – struct is the required keyword. – tag is the name of the structure. – member 1, member 2, … are individual member declarations. Spring 2013Programming and Data Structure3

Contd. The individual members can be ordinary variables, pointers, arrays, or other structures. – The member names within a particular structure must be distinct from one another. – A member name can be the same as the name of a variable defined outside of the structure. Once a structure has been defined, individual structure-type variables can be declared as: struct tag variable_1, variable_2, …, variable_n; Spring 2013Programming and Data Structure4

Example A structure definition: struct student { char name[30]; int roll_number; int total_marks; char dob[10]; }; Defining structure variables: struct student a1, a2, a3; Spring 2013Programming and Data Structure5 A new data-type }

A Compact Form It is possible to combine the declaration of the structure with that of the structure variables: struct tag { member 1; member 2; : member m; } variable_1, variable_2,…, variable_n; In this form, “tag” is optional. Spring 2013Programming and Data Structure6

Example struct student { char name[30]; int roll_number; int total_marks; char dob[10]; } a1, a2, a3; struct { char name[30]; int roll_number; int total_marks; char dob[10]; } a1, a2, a3; Spring 2013Programming and Data Structure7 Equivalent declarations

Processing a Structure The members of a structure are processed individually, as separate entities. A structure member can be accessed by writing variable.member where variable refers to the name of a structure- type variable, and member refers to the name of a member within the structure. Examples: – a1.name, a2.name, a1.roll_number, a3.dob; Spring 2013Programming and Data Structure8

Example: Complex number addition Spring 2013Programming and Data Structure9 #include main() { struct complex { float real; float complex; } a, b, c; scanf (“%f %f”, &a.real, &a.complex); scanf (“%f %f”, &b.real, &b.complex); c.real = a.real + b.real; c.complex = a.complex + b.complex; printf (“\n %f + %f j”, c.real, c.complex); } Structure definition And Variable Declaration Accessing members Reading a member variable Scope restricted within main()

Comparison of Structure Variables Unlike arrays, group operations can be performed with structure variables. – A structure variable can be directly assigned to another structure variable of the same type. a1 = a2; All the individual members get assigned. – Two structure variables can be compared for equality or inequality. if (a1 = = a2) ……. Compare all members and return 1 if they are equal; 0 otherwise. Spring 2013Programming and Data Structure10

Arrays of Structures Once a structure has been defined, we can declare an array of structures. struct student class[50]; – The individual members can be accessed as: class[i].name class[5].roll_number Spring 2013Programming and Data Structure11

Arrays within Structures A structure member can be an array: The array element within the structure can be accessed as: a1.marks[2] Spring 2013Programming and Data Structure12 struct student { char name[30]; int roll_number; int marks[5]; char dob[10]; } a1, a2, a3;

Defining data type: using typedef One may define a structure data-type with a single name. General syntax: typedef struct { member-variable1; member-variable2;. member-variableN; } tag; tag is the name of the new data-type. Spring 2013Programming and Data Structure13

typedef : An example typedef struct{ float real; float imag; } _COMPLEX; _COMPLEX a,b,c; Spring 2013Programming and Data Structure14

Structure Initialization Structure variables may be initialized following similar rules of an array. The values are provided within the second braces separated by commas. An example: _COMPLEX a={1.0,2.0}, b={-3.0,4.0}; Spring 2013Programming and Data Structure15 a.real=1.0; a.imag=2.0; b.real=-3.0; b.imag=4.0;

Parameter Passing in a Function Structure variables could be passed as parameters like any other variable. Only the values will be copied during function invokation. Spring 2013Programming and Data Structure16 void swap(_COMPLEX a, _COMPLEX b) { _COMPLEX tmp; tmp=a; a=b; b=tmp; }

An example program Spring 2013Programming and Data Structure17 #include typedef struct{ float real; float imag; } _COMPLEX; void swap(_COMPLEX a, _COMPLEX b) { _COMPLEX tmp; tmp=a; a=b; b=tmp; }

Example program: contd. Spring 2013Programming and Data Structure18 void print(_COMPLEX a) { printf("(%f, %f) \n",a.real,a.imag); } main() { _COMPLEX x={4.0,5.0},y={10.0,15.0}; print(x); print(y); swap(x,y); print(x); print(y); }

Returning structures It is also possible to return structure values from a function. The return data type of the function should be as same as the data type of the structure itself. Spring 2013Programming and Data Structure19 COMPLEX add (COMPLEX a, COMPLEX b) { COMPLEX tmp; tmp.real = a.real+b.real; tmp.imag = a.imag+b.imag; return(tmp); } Direct arithmetic operations are not possible with Structure variables.