1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.

Slides:



Advertisements
Similar presentations
Programming in C Chapter 10 Structures and Unions
Advertisements

1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
Structures Often we want to be able to manipulate logical entities as a whole For example, complex numbers, dates, student records, etc Each of these must.
C Language.
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.
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structure.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
Kernighan/Ritchie: Kelley/Pohl:
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Pointers CSE 2451 Rong Shi.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Functions, Pointers, Structures Keerthi Nelaturu.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Learners Support Publications Classes and Objects.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Array, Structure and Union
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
 Complex data types  Structures  Defined types  Structures and functions  Structures and pointers.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
ECE Application Programming
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
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.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
LINKED LISTS.
Chapter 8 Arrays, Strings and Pointers
Programming and Data Structures
Structures, Unions, Enumerations
A bit of C programming Lecture 3 Uli Raich.
Chapter 10-1: Structure.
C Programming Tutorial – Part I
TMF1414 Introduction to Programming
Module 2 Arrays and strings – example programs.
CMSC 104, Section 4 Richard Chang
Structures.
הגדרת משתנים יום שלישי 27 נובמבר 2018
Derived types.
Sudeshna Sarkar 7th March 2017
Classes and Objects.
Structures In C Programming By Rajanikanth B.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Structures, Unions, and Enumerations
Presentation transcript:

1 Structures

2 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

3 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

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

5 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

6 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

7 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

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

9 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

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

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

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

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

14 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

15 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

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

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

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

19 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

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

Union union Data { int i; float f; char str[20]; }; int main( ) { union Data data; data.i = 10; data.f = 220.5; strcpy( data.str, "C Programming"); printf( "data.i : %d\n", data.i); printf( "data.f : %f\n", data.f); printf( "data.str : %s\n", data.str); return 0; } 21 data.i : data.f: data.str : C Programming

union Data { int i; float f; char str[20]; }; int main( ) { union Data data; data.i = 10; printf( "data.i : %d\n", data.i); data.f = 220.5; printf( "data.f : %f\n", data.f); strcpy( data.str, "C Programming"); printf( "data.str : %s\n", data.str); return 0; } 22 data.i : 10 data.f : data.str : C Programming Union