Used for handling a group of logically related data items

Similar documents
arrays C Programming Language - Arrays

C++ DATA STRUCTURES. Defining a Structure: Accessing Structure Members:

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Stacks. Linear data structures

Sources: On the Web: Slides will be available on:

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

The C Programming Language course syllabus associate level

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

C++FA 5.1 PRACTICE MID-TERM EXAM

5 Arrays and Pointers

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Informatica e Sistemi in Tempo Reale

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Data Structures using OOP C++ Lecture 1

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Object Oriented Software Design II

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

C++ Programming Language

Crash Dive into Python

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Tutorial on C Language Programming

Example of a Java program

Implementation Aspects of OO-Languages

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Semantic Analysis: Types and Type Checking

Lecture 22: C Programming 4 Embedded Systems

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Parameter Passing. Standard mechanisms. Call by value-result Call by name, result

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

In this Chapter you ll learn:

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

An Overview of Java. overview-1

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

MISRA-C:2012 Standards Model Summary for C / C++

13 Classes & Objects with Constructors/Destructors

B.Sc.(Computer Science) and. B.Sc.(IT) Effective From July 2011

Computer Programming Tutorial

The Function Pointer

Software Development (CS2500)

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

Parameter Passing in Pascal

C++ INTERVIEW QUESTIONS

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

C PROGRAMMING FOR MATHEMATICAL COMPUTING

System Calls and Standard I/O

VB.NET Programming Fundamentals

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

1 Abstract Data Types Information Hiding

CS 241 Data Organization Coding Standards

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Illustration 1: Diagram of program function and data flow

Module 816. File Management in C. M. Campbell 1993 Deakin University

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

File Handling. What is a file?

Fundamentals of Programming

FEEG Applied Programming 5 - Tutorial Session

A C Test: The 0x10 Best Questions for Would-be Embedded Programmers

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Lecture 11 Array of Linked Lists

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Project 1: Implement a simple hosts framework using UNIX TCP Sockets to demonstrate the concept of Mobile Agents

6.S096 Lecture 1 Introduction to C

Objective-C Tutorial

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

2.3 WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Moving from C++ to VBA

InterBase 6. Embedded SQL Guide. Borland/INPRISE. 100 Enterprise Way, Scotts Valley, CA

Beyond the Mouse A Short Course on Programming

Project 2: Bejeweled

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Introduction to Computer Graphics with WebGL

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Comp151. Definitions & Declarations

B AB 5 C AC 3 D ABGED 9 E ABGE 7 F ABGEF 8 G ABG 6 A BEDA 3 C BC 1 D BCD 2 E BE 1 F BEF 2 G BG 1

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

C Coding Style Guide. Technotes, HowTo Series. 1 About the C# Coding Style Guide. 2 File Organization. Version 0.3. Contents

Embedded C Programming

Programming Language Features (cont.) CMSC 330: Organization of Programming Languages. Parameter Passing in OCaml. Call-by-Value

Introduction to Data Structures

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

CP Lab 2: Writing programs for simple arithmetic problems

DNA Data and Program Representation. Alexandre David

Chapter 2 Introduction to Java programming

C Interview Questions

AP Computer Science Java Subset

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Ada-95 dla programistów C/C++

C++ Language Tutorial

Lecture 12 Doubly Linked Lists (with Recursion)

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

Transcription:

Structures 1

What is a Structure? Used for handling a group of logically related data items Examples: Student name, roll number, and marks Real part and complex part of a complex number Helps in organizing complex data in a more meaningful way The individual structure elements are called members 2

Defining a Structure struct tag { member 1; member 2; : member m; }; struct is the required C keyword tag is the name of the structure member 1, member 2, are individual member declarations 3

Contd. The individual members can be ordinary variables, pointers, arrays, or other structures (any data type) 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, the individual structure-type variables can be declared as: struct tag var_1, var_2,, var_n; 4

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; A new data-type 5

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; } var_1, var_2,, var_n; Declares three variables of type struct tag In this form, tag is optional 6

Accessing a Structure The members of a structure are processed individually, as separate entities Each member is a separate variable 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 7

Example: Complex number addition void main() { struct complex { float real; float cmplex; } a, b, c; scanf ( %f %f, &a.real, &a.cmplex); scanf ( %f %f, &b.real, &b.cmplex); } c.real = a.real + b.real; c.cmplex = a.cmplex + b.cmplex; printf ( \n %f + %f j, c.real, c.cmplex); 8

Operations on Structure Variables Unlike arrays, 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 not be compared for equality or inequality if (a1 == a2) this cannot be done 9

Arrays of Structures Once a structure has been defined, we can declare an array of structures struct student class[50]; type name The individual members can be accessed as: class[i].name class[5].roll_number 10

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

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: struct complex a={1.0,2.0}, b={-3.0,4.0}; a.real=1.0; a.imag=2.0; b.real=-3.0; b.imag=4.0; 12

Parameter Passing in a Function Structure variables can be passed as parameters like any other variables. Only the values will be copied during function invocation void swap (struct complex a, struct complex b) { struct complex tmp; } tmp=a; a=b; b=tmp; 13

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 struct complex add(struct complex a, struct complex b) { struct 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 14

Defining data type: using typedef One may define a structure data-type with a single name typedef struct newtype { member-variable1; member-variable2;. member-variablen; } mytype; mytype is the name of the new data-type Also called an alias for struct newtype Writing the tag name newtype is optional, can be skipped Naming follows rules of variable naming 15

typedef : An example typedef struct { float real; float imag; } _COMPLEX; Defined a new data type named _COMPLEX. Now can declare and use variables of this type _COMPLEX a, b, c; 16

Note: typedef is not restricted to just structures, can define new types from any existing type Example: typedef int INTEGER Defines a new type named INTEGER from the known type int Can now define variables of type INTEGER which will have all properties of the int type INTEGER a, b, c; 17

The earlier program using typedef typedef struct{ float real; float imag; } _COMPLEX; void swap (_COMPLEX a, _COMPLEX b) { _COMPLEX tmp; } tmp = a; a = b; b = tmp; 18

Contd. void print (_COMPLEX a) { printf("(%f, %f) \n",a.real,a.imag); } void main() { _COMPLEX x={4.0,5.0}, y={10.0,15.0}; print(x); print(y); swap(x,y); print(x); print(y); } swap.c 19

Output: (4.000000, 5.000000) (10.000000, 15.000000) (4.000000, 5.000000) (10.000000, 15.000000) x and y are not swapped! But that has got nothing to do with structures specially. We will see its reason shortly 20

Structures and Functions A structure can be passed as argument to a function A function can also return a structure 21

Example: complex number addition void main() { _COMPLEX a, b, c; scanf( %f %f, &a.real, &a.imag); scanf( %f %f, &b.real, &b.imag); c = add (a, b) ; printf( \n %f %f, c,real, c.imag); } _COMPLEX add(_complex x, _COMPLEX y) { _COMPLEX t; } t.real = x.real + y.real; t.imag = x.imag + y.imag ; return (t) ; 22

Exercise Problems 1. Extend the complex number program to include functions for addition, subtraction, multiplication, and division 2. Define a structure for representing a point in twodimensional Cartesian co-ordinate system Write a function to compute the distance between two given points Write a function to compute the middle point of the line segment joining two given points Write a function to compute the area of a triangle, given the co-ordinates of its three vertices 23