Structured Data Objects (Structs). array Recall our definition of an array: fixedcontiguousall of the same data type an array is a fixed number of contiguous.

Slides:



Advertisements
Similar presentations
Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Advertisements

C Language.
Programming and Data Structure
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.
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.
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.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Kernighan/Ritchie: Kelley/Pohl:
Representing Data Elements Gayatri Gopalakrishnan.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
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 Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Names Variables Type Checking Strong Typing Type Compatibility 1.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
C Tokens Identifiers Keywords Constants Operators Special symbols.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
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.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.
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:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Chapter 3.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
Sql DDL queries CS 260 Database Systems.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Strings Chapter 5.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
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.
Variables and memory addresses
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Structured Data Objects (Structs) Chapter 7.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Chapter 4 C Program Control Part I
Numeric Arrays Numeric Arrays Chapter 4.
Module 2 Arrays and strings – example programs.
DATA HANDLING.
Lecture 18 Arrays and Pointer Arithmetic
DATA TYPES There are four basic data types associated with variables:
SQL (Structured Query Language)
Presentation transcript:

Structured Data Objects (Structs)

array Recall our definition of an array: fixedcontiguousall of the same data type an array is a fixed number of contiguous storage elements all of the same data type. struct The abstract data type struct avoids one restriction: NEED NOT The elements be NEED NOT all be of the same data type. Structs (structured data objects) struct A struct : Consists of 2 or more component data types A component part may be either a simple data type intunsigned intfloatcharlongdouble (e.g., int, unsigned int, float, char, long, double, etc.) A pointer intfloatcharlongdouble (e.g., * int, * float, * char, * long, * double, etc.) Or another structured object struct (i.e., abstract type struct)

struct A struct corresponds to records in a database Assume we maintain a simple database of student information Attribute (Field) Data Type Student NameString (Allow for 30 characters) Student IDInteger (int) MajorString (Allow for 10 characters) Grade-Point Average float Real (float) Hours CompletedInteger (int) student This record (let’s call it student ): Consists of 5 (five) data elements Consists of 3 (three) different data types (Student Name, Student ID, Major, Grade-Point Average, Hours Completed) float (Strings (2), Integers (2), and a real (float))

Declaring Records (in other languages): COBOL: 01STUDENT 02 STUDENT-NAMEPIC X(30). 02 STUDENT-IDPIC 9(6). 02 MAJORPIC X(10). 02 GPAPIC 9V TOTAL-HOURSPIC 999. Pascal : typestudent = record student_name : array[1..30] of char; student_id : integer; major: array[1..10] of char; gpa: real;. total_hours : integer; end; SQL : create table student ( student_name char(30) not null, student_id integer not null, major char(10) default ‘CIS’, gpa decimal(4,2), total_hours smallint, primary key (student_id), check (gpa > 0) )

