STRUCTURES. Structures in C A structure is –a convenient way of grouping several pieces of related information together –a collection of variables under.

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
LIST PROCESSING.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Programming in C Chapter 10 Structures and Unions
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.
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.
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
Chapter 6 Data Types
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
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.
Senem KUMOVA METİN CS FALL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C.
Dynamic Memory Allocation
C Programming : Dynamic memory allocation & Structures 2008/11/19 Made by Jimin Hwa Edited and presented by Souneil Park
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
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.
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.
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.
Structures ANSI-C. Review: Data Structures Functions give us a way to organize programs. Data structures are needed to organize data, especially: –1.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
STRUCTURES (PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 11: Structure and Union 1.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Array, Structure and Union
Structures. Structures (1) u Structures are C’s way of grouping collections of data into a single manageable unit. –This is also the fundamental element.
Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Functions & Pointers in C Jordan Erenrich
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
Computer Programming for Engineers
Structures 142 S -1 What is a structure? It is a collection of possibly different types Name the collection Name the components For example: a student.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
CSE 251 Dr. Charles B. Owen Programming in C1 structs Aggregating associated data into a single variable Box width length height Circle radius int main()
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
CS SUMMER LECTURE 1 by İlker Korkmaz.
LINKED LISTS.
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.
Week 9 - Monday CS222.
Presentation transcript:

STRUCTURES

Structures in C A structure is –a convenient way of grouping several pieces of related information together –a collection of variables under a single name Examples : real number && imaginary number  complex number ( 3+5i ) height && width && length  rectangular prism

Structures in C The variables in structures can be of different types, and each has a name which is used to select it from the structure Example : ID (integer) && Name (char array)  A Student record A structure is a new named type that may involve several different typed variables

Defining Structures 1 struct complex_number { int real_part; int imaginary_part; }; “complex_number” is the tag name “struct complex_number” is the new type

Defining Structures 2 /* DEFINITION OF RECTANGULAR PRISM */ struct rectangular_prism {int height; int width; int length;}; // name of new type?? /* DEFINITION OF STUDENT RECORD */ struct student_record { int ID; char name[100]; };

Structures : Creating objects struct complex_number { int real_part; int imaginary_part;}; // Below the definition of structure you can create // objects from it  “s1” and “s2” struct complex_number s1; struct complex_number s2; s2 real_part imaginary_part s1 real_part imaginary_part

Structures : Creating objects struct rectangular_prism {int height; int width; int length;}; // Below the definition of structure you can create // objects from it  “obj” struct rectangular_prism obj; obj height lengthwidth

Structures : Creating static arrays of objects struct student_record { int ID; char name[100]; }; // Creates an array of student records  “group” struct student_record group[4]; group IDname IDname IDname IDname group[0]  group[1]  group[2]  group[3] 

Structures : Creating dynamic arrays of objects struct rec { int ID; char name[100]; }; // Creating a dynamic array // of student records  “group” struct rec * group; // DECLARES POINTER group = ( struct rec * ) malloc ( sizeof (struct rec) * 4 ); size of objects in array number of objects in array (array size)

Structures : Accessing members of structures struct rectangular_prism {int height; int width; int length;}; // Create an object from the structure defined above  “obj” struct rectangular_prism obj; // Members of the objects can be accessed by putting a dot // following the object name obj.height=10; obj.width=15; obj.length=40; obj height=10 length=40width=15

Structures : Accessing members of structures struct rectangular_prism {int height; int width; int length;}; struct rectangular_prism obj; // Create a pointer to point object “obj” struct rectangular_prism *p = &obj; (*p).height=10 // or obj.height or p->height=10 p->width=15; p->length=40; obj == *p height=10 length=40width=15

Structures : Accessing members of structures // Defines structure struct student_record { int ID; char name[100]; }; // Creates an array of student records  “group” struct student_record group[2]; group[0].ID=200710; strcpy(group[0].name, “doddy”); group[1].ID=200711; strcpy(group[1].name,group[0].name); group ID=200710name= “doddy” ID=200711name =?? group[0]  group[1] 

Structures : DECLARATION ALTERNATIVES Declaration 1 : struct record { int ID; char * name; char grade; }; struct record s1; struct record s2; struct record s3; Declaration 2 : struct record { int ID; char * name; char grade; } s1, s2; struct record s3;

Structures : DECLARATION ALTERNATIVES Declaration 1 : struct record { int ID; char * name; char grade; }; struct record s1; struct record s2; Declaration 3 : struct { int ID; char * name; char grade; } s1, s2; /* no tag name */ /* no permission to declare other variables of this type */

Structures : DECLARATION ALTERNATIVES Declaration 4 : struct record { int ID; char * name; char grade; }; typedef struct record rec; rec s1; struct record s2; Declaration 5 : /* high degree of modularity and portability */ typedef struct { int ID; char * name; char grade; } rec; rec s1; rec s2;

Initialization of Structure Objects 1.struct names { char name[10]; int length; int weigth;} man[3]= { “Tom”, 180, 65, “George”, 170, 68, “Bob”, 190, 100 }; 2.struct names woman[2]={{“Mary”, 170, 55}, {“Sue”, 160,67}}; 3.struct names your; your. name=“Jane”; your.length=160; your.weigth=50;

Structures in Structures #include struct physical_info { int length; int weigth; } ; struct record { int salary; int working_hour; struct physical_info man; } ; main() {struct record s1; s1.salary=10000; s1.working_hour= 6; s1.man.length=180; s1.man.weigth=78; }

Exercise 1 on Structures Declare a structure for complex numbers (real and imaginary part) Create 1 dynamic object (use pointers) Ask user to fill the object Print out the complex number as given below output Please give the values for complex number : 5 6 Complex number : 5 + 6i

Exercise 1 on Structures struct complex {int real; int imaginary; }; main() { struct complex * p; // Declare pointer for object // Memory allocation for object p=(struct complex *) malloc(sizeof(struct complex)); printf( " Please give the values for complex number : " ); scanf( "%d%d", &(p->real), & (p->imaginary) ); printf( "Complex number : %d + %d i", p->real, p->imaginary ); }

Senem Kumova Metin // A function to add two integers int add(int a, int b) { int result= a+b; return result;} struct complex { int r; int i; }; // A function to add two complex numbers struct complex add ( struct complex a, struct complex b ) { struct complex result; result.r=a.r+b.r; result.i=a.i+b.i; return result;} Exercise 2 : Define a function to add two complex numbers

Exercise 3 on Structures struct rectangular_prism {int height; int width; int length;}; int volume(struct rectangular_prism x) {return x.height*x.width*x.length; } int area (struct rectangular_prism x) { return 2*x.width*x.lenght + 2*x.lenght*x.height + 2*x.width*x.lenght; }