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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
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.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
C Language.
Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
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.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Programming and Data Structure
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Structures Spring 2013Programming and Data Structure1.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Structures in C.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
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.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
Exercise 11 Dynamic allocation, structures, linked lists.
Introduction to C Programming CE Lecture 10 Data Structures typedef and struct.
Exercise 11 Dynamic allocation and structures. Dynamic Allocation Array variables have fixed size, used to store a fixed and known amount of variables.
1 Structures, Dynamic Memory Allocation. 2 Agenda Structures Definition & usage Pointers to structures Arrays and pointers in structures Dynamic Memory.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Structures and Dynamic Allocation. Dynamic Memory Allocation The memory requirement of our program is not always known in advance  Arrays.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
C Course Lecture 4 This lecture we'll talk about: Multi-dimensional arrays. Pointer arithmetic. Pointers to structures. Multi-file programming. What is.
Functions, Pointers, Structures Keerthi Nelaturu.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Learners Support Publications Classes and Objects.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
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:
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.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
11 Introduction to Object Oriented Programming (Continued) Cats.
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.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
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.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
LINKED LISTS.
Lesson #8 Structures Linked Lists Command Line Arguments.
CS1010 Programming Methodology
EECE.2160 ECE Application Programming
הגדרת משתנים יום שלישי 27 נובמבר 2018
C Programming Strings.
EECE.2160 ECE Application Programming
Classes and Objects.
Programming Structures.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Programming Languages and Paradigms
EECE.2160 ECE Application Programming
Week 9 - Monday CS222.
Presentation transcript:

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 be composed of more than one variable, but are logically units A struct (short for structure) is a collection of variables of different types, gathered into one super-variable It is used to define more complex data types Variables in a struct are called members or fields

Example – complex numbers. The following is the definition of a new variable of type complex number: struct complex { int real; int img; }; Once we define a structure, we can treat it as any type. In a program, we can then write: struct complex num1, num2, num3;

Access structure members If A is of some structure with a member named x, then A.x is that member of A struct complex C; C.real = 0; If A is a pointer to a structure with a member x, then A->x is that member of the variable pointed by A. This is simply shorthand for - (*A).x struct complex *pc = &C; pc->real = 1;

A more convenient usage with typedef An alternative definition: typedef struct complex_t { int real; int img; } complex; Now the program has a new variable type - complex. This way we dont have to write struct complex every time! For example, we can define two complex numbers in the following line: complex num1, num2;

Examples AddComplex.c

