CS561 Manju Muralidharan Priya Structures in C CS200 STRUCTURES. Manju Muralidharan Priya

Similar documents
3.3 Structures. Department of CSE

Chapter-14 STRUCTURES

IV Unit Second Part STRUCTURES

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

Subject: Fundamental of Computer Programming 2068

CS100 : Computer Programming

ALQUDS University Department of Computer Engineering

Problem Solving and 'C' Programming

Tin học cơ sở 4$ Structures!

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Structures Unions and Enumerated Datatypes 224

Structures. 21 July 2009 Programming and Data Structure 1

Chapter 10 C Structures, Unions, Bit Manipulations

The syntax of structure declaration is. struct structure_name { type element 1; type element 2; type element n;

INTRODUCTION 1 AND REVIEW

M.EC201 Programming language

ENEE150 Final Exam Review

CSI33 Data Structures

Technical Questions. Q 1) What are the key features in C programming language?

Structure, Union. Ashishprajapati29.wordpress.com. 1 What is structure? How to declare a Structure? Explain with Example

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Short Notes of CS201

Full file at C How to Program, 6/e Multiple Choice Test Bank

CS201 - Introduction to Programming Glossary By

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Bit Manipulation in C

Example: Structure, Union. Syntax. of Structure: struct book { char title[100]; char author[50] ]; float price; }; void main( )

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM

Guide for The C Programming Language Chapter 4

ONE DIMENSIONAL ARRAYS

C Language, Token, Keywords, Constant, variable

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

CS240: Programming in C

Unit 8. Structures and Unions. School of Science and Technology INTRODUCTION

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

MCA Semester 1. MC0061 Computer Programming C Language 4 Credits Assignment: Set 1 (40 Marks)

Fundamentals of Programming Session 24

CS Programming In C

To understand the concept of candidate and primary keys and their application in table creation.

A First Book of ANSI C Fourth Edition. Chapter 12 Structures

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

C++ Notes Class XI Structure

Microcontroller Systems. ELET 3232 Topic 8: Structures, Arrays, & Pointers

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

C Programming Review CSC 4320/6320

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

MYcsvtu Notes LECTURE 34. POINTERS