Declaring Records (in C): struct student { char student_name[31]; // Remember: we need 1-byte for ‘\0’ int student_id; char major[11]; // Remember: we need 1-byte for ‘\0’ float gpa; int total_hrs; }; struct student We have created a new data type: struct student struct student just as with basic data types, we can now associate variables (specific locations in RAM) with the data type struct student struct student just as with basic data types, whenever we declare a variable to be of type struct student, we are requesting a specific number of contiguous bytes of RAM storage. How many bytes of storage do we need for a struct? That depends on the struct

For our struct (struct student): ElementBytes char char student_name[31];31 int int student_id;2 char char major[11];11 float float gpa;4 int int total_hrs; struct student We need 50 bytes of contiguous RAM storage for our data type struct student How do we declare and use structs in a C program ??

The output would appear as:

How is this data stored in RAM?? struct As with ALL data types, the data type struct has a base address: activestruct student assume that variable active (of data type struct student) has the base address 7520 (and requiring 50 contiguous bytes, to 7569) 7520 M 7521 a 7522 n 7523 n 7524 i 7525 n 7526 g 7527, P 7530 e 7531 y 7532 t 7533 o 7534 n 7535 \ P 7554 a 7555 s 7556 s 7557 i 7558 n 7559 g 7560 \

How can the address of each element in the struct be calculated?? struct Just as we calculated the address of each element in an array, the address of each element in a struct is determined by adding the amount of storage required for each element and adding it to the base address. ElementStart addr.StorageNext Address student_name bytes = 7551 student_ID75512-bytes = 7553 major Or: = bytes = 7564 gpa75644-bytes Or: = = 7568 Or: = 7568 total_hrs bytes N/A Type char [31] int char [11] float int

BUT, if structs are equivalent to records in a database, how can a database consist of only one (1) record ??? structint floatchar We can have an array of struct, just as we can have an array of int, and array of float, an array of char (string), etc. struct Remember: a struct, while abstract, is simply a data type struct student { char student_name[20]; // names no longer than 19 characters float gpa; }; For the sake of simplicity, let’s assume the following record structure: How would this struct be set up as an array ???

Structs and pointers Rewrite the previous program so it appears as follows:

The output should be:

Consider the following code: #include struct student { char student_name[20]; float gpa; }; int main() { struct student active[5] = {{"Favre, Brett",3.26}, {"White, Reggie",1.34}, {"Smith, Bruce",3.78}, {"Smith, Emmitt", 2.05}, {"Rice, Jerry", 3.55}}; int i; for (i = 0; i < 5; i++) printf("Name: %15s gpa: %4.2f\n", active[i].student_name,active[i].gpa); return 0; }

The program, as entered, produces the output : Name: Favre, Brett gpa: 3.26 Name: White, Reggie gpa: 1.34 Name: Smith, Bruce gpa: 3.78 Name: Smith, Emmitt gpa: 2.05 Name: Rice, Jerry gpa: 3.55 How does this program work ?? What if we were searching for a specific name, …. Say, Bruce Smith ??? Consider the following C code

#include struct student { char student_name[20]; float gpa; }; int main() { struct student active[5] = {{"Favre, Brett",3.26}, {"White, Reggie",1.34}, {"Smith, Bruce",3.78}, {"Smith, Emmitt",2.05}, {"Rice, Jerry",3.55}}; int i = 0; char find[20] = "Smith, Bruce"; while((strcmp(active[i].student_name,find)!=0)&&(i<5)) i++; if (i >= 5) printf("The name is NOT on the list"); else printf("%s IS on the list (gpa = %4.2f)", active[i].student_name, active[i].gpa); return 0; }

structs structs and pointers structs structs, like any other data type, have base addresses, and can be accessed by referring to its address. Consider the following modification of our previous program: #include struct student { char name[18]; float gpa; }; int main() { struct student active[5] = {{"Favre, Brett",3.26}, {"White, Reggie",1.34}, {"Smith, Bruce",3.78}, {"Smith, Emmitt",2.05}, {"Rice, Jerry",3.55}}; struct student *record = active; printf(“%lu\n”, &record); while(record <= &active[4]) { printf(“%lu %20s %7.3f\n”, record, record->name, record->gpa); record++; { return 0; }

structs structs and pointers structs structs, like any other data type, have base addresses, and can be accessed by referring to its address. Consider the following modification of our previous program: #include struct student { char name[18]; float gpa; }; int main() { struct student active[5] = {{"Favre, Brett",3.26}, {"White, Reggie",1.34}, {"Smith, Bruce",3.78}, {"Smith, Emmitt",2.05}, {"Rice, Jerry",3.55}}; struct student *record = active; printf(“%lu\n”, &record); while(record <= &active[4]) { printf(“%lu %20s %7.3f\n”, record, record->name, record->gpa); record++; { return 0; }

The following output would be produced: Favre, Brett White, Reggie Smith, Bruce Smith, Emmitt Rice, Jerry3.550 Note: These addresses are not the TRUE addresses, and they may change with each run How does this program work ??

Let’s take it from the line: struct student * record = active; Our new data type struct student { char name[18]; float gpa; }; pointer to BASE The BASE address array of our arrayactive &active[0] The same as: &active[0] record active Meaning, the variable pointer record is now initialized to the BASE address of array active ( or, in or case, 86562) How would this appear in RAM??

86562 F a v r e 86567, B r e t t \ W h i t 86589, g i e record active&active[0] The contents of address record ( = active OR &active[0]) Points to R e g \ e Continue to R i c e 86654, J e r r y \ Located at

The next statement in the program: printf(“%lu\n”, &record); Prints the BASE ADDRESS of our structure NOT NOT the contents active (Which contains the base address of our array active) record Notice also that ALL pointers (e.g., record) require 4-bytes of storage We can now begin our loop: while(record <= &active[4]) record record CONTAINS the address Which is less than the base address of our 5th record (86650) Enter Loop

Now we simply print out the first record: printf(“%lu %20s %7.3f\n”, record, record->name, record->gpa); record The CONTENTS of record name Field: name (on 20 spaces) gpa Field: gpa (as f7.3) 86562Favre, Brett3.260 (*record).name An alternative statement is: (*record).name record->name Notice that the statement record->name is a REDIRECT: Go to the ADDRESS contained in record and find field name recordname i.e., record contains the address 86562, and name is at record->gpa The redirect record->gpa would yield the address 86580

The next statement:record++; record Increments the contents of record: Changed to: record The contents of record are increased by 22 Why are the contents increased by 22 and not 1 ??? record --;--record; Record is pointer to our data type struct student (Initialized as: struct student *record;) which requires 22-bytes of storage. Incrementing increases the value contained by 22. Decrementing it (i.e., record --; or --record; would decrease the value contained by 22)

Remember: int *integer_pointer;// contains an address to an integer float *float_pointer;// contains an address to a float double *double_pointer;// contains an address to a double IF:VariableLocated at Contains integer_pointer float_pointer double_pointer A pointer is an address in RAM which contains an address What type of data is at that address MUST be known in advance Then:StatementWill result in the new value integer_pointer++; float_pointer;22738 double_pointer--;71114

For the rest of the loop: record record value Output 86584White, Reggie Smith, Bruce Smith, Emmitt Rice, Jerry Greater than Stop

For the rest of the loop: record record value Output 86584White, Reggie Smith, Bruce Smith, Emmitt Rice, Jerry Greater than Stop

For the rest of the loop: record record value Output 86584White, Reggie Smith, Bruce Smith, Emmitt Rice, Jerry Greater than Stop