IV Unit Second Part STRUCTURES

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

3.3 Structures. Department of CSE

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

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

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

Subject: Fundamental of Computer Programming 2068

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

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

Multi-Dimensional arrays

Data Types and Variables in C language

Unit IV & V Previous Papers 1 mark Answers

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

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

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

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

Basics of Programming

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

Structures Unions and Enumerated Datatypes 224

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

Single Dimension Arrays

User Defined data Types

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

Chapter-14 STRUCTURES

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

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

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Dynamic Memory Allocation

UNIT - V STRUCTURES AND UNIONS

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

DEPARTMENT OF MATHS, MJ COLLEGE

Variation of Pointers

Pointers and Arrays 1

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

Unit 3 Decision making, Looping and Arrays

Questions Bank. 14) State any four advantages of using flow-chart

ALQUDS University Department of Computer Engineering

Chapter 10 C Structures, Unions, Bit Manipulations

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

C library = Header files + Reserved words + main method

POINTERS, STRUCTURES AND INTRODUCTION TO DATA STRUCTURES

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

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

BLM2031 Structured Programming. Zeyneb KURT

COMPUTER APPLICATION

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

Computer Programming Unit 3

Module 6: Array in C

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

Advanced C Programming and Introduction to Data Structures

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

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

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

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

A Fast Review of C Essentials Part I

INTRODUCTION 1 AND REVIEW

Problem Solving and 'C' Programming


C Syntax Out: 15 September, 1995

AMCAT Automata Coding Sample Questions And Answers

Guide for The C Programming Language Chapter 4

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

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

Function Call Stack and Activation Records

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

Arrays and Pointers (part 1)

C Language, Token, Keywords, Constant, variable

Fundamentals of Computer Programming Using C

C Programming Review CSC 4320/6320

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

Basic Elements of C. Staff Incharge: S.Sasirekha

Chapter-8 DATA TYPES. Introduction. Variable:

Multiple Choice Questions ( 1 mark)

struct structure_name { //Statements };

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

12 CREATING NEW TYPES

C Programming Multiple. Choice

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

Array. Arrays. Declaring Arrays. Using Arrays

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

Pointers, Dynamic Data, and Reference Types

Decision Making -Branching. Class Incharge: S. Sasirekha

Object Oriented Pragramming (22316)

Government Polytechnic Muzaffarpur.

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

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

CS100 : Computer Programming

DS Assignment I. 1. Set a pointer by name first and last to point to the first element and last element of the list respectively.

Multiple-Subscripted Arrays

M1-R4: Programing and Problem Solving using C (JAN 2019)

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

The New C Standard (Excerpted material)

CSE 230 Intermediate Programming in C and C++

SRI VIDYA COLLEGE OF ENGINEERING & TECHNOLOGY, VIRUDHUNAGAR Department of CSE & IT Internal Test I

Computer Systems Principles. C Pointers

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; };

VARIABLES AND CONSTANTS

Transcription:

STRUCTURES IV Unit Second Part 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 s1= xyz,123; Or s1; s1= xyz,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 s1= xyz,123; Or s1; s1= xyz,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 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 1

For eg., s1; printf( enter the name and rollno: ); scanf( %s%s%d,s1.name,&s1.rollno); printf( name=%s\n,s1.name); printf( rollno=%d\n,s1.rollno); In the above example a structure by name student is declared with members name declared as character array and rollno declared of type 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(or)Structure within Structure: 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 int empid; char designation[15]; struct salary int basic; int da; s1; emp; struct date int day; int month; int year; ; Struct employee int empno; float basic; struct date joindate; emp1; 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. 2

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. # include<stdio.h> // Structure declaration ; int roll; char name[15]; fun(); //Function Prototype s=24, xyz,x; x=fun(s); /* function call with structure variable passed as argument */ printf( %d %s,x.roll,x.name); fun ( b) b.roll=99; strcpy(b.name, abc ); return b; In the above example the structure variable is passed as argument in the function call and received in an argument by name x 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. 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. 3

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: 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. Unions Unions are derived data types that allows to group one or more variables of different data type with single name. Syntax: Syntax: struct <tagname> union <tagname> ; ; Size allocated to the structure variable is the sum of Size allocated to the union variable is the maximum 4

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. 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. 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. 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. 5

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> #include <conio.h> int date:5; int month:4; int year:12; s; s.date=10; s.month=6; s.year=2013; printf( %d%d%d,s.date,s.month,s.year); In the above program, structure is defined by name student comprising of members date, month, year 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. Enumerations An enumeration is a list of constant integer values. Ex: enum colors RED,WHITE,BLUE; By default, the first name in an enumeration has value 0, the next 1, and so on. In this example, RED = 0, WHITE = 1, BLUE = 2. The output of the following statement is 2. printf( %d,blue); We can specify explicit values to enumerators with the help of equal to (=) operator. The subsequent enumerators continue from the assigned value by incrementing 1. Ex: enum branchcode CIVIL=1,EEE,MECH,ECE,CSE; Here, EEE is 2, MECH is 3, ECE is 4 and CSE is 5. Names in an enumeration must be distinct. Values in the same enumeration need not be distinct. Ex: enum names ASHA=28,SWAPNA=21,JOHN=5, HARI=46; #include<stdio.h> enum colors RED,WHITE,BLUE; printf( %d %d %d,red,white,blue); Output: 0 1 2 6

Structure Pointer The way we can have a pointer pointing to an int, or a pointer pointing to a char, similarly we can have a pointer pointing to a struct. Such pointers are known as structure pointers. Using Structure Pointers: To access the values of the members of the structure using pointers we need to perform following things, Let s take the following segment of code #include<stdio.h> void main( ) int rno; char name[10]; float avg; ; s=101, ABC,78.98; *ptr; ptr=&s; printf( \n%d %s %f, s.rno,s.name,s.avg); printf( \n%d %s %f, ptr rno, ptr name, ptr avg); getch(); ptr is a pointer to structure, then the members of the structure can also be accessed using selection operator denoted by (which is formed by minus sign and greater than symbol). Structure Containing Pointers We can also contain pointers as members of the structures. The pointer is pointing to a value of the structure member or some other variable. The below code illustrates pointers as members of structures. #include<stdio.h> char name [10]; int rno; float *ptr; ; void main( ) s1= "ABC",405; float avg=34.5; s1.ptr=&avg; // initializing pointer printf ("%s %d %f",s1.name,s1.rno,*s1.ptr); // accessing the value of a variable 7