Structures A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.

Slides:



Advertisements
Similar presentations
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
Advertisements

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 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
Structures Spring 2013Programming and Data Structure1.
1 6.1 Basics of Structures 6.2 Structures and Functions 6.3 Arrays of Structures 6.4 Pointers to Structures 6.5 Self-referential Structures + malloc +
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.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
CENG7071 Review of C Programming Language. CENG7072 Structure of a C Program /* File: powertab.c * * This program generates a table comparing.
C and Data Structures Baojian Hua
Heterogeneous Structures 4 Collection of values of possibly differing types 4 Name the collection; name the components 4 Example: student record harveyC.
C language issues CSC 172 SPRING 2002 EXTRA LECTURE.
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.
Programming in C Pointer Basics.
8. Structures, File I/O, Recursion 18 th October IIT Kanpur 1C Course, Programming club, Fall 2008.
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.
Structures ANSI-C. Review: Data Structures Functions give us a way to organize programs. Data structures are needed to organize data, especially: –1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Learners Support Publications Classes and Objects.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
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.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
1 6.1 Basics of Structures 6.2 Structures and Functions 6.3 Arrays of Structures 6.4 Pointers to Structures 6.5 Self-referential Structures + Linked List.
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.
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.
Homework Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
1 Homework / Exam Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next Continue HW5.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
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.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
CE-2810 Dr. Mark L. Hornick 1 “Classes” in C. CS-280 Dr. Mark L. Hornick 2 A struct is a complex datatype that can consist of Primitive datatypes Ints,
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
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)
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Review of C Programming Language Reference: tutorialspoint
C Structures and Memory Allocation
Homework / Exam Continuing K&R Chapter 6 Exam 2 after next class
Lecture 9 Structure 1. Concepts of structure Pointers of structures
C Structures, Unions, and Enumerations
Programming in C Pointer Basics.
CS1100 Computational Engineering
Classes and Objects.
Programming in C Pointer Basics.
CS111 Computer Programming
Homework Finishing K&R Chapter 5 today Starting K&R Chapter 6 next
Structures CSE 2031 Fall February 2019.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Structures.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
A simple function.
Structures CSE 2031 Fall April 2019.
Structures CSE 2031 Fall May 2019.
CS1100 Computational Engineering
Structures EECS July 2019.
C Structures and Memory Allocation
EECE.2160 ECE Application Programming
Presentation transcript:

Structures A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. BBS514 Structured Programming (Yapısal Programlama)1

Structure Definitions BBS514 Structured Programming (Yapısal Programlama)2 Example: struct point { int x; int y; }; struct point pt; /* defines a variable pt which is a structure of type struct point */ pt.x = 15; pt.y = 30; printf(“%d, %d”, pt.x, pt.y);

Structure Definitions BBS514 Structured Programming (Yapısal Programlama)3 /* Structures can be nested. One representation of a rectangle is a pair of points that denote the diagonally opposite corners. */ struct rect { struct point pt1; struct point pt2; }; struct rect screen; /* Print the pt1 field of screen */ printf(“%d, %d”,screen.pt1.x,screen.pt1.y); /* Print the pt2 field of screen */ printf(“%d, %d”,screen.pt2.x,screen.pt2.y);

typedef BBS514 Structured Programming (Yapısal Programlama)4 typedef –Creates synonyms (aliases) for previously defined data types –Use typedef to create shorter type names –Example: typedef struct point pixel; –Defines a new type name pixel as a synonym for type struct point –typedef does not create a new data type Only creates an alias

Using Structures With Functions BBS514 Structured Programming (Yapısal Programlama)5 /* Demonstrates passing a structure to a function */ #include struct data{ float amount; char fname[30]; char lname[30]; }rec; void printRecord(struct data x); int main(void) { printf(“Enter the donor’s first and last names\n”); printf(“separated by a space: ”); scanf(“%s %s”,rec.fname, rec.lname); printf(“Enter the donation amount: ”); scanf(“%lf”,&rec.amount); printRecord(rec); return 0; }

BBS514 Structured Programming (Yapısal Programlama)6 void printRecord(struct data x) { printf(“\nDonor %s %s gave $%.2f.”, x.fname, x.lname, x.amount); }

