Structures vol2.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Structures Spring 2013Programming and Data Structure1.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
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.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
How to design and code functions Chapter 4 (ctd).
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Functions. What is a function? It’s a group of statements that has a specific purpose and can be be repeatedly executed as needed. By using functions:
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
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.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
PROGRAMMING II( Files+Structures) Tallinn 2016 Vladimir Viies, Lembit Jürimägi, Margit Aarna
Introduction We will study how to broaden the modeling facilities of C by defining our own data types that represent structured collections of data pertaining.
PROGRAMMING II( Dynamic Memory II)
PROGRAMMING II( Dynamic Memory I)
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
UNIT 5 C Pointers.
Functions and Pointers
CS1010 Programming Methodology
Functions, Part 2 of 2 Topics Functions That Return a Value
C Programming Tutorial – Part I
Functions Dr. Sajib Datta
Pointers.
Structure.
Hassan Khosravi / Geoffrey Tien
Module 2 Arrays and strings – example programs.
Arrays in C.
EECE.2160 ECE Application Programming
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Pointers.
Functions and Pointers
INC 161 , CPE 100 Computer Programming
Computer Science 210 Computer Organization
Using Arrays in C Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning.
Pointers  Week 10.
הגדרת משתנים יום שלישי 27 נובמבר 2018
EKT150 : Computer Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
EECE.2160 ECE Application Programming
Qsort.
Revision.
Pointers.
EECE.2160 ECE Application Programming
Functions.
EECE.2160 ECE Application Programming
Functions continued.
EECE.2160 ECE Application Programming
Arrays.
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
Structures Structured Data types Data abstraction structs ---
EECE.2160 ECE Application Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
FUNCTION ||.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
Structures, Unions, and Enumerations
Files Chapter 8.
Presentation transcript:

Structures vol2

Initializing a structure Initializing, when the data is passed in original order of the declaration product item = {4.00, "Cucumber"}; You can specify the field and rearrange the order product item = {.name = "Cabbage", .price = 0.25}; Initializing multiple structures product items[] = { {0.75, "Banana"}, {1.90, "Tomato"} }; 2018 Risto Heinsar

Sample 1 1 #include <stdio.h> 2 3 typedef struct product 4 { 5 float price; 6 char name[30]; 7 } product; 8 9 int main(void) 10 { 11 product item = {.price = 0.25, .name = "Cabbage"}; 12 product items[] = {{0.75, "Noodles"}, {1.90, "Tomato"}}; 13 printf("%.2f eur - %s\n", item.price, item.name); 14 printf("%.2f eur - %s\n", items[0].price, items[0].name); 15 printf("%.2f eur - %s\n", items[1].price, items[1].name); 16 return 0; 17 } 2018 Risto Heinsar

Nested structures You can put one structure inside of another The order they’re defined is important Point is still used as the separator between the name and the field typedef struct date { unsigned day; unsigned month; unsigned year; } date;   typedef struct person char fName[25]; char lName[25]; date contractSigned; } person; 2018 Risto Heinsar

Sample 2 typedef struct date { unsigned day; unsigned month; int main(void) { person employee; strcpy(employee.fName, "Andrus"); strcpy(employee.lName, "Haab"); employee.contractSigned.year = 1985; employee.contractSigned.month = 4; employee.contractSigned.day = 14; printf("%s %s - %u.%u.%u", employee.fName, employee.lName, employee.coontractSigned.day, employee.contractSigned.month, employee.contractSigned.year); return 0; } typedef struct date { unsigned day; unsigned month; unsigned year; } date;   typedef struct person char fName[25]; char lName[25]; date contractSigned; } person; 2018 Risto Heinsar

Function returning a structure Reminder: structure was a data type, albeit a complex one. This means that we can also use it as a return type for a function. We can return one whole structure per function call. We could also return a structure pointer as we’ll see in a few weeks Let’s create a new type for our structure for easier use A prototype for our function The function itself typedef struct point { int x, y; } point; point EnterCoords(); point EnterCoords() { point temporary; temporary.x = 5; temporary.y = 7; return temporary; } 2018 Risto Heinsar

