Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Programming in C Chapter 10 Structures and Unions
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
C Language.
Intermediate Code Generation
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.
Programming and Data Structure
Structure.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures Spring 2013Programming and Data Structure1.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
VBA Modules, Functions, Variables, and Constants
Structure of a C program
Introduction to C Programming CE Lecture 10 Data Structures typedef and struct.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 Structures. 2 C gives you several ways to create a custom data type. –The structure, which is a grouping of variables under one name and is called an.
Review of C++ Programming Part II Sheng-Fang Huang.
LECTURE 4: INFORMATION REPRESENTATION (PART II) COMP26120: ALGORITHMS AND IMPERATIVE PROGRAMMING.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
Advanced Data types and Sorting
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Structure A structure is a collection of variables of different data type under one name. It is a derived data type. e.g. struct employee {int empno; char.
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
Learners Support Publications Classes and Objects.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
© Oxford University Press All rights reserved. CHAPTER 8 STRUCTURES.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
 Complex data types  Structures  Defined types  Structures and functions  Structures and pointers.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
STRUCTURES. C structures: aggregate, yet scalar  aggregate in that they hold multiple data items at one time  named members hold data items of various.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
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.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.
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)
Windows Programming Lecture 03. Pointers and Arrays.
Constructors and Destructors
Pointers, Enum, and Structures
Java Primer 1: Types, Classes and Operators
Pointers and Pointer-Based Strings
Student Book An Introduction
C Basics.
Pointers and References
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Using Arrays in C Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Structures and Union.
Derived types.
Constructors and Destructors
Classes and Objects.
Pointers and Pointer-Based Strings
Structures and Union.
C Language B. DHIVYA 17PCA140 II MCA.
Structures, Unions, and Enumerations
Presentation transcript:

Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

STRUCTURE A structure is a collection of variables under a single name. These variables can be of different types, and each has a name that is used to select it from the structure. There can be structures within structures, which is known as nesting of structures. Arrays of structures can be formed and initialized as required. Pointers may also be used with structures. Structures may be passed as function arguments and they may also be returned by functions.

Declaring Structures and Structure Variables A structure is declared by using the keyword struct followed by an optional structure tag followed by the body of the structure. The variables or members of the structure are declared within the body. The general format of declaring a simple structure is given as follows.

Cont. The structure_tag_name is the name of the structure. The structure_variables are the list of variable names separated by commas. There are three different ways to declare and/or define a structure. These are Variable structure Tagged structure Type-defined structure A variable structure may be defined as follows: struct { member_list }variable_identifier;

Cont. 1.A structure can be defined as a user-defined data type that is capable of holding heterogeneous data of basic data type. 2. The structure is simply a type template with no associate storage. 3. The proper place for structure declarations is in the global area of the program before main(). 4. It is not possible to compare structures for equality using ‘==’, nor is it possible to perform arithmetic on structures.

Accessing the Members of a Structure The members of a structure can be accessed in three ways. One of the ways consists of using the ‘.’, which is known as the ‘dot operator’. The members are accessed by relating them to the structure variable with a dot operator. The general form of the statement for accessing a member of a structure is as follows:. ;

Initialization of Structures Structures that are not explicitly initialized by the programmer are, by default, initialized by the system. In most of the C compilers, for integer and float data type members, the default value is zero. For char and string type members the default value is ‘\0’. Example:

Cont. The general construct for initializing a structure can be any of the two forms given as follows. struct { ; } = {constant1,constant2,..}; or struct ={constant1,constant2,..};

Copying & Comparing Structures A structure can be assigned to another structure of the same type. Here is an example of assigning one structure to another. Comparing one structure variable with another is not allowed in C. However, when comparing two structures, one should compare the individual fields in the structure. Example:

Typedef and its Use in Structure Declarations The typedef keyword allows the programmer to create a new data type name for an existing data type. The general form of the declaration statement using the typedef keyword is given as follows. typedef ; The typedef statement does not occupy storage; it simply defines a new type. typedef statements can be placed anywhere in a C program as long as they come prior to their first use in the code. The following examples show the use of typedef. typedef int id_number; typedef float weight; typedef char lower_case;

Nesting of Structures A structure can be placed within another structure. In other words, structures can contain other structures as members. A structure within a structure means nesting of structures. In such cases, the dot operator in conjunction with the structure variables are used to access the members of the innermost as well as the outermost structures. It must be noted that an innermost member in a nested structure can be accessed by chaining all the concerned structure variables, from outermost to innermost, with the member using the dot operator.

Arrays of Structures The structure variable would be an array of objects, each of which contains the member elements declared within the structure construct. The general construct for declaration of an array structure is given as follows: struct { ;... } [index]; Or struct [index];

Example: Arrays of Structures

Initializing Arrays of Structures Initializing arrays of structures is carried out in much the same way as arrays of standard data types. A typical construct for initialization of an array of structures would appear as follows:

Arrays within the Structure An innermost member in a nested structure can be accessed by chaining all the concerned structure variables, from outermost to innermost, with the member using the dot operator. Example:

