1 CSE1301 Computer Programming Lecture 22 Structures (Part 1)

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

Programming in C Chapter 10 Structures and Unions
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
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.
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.
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
1 CSE1301 Computer Programming Lecture 5 C Primitives 2.
CSE1301 Computer Programming Lecture 4: C Primitives I.
CSE1303 Part A Data Structures and Algorithms Lecture A6 – Dynamic Memory.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A6 – Dynamic Memory.
1 Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A1 – Welcome & Revision.
1 CSE1301 Computer Programming Lecture 6 C Primitives 3.
Topic 2 Pointers CSE1303 Part A, Summer Semester,2002 Data Structures and Algorithms.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A2 – Pointers (Revision)
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
1 CSE1301 Computer Programming Lecture 22 Structures (Part 1)
1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions.
1 CSE1301 Computer Programming Lecture 20 Structures (Part 1)
1 CSE1301 Computer Programming Lecture 21 Structures (Part 2)
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
1 CSE1301 Computer Programming Lecture 24 Structures (Part 2)
1 CSE1301 Computer Programming Lecture 23 Structures (Part 1)
1 CSE1301 Computer Programming: Lecture 19 Character Strings.
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
1 CSE1301 Computer Programming Lecture 18 Arrays (Part 1)
1 CSE1301 Computer Programming Lecture 23 Structures (Part 2)
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
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.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
1 Data Structures and Algorithms Programs. Different problems. Operations. Size of data. Resources available.
Kymberly Fergusson WELCOME CSE1303 Part A Data Structures and Algorithms Summer 2002.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
CPS120: Introduction to Computer Science Data Structures.
1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
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()
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
CS1010 Programming Methodology
C Programming Tutorial – Part I
DCP2073 Asas Pengaturcaraan C Lecture 6: Components of a C Program (Part 3) Lecture 6: C Primitives 6.
Module 2 Arrays and strings – example programs.
CSI 121 Structured Programming Language Lecture 23 Structures (Part 1)
Structures Lesson xx In this module, we’ll introduce you to structures.
C Structures, Unions, and Enumerations
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.
Introduction to Computer Programming Lecture 17 Structures (Part 2)
CSI 121 Structured Programming Language Lecture 24 Structures (Part 2)
CSI 121 Structured Programming Language Lecture 5: C Primitives 2
Introduction to Computer Programming Lecture 16 Structures (Part 1)
Structures, Unions, and Enumerations
Presentation transcript:

1 CSE1301 Computer Programming Lecture 22 Structures (Part 1)

2 Topics Structures –What are they? –What are they good for? typedef I/O Structs and functions (lect 23)

3 Structures Arrays hold many elements of the same type What happens if we want to keep a few things together that are of different types? For example, –title, artist and price of the CDs in a shop –name and telephone number –name, ID number, and mark

4 Structures (cont) For a limited number of elements –of varying types –which need to be kept together, in one place We can use a structure –Like a box –Or a briefcase –Or a cutlery drawer

5 Structures (cont) In C, a structure is known as a struct It contains a fixed number of parts, which may be of different types Example: –for a friend, you may want to store name, phone number and the street where they live

6 Declaring Structures struct friendStr { char name[MAXNAME]; long int phoneNumber; char street[MAXSTREET]; } ; Every struct needs a name Parts of the struct are known as members This declares a type of structure, but it does not create a variable

7 Common Mistake struct StudentRec { char lastname[MAXLEN]; float mark; } ; Do not forget the semicolon here!

8 Declaring Structures (cont) To create a structure in computer memory, you need to declare a structure variable, like this: struct friendStr sarah; name of the type name of the variable

9 Accessing Structures To access a member of a structure, use the '. ' operator gives you access to the value of sarah's name struct friendStr { char name[MAXNAME]; long int phoneNumber; char street[MAXSTREET]; }; struct friendStr sarah; sarah.name sarah.phoneNumber sarah.street

10 Accessing Structures (cont) A member of a structure is just like any other variable –If it's a string, it's just an ordinary string –If it's an int, it's just an ordinary int EXCEPT that you access them using the name of the struct variable, AND the name of the member: –sarah.phoneNumber = ; –strcpy(sarah.name,"Sarah Finch"); –strcpy(sarah.street,"Firthsmith St");

11 Accessing Structures (cont) struct friendStr sarah; scanf("%s",sarah.name); scanf("%ld",&sarah.phoneNumber); scanf("%s",sarah.street); printf("Name is %s\n",sarah.name); printf("Phone is %d\n",sarah.phoneNumber); printf("Street is %s\n",sarah.street);