Sample 3 #include <stdio.h> #include <math.h> typedef struct point { int x, y; } point; point EnterCoords(int nr); int main(void) { point segment[2]; segment[0] = EnterCoords(1); segment[1] = EnterCoords(2); printf("The length is %.2f", sqrt(pow(segment[1].x - segment[0].x, 2) + pow(segment[1].y - segment[0].y, 2))); return 0; } point EnterCoords(int nr) { point temporary; printf("Enter the x and y coordinates for point %d\n> ", nr); scanf("%d %d", &temporary.x, &temporary.y); return temporary; 2018 Risto Heinsar

Pointer operations int *p // declare a pointer variable &var // get the location of the variable (memory address) *p // dereferencing a pointer p = &var // assigning the address of var to pointer p *p = 55 // assign a value by dereferencing a pointer printf("%p", p) // printing out the address stored in var p printf("%d", *p) // printing out the value that p points to 2018 Risto Heinsar

Pointers visualized 0x0199FF8A 25 *p num #include <stdio.h>   int main(void) { int num = 25; int *p; p = &num; return 0; } *p num 0x0199FF8A 25 0x0199FF86 0x0199FF8A 2018 Risto Heinsar

Pointers and arrays visualized *p 48F9AC91 int array[] = {5, 3, 7, 3, 5}; int *p; p = array; array[0] 5 array[1] 3 array[2] 7 array[3] 3 array[4] 5 *(p + 0) *(p + 1) *(p + 2) *(p + 3) *(p + 4) 2018 Risto Heinsar

Pointers and structures You can point (refer) to the start of a structure or an element inside of it. struct employee { int employeeCode; char firstName[25]; char lastName[25]; float wage; }; struct employee manager; struct employee *pStr; pStr = &employee; 2018 Risto Heinsar

Accessing members using pointers Both of these are equal! (*pStr).employeeCode; (*pStr).fName; (*pStr).lName; (*pStr).wage; pStr->employeeCode; pStr->fName; pStr->lName; pStr->wage; However some are more equal than others! 2018 Risto Heinsar

Accessing members using pointers Both of these are equal! (*(pStr + i)).employeeCode; (*(pStr + i)).fName; (*(pStr + i)).lName; (*(pStr + i)).wage; (pStr + i)->employeeCode; (pStr + i)->fName; (pStr + i)->lName; (pStr + i)->wage; However some are more equal than others! 2018 Risto Heinsar

Sample 4 #include <stdio.h> #include <string.h> typedef struct employee { int employeeCode; char fName[25]; char lName[25]; float wage; } employee; void PrintEmployee(employee *pStr); int main(void) employee manager = {75, "Indrek", "Tamm", 4.75}; PrintEmployee(&manager); return 0; } void PrintEmployee(employee *pStr) printf("Employee %06d, %s %s, earns %2.2f per hour", pStr->employeeCode, pStr->fName, pStr->lName, pStr->wage); 2018 Risto Heinsar

Task Read data from two files into two structure arrays: products – product code, name, average sales per day warehouse – product code, stock Association is done by product code (key) In the basic variant, it’s a 1:1 relation. Sort the products based on alphabetical order. Find the products that are running out of supply (stock is less than the average sales for 3 days) Use structure pointers in at least 1 function 2018 Risto Heinsar

Advanced Expand the task so that there could be multiple stores getting supply from the same central storage. Calculations should include all stores. Use 3 files for this – products, warehouse and store sales. Move sales from products to stores. Products – product code, name Store sales – store name, product code, average sales It must be possible for not all stores to carry all possible items of merchandise Expand on the warehouses. It’s possible that the same merchandise is available at multiple storage locations. Add an expiration date to the storage entries and use it as a part of the condition to decide if inventory should be updated 2018 Risto Heinsar