9/9/2017. If ( condition ) { statements ; ;

Full file at

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Basic Elements of C. Staff Incharge: S.Sasirekha

Module 6: Array in C

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Introduction to C++ with content from

C PROGRAMMING Lecture 4. 1st semester

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

struct structure_name { //Statements };

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Structures. 5 th March Spring 2012 Programming and Data Structure 1

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Chapter-8 DATA TYPES. Introduction. Variable:

Structures in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Unit IV & V Previous Papers 1 mark Answers

Lecture 3 Tao Wang 1

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

CSE 230 Intermediate Programming in C and C++

Computer Language. It is a systematical code for communication between System and user. This is in two categories.

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 11: Records (structs)

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

UNIT - V STRUCTURES AND UNIONS

First of all, it is a variable, just like other variables you studied

Chapter 14. Structures

Syntax and Variables

Introduction to Programming (Java) 4/12

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

POINTERS, STRUCTURES AND INTRODUCTION TO DATA STRUCTURES

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Data Types and Variables in C language

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

C++ Structures Programming Workshop 2 (CSCI 1061U)

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

CS240: Programming in C

BASIC ELEMENTS OF A COMPUTER PROGRAM

UNIT-I Fundamental Notations

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Unit 7. Functions. Need of User Defined Functions

C: How to Program. Week /Mar/05

BLM2031 Structured Programming. Zeyneb KURT

You must declare all variables before they can be used. Following is the basic form of a variable declaration:

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

Transcription:

OBJECTIVES: CS200 STRUCTURES Manju Muralidharan Priya By the end of this class you will have understood: 1. Definition of a structure 2. Nested Structures 3. Arrays of structure 4. User defined data types 5. Pointers to structures 6. Using structures in functions INTRODUCTION A structure is a collection of one or more variables, possibly of different data types, grouped together under a single name for convenient handling. Structures help to organize data, particularly in large programs, because they allow a group of related variables to be treated as a single unit. E.g. Details of an employee in an organization (i.e. name, address, designation, salary etc.) Arrays cannot be used in this context as they are all different data types (i.e. char, float etc.). The simplest solution to this problem is structures. STRUCTURE DECLARATION Let us create a structure for the above example. char *address; char *designation; float salary; } emp1, emp2; 1

Breakdown 1. The keyword struct is used to declare a structure which is a list of declarations enclosed in braces. 2. Employee is called the structure tag, which is a name given to the structure. 3. The variables that have been declared within the structure definition are called its member variables. 4. The structure declaration as a whole is considered as a data type and emp1 and emp2 are considered as the variables of the structure and a memory space is allocated for them. The amount of memory allocated is the total value of individual members. 5. A structure for which no variable is declared only defines a template and no memory space is set aside for it. 6. Variables for a structure can also be defined later within the program as follows: struct employee emp3; 7. A member of the structure is always referred to and accessed with its structure name. emp3.salary The. Operator is used to connect the name of the structure variable and the member name. printf ( The salary of emp3 is %d, emp3.name); NESTED STRUCTURES Nested structures are structures within structures. Taking the same example as above let us break down the address into a more complex structure, struct employee_address { }; int no; char *street; char *area; int pincode; 2

Including this in place of address in the previous structure struct employee_address address; char *designation; float salary; } emp1, emp2; The above two definitions can also be consolidated by including the definition of address within the definition of employee as follows: strut employee_address { int no; } address; char *street; char *area; int pincode; char *designation; float salary; } emp1, emp2; ARRAYS OF STRUCTURE Let us consider the same example and assume that there are five employees in the organization whose records have to be maintained. The declaration of the structure and the structure variable will be as follows: char *emp_no; 3

} emp[5]; int *salary; The above declaration creates memory space for an array of five employee type. The value to the structure variable can be added as follows: struct employee{ char *emp_no; int *salary; } emp[5] = { { emp1, e001,1000}, { emp2, e002,2000}, { emp3, e003,3000}, { emp4, e004,4000}, { emp5, e005,5000} }; Blank spaces can be left in the assignment where the employee details are unknown. i.e. {, e007,} Employee name and salary are unknown. Any member of the structure can be accessed using the index number along with the structure name. For e.g. printf( The name of the fourth employee is %s, emp[3].name}; USER DEFINED DATA TYPES C allows programmer to define their own datatypes equivalent to the existing system datatypes. For this purpose we use typedef. Let us declare a new datatype date of birth using structures: typedef struct { int dd; 4

int mm; int yyyy;} dob; Given this declaration dob can be used in the structure just like another datatype like int or char or so on. dob emp_dob; POINTER TO STRUCTURES Pointers to structures are just like pointers to any other variable. The declaration is: struct employee{ } *emp; Here emp point to the structure employee and *emp is the pointer. To access the member variables using a structure pointer we use -> (hyphen followed by greater than symbol). emp->name refers to employee name. Any manipulation can be done by through these pointers. STRUCTURES AND FUNCTIONS Structures may be passed to functions either by value or by reference. However, they are usually passed by reference (the address of the structure is passed). Consider the following e.g. struct employee{ }emp1; float basic; float hra; Let us assume that the value of the gross salary is the addition of basic and HRA. Let us write a program which has a function which does this addition operation by using the structure variable and display the gross salary of the employee. 5

#include <stdio.h> }emp1; main() { } char name[20]; float basic; float hra; float gross, grosscalc(); printf( enter employee name ); scanf(%s,emp1.name); printf( enter basic salary ); scanf(%f,emp1.basic); printf( enter hra ); scanf(%f,emp1.hra); gross = grosscalc(&emp1); /* passing address of structure */ printf( the gross salary is : %f, gross); float grosscalc(struct employee *emp) { } return(emp->basic+emp->hra); /* adds basic and hra and returns value to main */ 6