AddComplex – step by step complex a, b, c; printf(…"); scanf("%lf%lf",&(a.real),&(a.img)); printf(…"); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(result = %g+%gi\n",c.real,c.img); return 0; …… realimg a …… realimg b …… realimg c

AddComplex – step by step complex a, b, c; printf(…"); scanf("%lf%lf",&(a.real),&(a.img)); printf(…"); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(result = %g+%gi\n",c.real,c.img); return 0; …… realimg a …… realimg b …… realimg c

AddComplex – step by step complex a, b, c; printf(…"); scanf("%lf%lf",&(a.real),&(a.img)); printf(…"); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(result = %g+%gi\n",c.real,c.img); return 0; realimg a …… realimg b …… realimg c

AddComplex – step by step complex a, b, c; printf(…"); scanf("%lf%lf",&(a.real),&(a.img)); printf(…"); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(result = %g+%gi\n",c.real,c.img); return 0; realimg a …… realimg b …… realimg c

AddComplex – step by step complex a, b, c; printf(…"); scanf("%lf%lf",&(a.real),&(a.img)); printf(…"); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(result = %g+%gi\n",c.real,c.img); return 0; realimg a realimg b …… realimg c

AddComplex – step by step complex a, b, c; printf(…"); scanf("%lf%lf",&(a.real),&(a.img)); printf(…"); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(result = %g+%gi\n",c.real,c.img); return 0; realimg a realimg b …… realimg c

AddComplex – step by step complex AddComp(complex x, complex y) { complex z; z.real = x.real + y.real; z.img = x.img + y.img; return z; } realimg x realimg y …… realimg z

AddComplex – step by step complex AddComp(complex x, complex y) { complex z; z.real = x.real + y.real; z.img = x.img + y.img; return z; } realimg x realimg y …6.0 realimg z

AddComplex – step by step complex AddComp(complex x, complex y) { complex z; z.real = x.real + y.real; z.img = x.img + y.img; return z; } realimg x realimg y realimg z

AddComplex – step by step complex AddComp(complex x, complex y) { complex z; z.real = x.real + y.real; z.img = x.img + y.img; return z; } realimg x realimg y realimg z

AddComplex – step by step complex a, b, c; printf(…"); scanf("%lf%lf",&(a.real),&(a.img)); printf(…"); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(result = %g+%gi\n",c.real,c.img); return 0; realimg a realimg b realimg c

AddComplex – step by step complex a, b, c; printf(…"); scanf("%lf%lf",&(a.real),&(a.img)); printf(…"); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(result = %g+%gi\n",c.real,c.img); return 0; realimg a realimg b realimg c

Exercise Implement the MultComplex function – Input - two complex numbers Output – their multiplication Note - If x=a+ib and y=c+id then: z = xy = (ac-bd)+i(ad+bc) Write a program that uses the above function to multiply two complex numbers given by the user

Solution MultiplyComplex.c

Miscellaneous structure trivia Structure members may be ordinary variable types, but also other structures and even arrays! Structures can therefore be rather large and take up a lot of space Many times we prefer to pass structures to functions by address, and not by value Thus a new copy of the structure is not created – just a pointer to the existing structure

More trivia Structures cannot be compared using the == operator They must be compared member by member Usually this will be done in a separate function Structures can be copied using the = operator Member-wise copy

Example Is_In_Circle.c

Is_in_circle – step by step printf(Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(&d, &c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); …… yx d (dot)c (circle) …… yx center (dot) radius …

Is_in_circle – step by step printf(Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(&d, &c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); …… yx d (dot)c (circle) …… yx center (dot) radius …

Is_in_circle – step by step printf(Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(&d, &c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); yx d (dot)c (circle) …… yx center (dot) radius …

Is_in_circle – step by step printf(Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(&d, &c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); yx d (dot)c (circle) …… yx center (dot) radius …

Is_in_circle – step by step printf(Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(&d, &c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); yx d (dot)c (circle) 0.0 yx center (dot) radius …

Is_in_circle – step by step printf(Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(&d, &c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); yx d (dot)c (circle) 0.0 yx center (dot) radius …

Is_in_circle – step by step printf(Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(&d, &c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); yx d (dot)c (circle) 0.0 yx center (dot) radius 5

Is_in_circle – step by step printf(Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(&d, &c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); yx d (dot)c (circle) 0.0 yx center (dot) radius 5

Is_in_circle – step by step int IsInCircle(dot *p_dot, circle *p_circle) { double x_dist,y_dist; x_dist = p_dot->x - p_circle->center.x; y_dist = p_dot->y - p_circle->center.y; if (x_dist*x_dist + y_dist*y_dist radius*p_circle->radius) return 1; return 0; } yx (dot)(circle) 0.0 yx center (dot) radius 5 x_disty_dist …… p_circlep_dot

Is_in_circle – step by step int IsInCircle(dot *p_dot, circle *p_circle) { double x_dist,y_dist; x_dist = p_dot->x - p_circle->center.x; y_dist = p_dot->y - p_circle->center.y; if (x_dist*x_dist + y_dist*y_dist radius*p_circle->radius) return 1; return 0; } yx (dot)(circle) 0.0 yx center (dot) radius 5 x_disty_dist 1.0… p_circlep_dot

Is_in_circle – step by step int IsInCircle(dot *p_dot, circle *p_circle) { double x_dist,y_dist; x_dist = p_dot->x - p_circle->center.x; y_dist = p_dot->y - p_circle->center.y; if (x_dist*x_dist + y_dist*y_dist radius*p_circle->radius) return 1; return 0; } yx (dot)(circle) 0.0 yx center (dot) radius 5 x_disty_dist p_circlep_dot

Is_in_circle – step by step int IsInCircle(dot *p_dot, circle *p_circle) { double x_dist,y_dist; x_dist = p_dot->x - p_circle->center.x; y_dist = p_dot->y - p_circle->center.y; if (x_dist*x_dist + y_dist*y_dist radius*p_circle->radius) return 1; return 0; } yx (dot)(circle) 0.0 yx center (dot) radius 5 x_disty_dist p_circlep_dot

Is_in_circle – step by step int IsInCircle(dot *p_dot, circle *p_circle) { double x_dist,y_dist; x_dist = p_dot->x - p_circle->center.x; y_dist = p_dot->y - p_circle->center.y; if (x_dist*x_dist + y_dist*y_dist radius*p_circle->radius) return 1; return 0; } yx (dot)(circle) 0.0 yx center (dot) radius 5 x_disty_dist p_circlep_dot

Is_in_circle – step by step printf(Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(&d, &c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); yx d (dot)c (circle) 0.0 yx center (dot) radius 5

Exercise Write a struct that represents a date (day, month, year) Write a function that increments the date void IncDate(Date *d); For example – >

Solution IncDate.c

Exiting the program void exit(int status); Sometimes an error occurs and we want the program to immediately exit The exit function closes all open files, frees all allocated memory, and exits the program Equivalent to calling return within main Remember to #include See strcpy_with_exit.c

Structures containing arrays A structure member that is an array does not behave like an ordinary array When copying a structure that contains a member which is an array, the array is copied element by element Not just the address gets copied For example - array_member.c Reminder – ordinary arrays cant be copied simply by using the = operator They must be copied using a loop

Structures containing arrays The same happens when passing the structure to a function Changing the array inside the function wont change it in the calling function Reminder – when passing an ordinary array to a function, all that gets passed is the address of its first element Hence every change to the array within the function, changes the array in the calling function

Pointers are another matter If the member is a pointer, for example to a dynamically allocated array, all that gets copied is the pointer (the address) itself For example, pointer_member.c Hence, we should take extra care when manipulating structures that contain pointers