12 struct friendStr { char name[MAXNAME]; long int phoneNumber; char street[MAXSTREET]; }; Initializing Structures struct friendStr sarah; sarah = {“SarahFinch”, , “Firthsmith Street”); struct friendStr sarah = {“SarahFinch”, , “Firthsmith Street”); struct friendStr sarah = {“SarahFinch”, , “Firthsmith Street”); struct friendStr susan; susan = sarah;

13 Example with struct (testing) #include #define MAXLEN 50 struct StudentRec { char lastname[MAXLEN]; float mark; }; int main() { struct StudentRec studA; struct StudentRec studB; printf("Enter last name and mark for student A: "); scanf("%s %f", studA.lastname, &(studA.mark)); printf("Enter last name and mark for student B: "); scanf("%s %f", studB.lastname, &(studB.mark)); printf("Student A: %s\t%f\n", studA.lastname, studA.mark); printf("Student B: %s\t%f\n", studB.lastname, studB.mark); return 0; }

14 Declaring Structures (cont) Declaring a lot of structs using " struct name " is awkward: struct friendStr sarah; struct friendStr tony; struct friendStr quinn; struct friendStr gunalwan; struct friendStr fong;

15 typedef Instead, we can give the struct type a shorter name, like this: struct friendStr { char name[MAXNAME]; long int phoneNumber; char street[MAXSTREET]; }; typedef struct friendStr friend;

16 typedef (cont) Now we can use friend everywhere we used struct friendStr typedef struct friendStr friend; friend sarah; friend tony; friend quinn; friend gunalwan; friend fong;

17 typedef (cont) All we have done is told the compiler: "every time you see friend, I really mean struct friendStr " In the same way we use symbolic constant declarations like " #define SIZE 20 " to tell the compiler: " every time you see SIZE, I really mean 20 "

18 typedef (cont) A shorter way to use typedef is typedef struct { char name[MAXNAME]; long int phoneNumber; char street[MAXSTREET]; } friend;

19 Notes on structs struct variables cannot be compared We can perform member comparisons only if (studA == studB) { printf(“Duplicate data.\n”); } if (strcmp(studA.lastname, studB.lastname) == 0 && (studA.mark == studB.mark) ) { printf(“Duplicate data.\n”); }

20 Notes on structs (cont) struct StudentRec { char lastname[MAXLEN]; float mark; }; struct StudentRec studA, studB, class[MAXN]; We can define a struct, and declare instances of that struct

21 typedef A typedef statement makes an identifier equivalent to a type specification struct StudentRec { char lastname[MAXLEN]; float mark; }; struct StudentRec studentA; struct StudentRec class[MAXN]; Example without typedef

22 struct StudentRec { char lastname[MAXLEN]; float mark; }; typedef struct StudentRec Student; Student studA; Student class[MAXN]; Example with typedef typedef (cont) The typedef statement makes an identifier equivalent to a type specification

23 Example with typedef (testing) #include #define MAXLEN 50 struct StudentRec { char lastname[MAXLEN]; float mark; }; typedef struct StudentRec Student; int main() { Student studA; Student studB; printf("Enter last name and mark for student A: "); scanf("%s %f", studA.lastname, &(studA.mark)); printf("Enter last name and mark for student B: "); scanf("%s %f", studB.lastname, &(studB.mark)); printf("Student A: %s\t%f\n", studA.lastname, studA.mark); printf("Student B: %s\t%f\n", studB.lastname, studB.mark); return 0; }

24 Example with typedef -1 #include #define MAXLEN 50 #define MAXN 20 struct StudentRec { char lastname[MAXLEN]; float mark; }; typedef struct StudentRec Student; int main() { int count = 0; Student class[MAXN]; int i; printf("How many students? "); scanf("%d", &count);

25 if (count > MAXN) { printf("Not enough space.\n"); exit(1); } for (i=0; i < count; i++) { printf("Enter last name and mark: "); scanf("%s %f", class[i].lastname, &(class[i].mark) ); } printf("\nClass list:\n\n"); for (i=0; i < count; i++) { printf("Last name: %s\n", class[i].lastname); printf(" Mark: %.1f\n\n", class[i].mark); } return 0; } Example with typedef -2

26 Reading King – 16.1, 16.2 Deitel & Deitel – Chapter 10, 10.1 – 10.7 Kernighan & Ritchie – 6.1, 6.7