/* Make a point from x and y components. */ struct point makepoint (int x, int y) { struct point temp; temp.x = x; temp.y = y; return (temp); } /* makepoint can now be used to initialize a structure */ struct rect screen; struct point middle; screen.pt1 = makepoint(0,0); screen.pt2 = makepoint(50,100); middle = makepoint((screen.pt1.x + screen.pt2.x)/2, (screen.pt1.y + screen.pt2.y)/2); BBS514 Structured Programming (Yapısal Programlama)7

/* add two points */ struct point addpoint (struct point p1, struct point p2) { p1.x += p2.x; p1.y += p2.y; return p1; } Both arguments and the return value are structures in the function addpoint. BBS514 Structured Programming (Yapısal Programlama)8

Structures and Pointers BBS514 Structured Programming (Yapısal Programlama)9 struct point *p; /* p is a pointer to a structure of type struct point */ struct point origin; p = &origin; printf(“Origin is (%d, %d)\n”, (*p).x, (*p).y); Parenthesis are necessary in (*p).x because the precedence of the structure member operator (dot) is higher than *. The expression *p.x ≡ *(p.x) which is illegal because x is not a pointer.

Structures and Pointers BBS514 Structured Programming (Yapısal Programlama)10 Pointers to structures are so frequently used that an alternative is provided as a shorthand. If p is a pointer to a structure, then p -> field_of_structure refers to a particular field. We could write printf(“Origin is (%d %d)\n”, p->x, p->y);

Structures and Pointers BBS514 Structured Programming (Yapısal Programlama)11 Both. and -> associate from left to right Consider struct rect r, *rp = &r; The following 4 expressions are equivalent. r.pt1.x rp -> pt1.x (r.pt1).x (rp->pt1).x

Declarations and Assignments struct student { char *last_name; int student_id; char grade; }; struct student temp, *p = &temp; temp.grade = ‘A’; temp.last_name = “Casanova”; temp.student_id = ; Expression Equiv. Expression Value temp.grade p -> grade A temp.last_name p -> last_name Casanova temp.student_id p -> student_id (*p).student_id p -> student_id BBS514 Structured Programming (Yapısal Programlama)12

Arrays of Structures BBS514 Structured Programming (Yapısal Programlama)13 Usually a program needs to work with more than one instance of data. For example, to maintain a list of phone #s in a program, you can define a structure to hold each person’s name and number. struct entry { char fname[10]; char lname[12]; char phone[8]; }; A phone list has to hold many entries, so a single instance of the entry structure isn’t of much use. What we need is an array of structures of type entry. After the structure has been defined, you can define the array as follows: struct entry list[1000];

BBS514 Structured Programming (Yapısal Programlama)14 struct entry list[1000] list[0] list[1] list[999] list[0].fname list[0].lname list[0].phone list[1].fname list[1].lname list[1].phone list[999].fname list[999].lname list[999].phone list[999].fname[2]

BBS514 Structured Programming (Yapısal Programlama)15 To assign data in one element to another array element, you write list[1] = list[5]; To move data between individual structure fields, you write strcpy(list[1].phone, list[5].phone); To move data between individual elements of structure field arrays, you write list[5].phone[1] = list[2].phone[3];

BBS514 Structured Programming (Yapısal Programlama)16 #define CLASS_SIZE 100 struct student { char *last_name; int student_id; char grade; }; int main(void) { struct student temp, class[CLASS_SIZE];... } int countA(struct student class[]) { int i, cnt = 0; for (i = 0; i < CLASS_SIZE; ++i) cnt += class[i].grade == ‘A’; return cnt; }

BBS514 Structured Programming (Yapısal Programlama)17 Arrays of structures can be very powerful programming tools, as can pointers to structures. struct part { int number; char name [10]; }; struct part data[100]; struct part *p_part; p_part = data; printf(“%d %s”, p_part->number, p_part->name);

BBS514 Structured Programming (Yapısal Programlama)18 The above diagram shows an array named x that consists of 3 elements. The pointer ptr was initialized to point at x[0]. Each time ptr is incremented, it points at the next array element x[0]x[1]x[2] Memory addresses ptr

BBS514 Structured Programming (Yapısal Programlama)19 /* Array of structures */ #include #define MAX 4 struct part { int number; char name[10]; } data[MAX] = {1, “Smith”, 2, “Jones”, 3, “Adams”, 4, “Wilson”}; int main (void) { struct part *p_part; int count; p_part = data; for (count = 0; count < MAX; count++) { printf(“\n %d %s”, p_part -> number, p_part -> name); p_part++; } return 0; }