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

Similar documents
IV Unit Second Part STRUCTURES

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

3.3 Structures. Department of CSE

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

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

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

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

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

Data Types and Variables in C language

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

Chapter-14 STRUCTURES

C Programming Review CSC 4320/6320

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

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

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Subject: Fundamental of Computer Programming 2068

Single Dimension Arrays

Module 6: Array in C

UNIT-IV. Structure is a user-defined data type in C language which allows us to combine data of different types together.

A Fast Review of C Essentials Part I

User Defined data Types

INTRODUCTION 1 AND REVIEW

( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING UNIT V 2 MARKS

BLM2031 Structured Programming. Zeyneb KURT

Unit IV & V Previous Papers 1 mark Answers

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

ALQUDS University Department of Computer Engineering

12 CREATING NEW TYPES

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

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

UNIT - V STRUCTURES AND UNIONS

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

C Syntax Out: 15 September, 1995

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


Chapter 10 C Structures, Unions, Bit Manipulations

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

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

Structures Unions and Enumerated Datatypes 224

Problem Solving and 'C' Programming

Structures. Dr. Donald Davendra Ph.D. (Department of Computing Science, Structures FEI VSB-TU Ostrava)

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

C Language, Token, Keywords, Constant, variable

Unit 3 Decision making, Looping and Arrays

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

Multi-Dimensional arrays

Advanced C Programming and Introduction to Data Structures

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Syntax and Variables

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

C: How to Program. Week /Mar/05

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

Basic Elements of C. Staff Incharge: S.Sasirekha

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Programming for Engineers Structures, Unions

DEPARTMENT OF MATHS, MJ COLLEGE

AMCAT Automata Coding Sample Questions And Answers

Guide for The C Programming Language Chapter 4

COMP 2355 Introduction to Systems Programming

Chapter 2 - Introduction to C Programming

Downloaded from

Variation of Pointers

ARRAYS(II Unit Part II)

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

C library = Header files + Reserved words + main method

Basics of Programming

Programming in C. What is C?... What is C?

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Chapter 21: Introduction to C Programming Language

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

Function Call Stack and Activation Records

Decision Making -Branching. Class Incharge: S. Sasirekha

Programming. Structures, enums and unions

typedef int Array[10]; String name; Array ages;

Arrays and Pointers (part 1)

CS Programming In C

Computer Systems Principles. C Pointers

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

Data Type Fall 2014 Jinkyu Jeong

CS349/SE382 A1 C Programming Tutorial

FUNCTION POINTERS. int (*pf)(); // pf is a pointer to a function returning an int.

Chapter-8 DATA TYPES. Introduction. Variable:

Chapter 14. Structures

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

Object Oriented Pragramming (22316)

// CSE/CE/IT 124 JULY 2015, 2.a. algorithm to inverse the digits of a given integer. Step 1: Start Step 2: Read: NUMBER Step 3: r=0,rev=0

Short Notes of CS201

Fundamental of Programming (C)

Arrays, Pointers and Memory Management

CCE RR REVISED & UN-REVISED KARNATAKA SECONDARY EDUCATION EXAMINATION BOARD, MALLESWARAM, BANGALORE G È.G È.G È..

Fundamentals of Computer Programming Using C

PROGRAMMAZIONE I A.A. 2018/2019

Transcription:

UNIT-V Structures Structure is a very useful derived data type supported in c that allows grouping one or more variables of different data types with a single name. The general syntax of structure is given below: Struct <tagname> datatype membername1; datatype membername2; ; Declaration and Initialization: Structure can be declared and initialized either above the main function or within the main function. The process of defining, declaring and initializing of structure is illustrated below: Above main function char name[20],course[5]; s1= ARYA, BTECH,123; Or char name[20],course[5]; s1; s1= ARYA, BTECH,123; In the above example student is the structure name and s1 is the structure variable declared and the values are initialized to the structure variable within flower braces Within main function char name[20],course[5]; s1= ARYA, BTECH,123; Or Main() char name[20],course[5]; s1; s1= ARYA, BTECH,123; In the above example student is the structure Name and s1 is the structure variable declared and the values are initialized to the structure an variable within flower braces Programming in C and Data Structures Unit V Page 1

Accessing members of the Structure: The member s of the structure can be accessed with a variable declared of that structure type and with dot operator(.) The general form is shown below: Structure variable name.member name For eg., char name[20],course[5]; s1; printf( enter the name,course and rollno: ); scanf( %s%s%d,s1.name,s1.course,&s1.rollno); printf( name=%s\n,s1.name); printf( course=%s\n,s1.course); printf( rollno=%d\n,s1.rollno); In the above example a structure by name student is declared with members name and course declared as character array and rollno declared of typed as int. The scanf statement shows how data is read for the members of the structure through structure variable likewise the printf statement shows how to print the data of the members of the structure. Nested Structures: Structure defined within another structure is said to be nested structures. Below are given examples of the different ways of making a structure nested. struct employee Struct date int empid; Int day; char designation[15]; Int month; struct salary Int year; int basic; ; Int da; Struct employee s1; emp; Int empno; Char name[20]; Float basic; Struct date joindate; emp1; Programming in C and Data Structures Unit V Page 2

In case of nested structures the accessing of data for members is done by accessing the outer structure variable followed by the dot operator and then the inner structure variable followed by the dot operator and then the member name. For example in the above structure to read data for the member basic. It is written as Scanf( %d,&emp1.s1.basic); Where emp1 is the outer structure variable and s1 is the inner structure variable and basic is the member name. For example in the above structure to read data for the member day. It is written as Scanf( %d,&emp1.joindate.day); Where emp1 is the outer structure variable and joindate is the inner structure variable and day is the member name. Structures and Functions: Structure variable can be passed as argument to function in two ways that is by value and by reference. Below we will see with example the procedure of passing structure variable as arguments by value and by reference. By Value struct employee int empid; char designation[15]; float salary; emp; readvalue(emp); /* function call with structure variable passed as argument */ readvalue(e) struct employee e; printf( enter employee id, designation, salary: ); scanf( %d%s%f,&e.empid,e.designation,&e.salaray); In the above example the structure variable is passed as argument in the function call and received in an argument by name e declared of the same structure in the called function and the procedure of reading data to the member of the structure is the same as what we have discussed above. Programming in C and Data Structures Unit V Page 3

struct employee int empid; char designation[15]; float salary; emp; By Reference readvalue(&emp); /* function call with structure variable passed as argument */ readvalue(e) struct employee *e; printf( enter employee id, designation, salary: ); scanf( %d%s%f,&e->empid,e->designation,&e->salaray); printf( employee id =%d\n,(*e).empid); printf( designation=%s\n,(*e).designation); printf( salary=%f\n,(*e).salary); In the above example the structure variable is passed as reference argument in the function call and received in an argument by name e declared of the same structure as pointer in the called function. The procedure of reading data to the member of the structure is done using &pointervariable->membername for members declared of type int or float and if the member is a string then the data is accessed using pointervariable->membername The procedure of printing data of the member of the structure is done using (*pointervariable).membername. Programming in C and Data Structures Unit V Page 4

Array of Structures Declaration As we know that structure is a derived data type that can group one or more variables of different data types. If the same structure is required for more than once then we declare the variable as array of structures. The declaration of array of structures is illustrated below: int marks1; ; s1[3]; In the above example s1 is a variable declared as array of structures, where s1 is the variable name and the value 3 enclosed in square brackets gives the size of the array variable. Accessing the structure with array of structure variables The following segment of code illustrates the process of accessing the structure with array of structure variables. for(i=0;i<3;i++) scanf( %d,&s[i].rollno); scanf( %d,&s[i].marks1); Here s1 is the structure variable declared as an array,rollno and marks1 are members of the structure and I is subscript variable used to vary the index of the array variable. The members of the structures are accessed by giving the structure variable name followed by subscript variable enclosed in square brackets followed by dot operator and then the member name. Arrays within structures Declaration Structure is a derived data type that can group one or more variables of different data types with a single name, if one of these variables or all of these variables is declared as an array variable then it becomes as use of arrays within structures. Example that illustrates the declaration of array within structure is given below: Programming in C and Data Structures Unit V Page 5

int marks[3]; ; s1; in the above example variable marks is declared as array of type int. Accessing data for array declared within structure The following segment of code illustrates the process of accessing arrays within structure. for(i=0;i<3;i++) scanf( %d,&s1.marks[i]); here s1 is the structure variable, marks is the member of structure declared as array variable and I is subscript variable used to vary the index of the array variable. The members of the structures declared as array are accessed by giving the structure variable name followed by dot operator and then the member name with subscript variable enclosed in square brackets. Structures and Unions: Structures Structures are derived data types that allows to group one or more variables of different data type with a single name. Syntax: struct <tagname> Data type member name; Data type member name; ; Size allocated to the structure variable is the sum of the bytes allocated to the members of the structure. Example: struct college int collegeid; char collegaddress[20]; c; Here c is the structure variable declared which takes a size of 22 bytes i.e., 2 bytes for the member college id and 20 bytes for the member collegeaddres. Unions Unions are derived data types that allows to group one or more variables of different data type with single name. Syntax: union <tagname> Data type member name; Data type member name; ; Size allocated to the union variable is the maximum number of bytes allocated to any member of the structure. Example: union type int x; char y; float z; u; Here u is the union variable declared which takes a size of 4 bytes i.e., 2 bytes for member x, 1 byte for member y and 4 bytes for member z and the maximum of these is 4 bytes which will be the size allocated to the union variable. Programming in C and Data Structures Unit V Page 6

Union of Structures: A structure variable declared as a member of the union is called as union of structure. e.g., union x Struct student s; ; Typedef The keyword typedef allows us to define an existing data type with other name. The general form of typedef is given below: typedef datatype newname; For e.g., Typedef int integer; Typedef float real; In the above example, int is redefined as integer and float redefined as real. Below we will see a simple example of how typedef is used. #include <stdio.h> typedef int integer; typedef float real integer rollno; real per_marks; printf( enter value rollno and per_marks: ); scanf( %d %f,&rollno,&per_marks); Bitfields: Bitfields is a concept supported in programming language C that allows the programmer to allocate bits of memory to variables instead of bytes of memory. Programming in C and Data Structures Unit V Page 7

If a variable needs store a value in between 0 and 7 then no need to allocate bytes of memory only bits of memory is sufficient and this can be done by accompanying the variable with colon( : ) followed by non-negative integer value. This concept can be applied only to members of a structure. The general form of usage of bit fields is given below: Struct <tagname> Data type membername:integer value; Data type membername:integer value; ; Where integer value specifies the no. of bits to be allocated for that membername. For e.g., #include <stdio.h> #define regular 1 #define lateral 5 #define A 1 #define F 2 #define E 3 #define D 4 int yoj:4; int type:3; int branch:4; int course:3; ; s; s.yoj=10; s.type=regular; s.branch=5; s.course=a; printf( %d%d%d%d,s.yoj,s.type,s.course,s.branch); Programming in C and Data Structures Unit V Page 8

In the above program few macros are defined and used in the program, a structure is defined by name student comprising of members yoj, type, branch, course with each member assigned with bits of memory. For the members defined in the structure only bits of memory is allocated rather than bytes of memory. In the statement s.type=regular, regular is macro constant defined with value 1, value 1 is assigned to s.type for which 3 bits of memory is allocated. Enumerated data types: The programming language C supports programmers to create enumerated data types by using the keyword enum. The programmer can create his/her own data type and define what values the variables of these data types can hold. The general form of the usage of enum keyword is given below: enum datatypenamevalue,value1 valuen; where datatypename can be any name that follows the rules of the identifier and value,value1 valuen can take any identifier. Note: The values are not to be enclosed in double quotes and these values cannot be integer constant. By default the first value is assigned with unsigned integer constant 0 and the next value with 1 and so on. #include <stdio.h> enum monthjan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec; clrscr(); printf( %d\n,jan); printf( %d\n,jun); printf( %d\n,dec); If the first value of the defined data type should start with 1 then it is defined as follows: #include <stdio.h> enum monthjan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec; clrscr(); printf( %d\n,jan); printf( %d\n,jun); printf( %d\n,dec); Programming in C and Data Structures Unit V Page 9

Review Questions: 1. Define Structures. Explain with an example how structure members are initialized and accessed. (Refer Page No. 1& 2). 2. Explain with an example how to pass structure variable as argument by value and by reference. 3. (Refer Page No.3 &4). 4. Explain with an example the usage of array of structures. (Refer Page No.5). 5. Explain with an example the usage of array within structures. (Refer Page No.5&6). 6. Differentiate structures from unions. (Refer Page No.6). 7. Write Short notes on typedef, bitfields and enum data type. (Refer Page No. 7,8 & 9). Programming in C and Data Structures Unit V Page 10