Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.

Slides:



Advertisements
Similar presentations
LIST PROCESSING.
Advertisements

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.
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 Structures By C. Shing ITEC Dept Radford University.
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.
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
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.
C Programming : Dynamic memory allocation & Structures 2008/11/19 Made by Jimin Hwa Edited and presented by Souneil Park
Kernighan/Ritchie: Kelley/Pohl:
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.
UBC104 Embedded Systems Variables, Structures & Pointers.
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.
C PROGRAMMING LECTURE 17 th August IIT Kanpur C Course, Programming club, Fall by Deepak Majeti M-Tech CSE
Programming in C Structs and Unions. No Classes in C Because C is not an OOP language, there is no way to combine data and code into a single entity.Because.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Programming in C Structs and Unions. Java vs C Suppose you were assigned a write an application about points and straight lines in a coordinate plane.
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002.
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
STRUCTURES (PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz.
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
Homework Finishing up K&R Chapter 6 today Also, K&R 5.7 – 5.9 (skipped earlier)
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Unions and Bitfields Gabriel Hugh Elkaim Spring 2013.
Today’s Material Aggregate Data Types: Structures and Unions
STRUCTURES. Structures in C A structure is –a convenient way of grouping several pieces of related information together –a collection of variables under.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Struct Advanced Programming Language. Struct Struct allows us to create a new data type We can now declare “struct Point” as a new data type and can use.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Structures and Unions in C Alan L. Cox
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
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.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
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()
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
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.
CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
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.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
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.
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.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Lecture 10 union, enumeration, bit
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
Structures, Unions, Enumerations
Structures.
DATA HANDLING.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
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.
Sudeshna Sarkar 7th March 2017
prepared by Senem Kumova Metin modified by İlker Korkmaz
Chapter 1: Introduction to Data Structures(8M)
Programming Language C Language.
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Structures, Unions, and Enumerations
Presentation transcript:

Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C

Senem Kumova Metin 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

Senem Kumova Metin Defining Structures /* 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]; };

Senem Kumova Metin Structures : Creating objects struct complex {int real; int imaginary;}; // create an object struct complex s; struct complex * p= (struct complex *) malloc( sizeof(struct complex)); // create an array of objects struct complex a[4]; struct complex * p= (struct complex *) malloc( sizeof(struct complex)*4);

Senem Kumova Metin Structures : Accessing members of structures 1 struct complex {int real; int imaginary;}; struct complex s; // access members s.real=5; s.imaginary =3; struct complex * p= (struct complex *) malloc( sizeof(struct complex)); // access members p->real=5; p->imaginary=3;

Senem Kumova Metin Structures : Accessing members of structures 2 struct complex {int real; int imaginary;}; int i; struct complex a[4]; struct complex * p= (struct complex *) malloc( sizeof(struct complex)*4); for(i=0;i<4;i++) { a[i].real=i; p[i].real=i; a[i].imaginary=i; p[i].imaginary=i; }

Senem Kumova Metin Structures : DECLARATION ALTERNATIVES struct record { int ID; char * name; char grade; }; struct record s1; struct record { int ID; char * name; char grade; } s1, s2; struct { int ID; char * name; char grade; } s1, s2; struct record { int ID; char * name; char grade; }; typedef struct record rec; typedef struct { int ID; char * name; char grade; } rec; rec s1;

Senem Kumova Metin Structures as Function Arguments Examples Example1. Call by value Example2. Call by value Example3. Call by reference Example4. Arrays

Senem Kumova Metin Example 1 : Define functions // A function to add two integers int sum(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 sum ( struct complex a, struct complex b ) { struct complex result; result.r=a.r+b.r; result.i=a.i+b.i; return result;} // A function to print out a complex number void my_print ( struct complex a) { printf(“%d+%di\n”, a.r, a.i );}

Senem Kumova Metin Example 1 : Define main() // Structure definition struct complex { int r; int i; } ; // function prototypes struct complex sum ( struct complex a, struct complex b ); void my_print ( struct complex a); main() { struct complex e1={2,3}; struct complex e2 ={1,2}; struct complex e3; my_print(e1); my_print(e2); e3=sum(e1, e2); my_print(e3);} void my_print ( struct complex a) { printf(“%d+%di\n”, a.r, a.i );}

Senem Kumova Metin Example 2 Define a date structure day - month – year Define a function to find the next day TODAY  NEXT DAY –5 – 1 – 2009  6 – –  – – 2009  1 – In main create an object of today and call function to find next day

Senem Kumova Metin Example 2 typedef struct { int day; int month; int year; } date ; // structure definition date calculate ( date d ) // function definition {date n; if( d.day==30) if(d.month==12) { n.day=1; n.month=1; n.year=d.year+1; } else { n.day=1; n.month=d.month+1; n.year=d.year; } else {n.day=d.day+1; n.month=d.month; n.year=d.year; } return n;} main()// main function { date today ={12, 10, 2005}; date next_day; next_day= calculate(today); printf(“Next day is %d.%d.%d”, next_day.day,next_day.month,next_day.year); }

Senem Kumova Metin Example 3 (Call by reference) : Define functions // A function to increase an integers void increase_i (int *a) { *a=*a+1;} typedef struct { int n; int d; } fraction; // fraction=n/d // A function to increase a fraction void increase_f (fraction * a ) { a->n= a->n +a->d; } // n/d+1 = (n+d) /d

Senem Kumova Metin Example 3 (Call by reference): Define functions // A function to swap integers void swap_i (int *a, int * b) { int tmp; tmp=*a; *a=*b; *b=tmp;} typedef struct { int n; int d; } fraction; // A function to swap fractions void swap_f (fraction *a, fraction * b) { fraction tmp; tmp=*a; *a=*b; *b=tmp;}

Senem Kumova Metin Example 3 (Call by reference): Define main() typedef struct { int n; int d; } fraction; void swap_f (fraction *a, fraction * b); void increase_f (fraction * a ); main() { fraction x={3,4}; fraction y={4,5}; increase_f(&x); increase_f(&y); swap_f(&x,&y); swap_f(&y,&x); printf(“%d/%d\n”,x.n,x.d); printf(“%d/%d\n”,y.n,y.d);}

Senem Kumova Metin Example 4 : Arrays ( Bookstore example) You will create Bbookstore_system Ask user to enter how many different books he has. You have to store name and total number of copies for each book. Ask user to enter information for each book. Define a function to calculate the number of books in store.

Senem Kumova Metin Example 4 : Arrays void main() { int s, i, total; book * array; printf("How many books do you have?"); scanf("%d",&s); array= (book *)malloc(sizeof( book)*s); for(i=0;i<s;i++) scanf("%d",&(array[i].number)); total=sum(array,s); // sum(&array[0]); printf("total number of books %d",total); } typedef struct { char * name; int number;} book; int sum( book * a, int size) { int i; int r=0; for (i=0;i<size;i++) r=r+a[i].number; return r; } /*int sum( book a[], int size) { int i; int r=0; for (i=0;i<size;i++) r=r+a[i].number; return r; } */

Senem Kumova Metin UNIONS A union –defines a set of alternative values that may be stored in a shared poriton of memory – is similar to structures a collection of variables under a single name Examples : union int _or_float {int i; float f;};

Senem Kumova Metin Example 1: UNIONS union int_or_char {char c; int i; }; int main() { union int_or_char test; test.i= 83; printf("i= %d\n", test.i);// 83 printf("c= %c\n", test.c);// S test.c='A'; printf("i= %d\n", test.i); printf("c= %c\n", test.c); return 0; }

Senem Kumova Metin union int_or_double { /* to be used as an integer OR a double*/ int i; /*needs 4 bytes*/ double d; /*needs 8 bytes*/}; main() { printf ("%d\n", sizeof(int)); printf ("%d\n", sizeof(double)); printf ("%d\n", sizeof(union int_or_double)); } Example 2: UNIONS

Senem Kumova Metin Bit Fields The bit field is used to pack the bits. An int or unsigned member of a structure or union can be declared to consist of a specified number of bits. Such a member is called a bit field, and the number of associated bits is called its width. The width is at most the number of bits in a machine word. The compiler packs the bit fields into a minimal number of machine words

Senem Kumova Metin Example : Bit Fields struct Bits { int a:2; /* 2 bit */ unsigned b:4; /* 4 bits */ int c:1; /* 1 bit */ }; int main() { struct Bits x; x.a=1; x.b=5; x.c=1; printf("a:%d b: %d c:%d\n", x.a, x.b, x.c); return 0; }