Structures and Pointers At times it is useful to assign pointers to structures. A pointer to a structure is not itself a structure, but merely a variable that holds the address of a structure. This pointer variable takes four bytes of memory just like any other pointer in a 32-bit machine. Declaring pointers to structures is basically the same as declaring a normal pointer. There are many reasons for using a pointer to a struct. One of them is to make a two-way communication possible within functions. This aspect is explained with examples in the following section. A pointer to a structure is not itself a structure, but merely a variable that holds the address of a structure.

Structures and Pointers A typical construct for declaring a pointer to a structure will appear as follows: struct /* structure declaration */ { ;... ; }*ptr; struct { ; … ; }; struct *ptr;

Structures and Functions Passing and working with pointers to large structures may be more efficient while passing structures to a function and working within it. When a structure is passed as an argument, each member of the structure is copied. In fact, each member is passed by value. In case the member is an array, a copy of this array is also passed. This can prove to be inefficient where structures are large or functions are called frequently. The general construct for passing a structure to a function and returning a structure is struct structure_tag function_name(struct structure_tag structure_variable);

Initializing Structures Ex 1 #include struct employee { char name[35],dept[25]; unsigned int salary; }e1={"ALLEN","COMPUTER",2500}; int main() { struct employee e2={"SMITH","MECHANICAL",3000}; printf("NAME\tDEPT\t\tSALARY\n"); printf("%s\t%s\t%d\n",e1.name,e1.dept,e1.salary); printf("%s\t%s\t%d\n",e2.name,e2.dept,e2.salary); return 0; }

Taking Structures Value form user #include struct employee { char name[35],dept[25]; unsigned int salary; }e1,e2; int main() { printf("Enter the name,dept and salary of employee 1:\n"); scanf("%s%s%d",e1.name,e1.dept,&e1.salary); printf("Enter the name,dept and salary of employee 2:\n"); scanf("%s%s%d",e2.name,e2.dept,&e2.salary); printf("NAME\tDEPT\t\tSALARY\n"); printf("%s\t%s\t%d\n",e1.name,e1.dept,e1.salary); printf("%s\t%s\t%d\n",e2.name,e2.dept,e2.salary); return 0; }

Arrays of Structures #include struct employee { char name[35],dept[25]; unsigned int salary; }; int main() { int i; struct employee e[3]={ {"ALLEN","COMPUTER",2400}, {"SMITH","MECHANICAL",2300}, {"GATES","ELECTRICAL",2200} };

printf("NAME\tDEPT\t\tSALARY\n"); for(i=0;i<3;i++) printf("%s\t%s\t%d\n",e[i].name,e[i].dept,e[i].salary); return 0; }

Hospital Mgmt #include struct patients { char fname[20],mname[20],surname[20],disease[20]; char dob[11]; }; int main() { int i; char disease[20];

struct patients p[4]={ {"ALLEN","SMITH","DSOUZA","LUKEMIA"," "}, {"ALICE","SMITH","DSOUZA","TUBERCLOSIS"," "}, {"GATES","WILL","RICE","LUKEMIA"," "}, {"BILL","ANTHONY","DSOUZA","CHOLERA"," "} }; printf("Enter the disease\n"); scanf("%s",disease); printf("FNAME\tMNAME\tSURNAME\tDISEASE\tDOB\n"); }

for(i=0;i<4;i++) if(strcmp(p[i].disease,disease)==0) printf("\n%s\t%s\t%s\t%s\t%s",p[i].fname,p[i].mname,p[i].surname,p[i].disease,p[i].dob); return 0; }

Club Data #include struct player { char name[30]; int age,matches,runs; float average; }; int main() { int i,n; struct player p[100]; printf("\nEnter the number of players: "); scanf("%d",&n); printf("\nEnter the data for players"); for(i=0;i<n;i++) {

printf("\nPLAYER %d",i+1); printf("\nName: "); scanf("%s",p[i].name); printf("\nAge: "); scanf("%d",&p[i].age); printf("\nNo. of Matches: "); scanf("%d",&p[i].matches); printf("\nNo. of Runs: "); scanf("%d",&p[i].runs); p[i].average=(float)p[i].runs/p[i].matches; }

printf("\nSports Club Data"); printf("\nName\t\tAge\tMatches\tRuns\tAve rage"); for(i=0;i<n;i++) printf("\n%s\t\t%d\t%d\t%d\t%f",p[i].na me,p[i].age,p[i].matches,p[i].runs,p[i].avera ge); return 0; }

Employees Data Sorted According to ID #include struct employee { char name[35]; unsigned int id,salary; }e[100]; int main() { int i,j,n; printf("\nEnter the number of employees: "); scanf("%d",&n); printf("\nEnter the data for employees");

for(i=0;i<n;i++) { printf("\nEmployee %d",i+1); printf("\nName: "); scanf("%s",e[i].name); printf("\nID: "); scanf("%d",&e[i].id); printf("\nSalary: "); scanf("%d",&e[i].salary); }

for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(e[i].id>e[j].id) { struct employee temp; temp=e[i]; e[i]=e[j]; e[j]=temp; } printf("\nEmployees Data Sorted According to ID"); printf("\nName\t\tID\tSalary"); for(i=0;i<n;i++) printf("\n%s\t\t%d\t%d",e[i].name,e[i].id,e[i].salary); return 0;}