CSI 121 Structured Programming Language Lecture 23 Structures (Part 1)

Slides:



Advertisements
Similar presentations
Programming in C Chapter 10 Structures and Unions
Advertisements

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.
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
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 CSE1301 Computer Programming Lecture 22 Structures (Part 1)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A2 – Pointers (Revision)
1 CSE1301 Computer Programming Lecture 22 Structures (Part 1)
1 CSE1301 Computer Programming Lecture 20 Structures (Part 1)
1 CSE1301 Computer Programming Lecture 21 Structures (Part 2)
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.
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).
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
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.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
CS1010 Programming Methodology
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
C Programming Tutorial – Part I
Arrays Declarations CSCI N305
C Language By Sra Sontisirikit
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
DCP2073 Asas Pengaturcaraan C Lecture 6: Components of a C Program (Part 3) Lecture 6: C Primitives 6.
Getting Started with C.
CSI 121 Structure Programming Language Lecture 17 Arrays (Part 1)
Module 2 Arrays and strings – example programs.
Computer science C programming language Lesson 5
Arrays in C.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI-121 Structured Programming Language Lecture 16 Pointers
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Visit for more Learning Resources
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Structures Lesson xx In this module, we’ll introduce you to structures.
C Structures, Unions, and Enumerations
Arrays, For loop While loop Do while loop
CSI 121 Structured Programming Language Lecture 7: Input/Output
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
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
EKT150 : Computer Programming
Declaration, assignment & accessing
Functions, Part 2 of 3 Topics Functions That Return a Value
Govt. Polytechnic,Dhangar
Engineering H192 Winter 2005 Structs and Enumeration Prof. Almas Ansari Dept of Computer Science Engineering Anjuman College of Engineering & Technology..
More Loops Topics Counter-Controlled (Definite) Repetition
Introduction to Computer Programming Lecture 16 Structures (Part 1)
Character Arrays char string1[] = “first”;
More Loops Topics Counter-Controlled (Definite) Repetition
The while Looping Structure
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Functions, Part 2 of 3 Topics Functions That Return a Value
Variables and Constants
Functions, Part 2 of 3 Topics Functions That Return a Value
More Loops Topics Counter-Controlled (Definite) Repetition
Structures, Unions, and Enumerations
Files Chapter 8.
Presentation transcript:

CSI 121 Structured Programming Language Lecture 23 Structures (Part 1)

Topics Structures typedef I/O Structs and functions What are they? What are they good for? typedef I/O Structs and functions

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, the title, artist and price of the CDs in a shop Or name and telephone number Or name, ID number, and mark

Structures 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, with compartments for different things

Structures Like my briefcase, which has compartments for different shaped things Or a cutlery drawer which has different sections for teaspoons, knives, forks and spoons Can you think of more examples?

Structures In C, a structure is known as a struct It contains a fixed number of parts, which may be of different types So for a friend, you may want to store name, phone number and the street they live in

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

Declaring structures 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

gives you access to the value of sarah's name Accessing structures To access a member of a structure, you use the '.' operator, like this: struct friendStr sarah; sarah.name sarah.phoneNumber sarah.street gives you access to the value of sarah's name

Initialising structures struct friendStr { char name[MAXNAME]; long phoneNumber; char street[MAXSTREET]; }; struct friendStr sarah; scanf("%s",sarah.name); scanf("%ld",&sarah.phoneNumber); scanf("%s",sarah.street);

Accessing structures 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);

Accessing structures 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 = 55559999; strcpy(sarah.name,"Sarah Finch"); strcpy(sarah.street,"Firthsmith St");

Accessing structures If you want to declare a lot of structs, using "struct name" all the time is awkward: struct friendStr sarah; struct friendStr tony; struct friendStr quinn; struct friendStr gunalwan; struct friendStr fong;

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

typedef Now we can use friend everywhere we used to use struct friendStr typedef struct friendStr friend; friend sarah; friend tony; friend quinn; friend gunalwan; friend fong;

typedef 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."

typedef The other way to use typedef is shorter, like this: typedef struct { char name[MAXNAME]; long phoneNumber; char street[MAXSTREET]; }friend ;

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

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”); }

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

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

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

Example with typedef (testing) #include <stdio.h> #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; }

Example with typedef-1 #include <stdio.h> #include <stdlib.h> #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);

Example with typedef-2 { printf("Not enough space.\n"); exit(1); } 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"); printf("Last name: %s\n", class[i].lastname); printf(" Mark: %.1f\n\n", class[i].mark); return 0;

Reading to be continued... King Deitel & Deitel Chapter 10 10.1 – 10.7 16.1, 16.2 Deitel & Deitel Chapter 10 10.1 – 10.7 Kernighan & Ritchie 6.1, 6.7 to be continued...