CSI 121 Structured Programming Language Lecture 24 Structures (Part 2)

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Structures Spring 2013Programming and Data Structure1.
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)
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 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)
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 13 Static vs Dynamic Memory Allocation
Introduction to Programming 3D Applications CE Lecture 12 Structure Pointers and Memory Management in C.
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.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
LINKED LISTS.
CS1010 Programming Methodology
Chapter 10-1: Structure.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
C Programming Tutorial – Part I
CSI 121 Structure Programming Language Lecture 17 Arrays (Part 1)
Visit for more Learning Resources
Module 2 Arrays and strings – example programs.
CSI-121 Structured Programming Language Lecture 16 Pointers
EECE.2160 ECE Application Programming
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Buy book Online -
CSI 121 Structured Programming Language Lecture 23 Structures (Part 1)
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
CSI 121 Structured Programming Language Lecture 7: Input/Output
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
CSI 121 Structured Programming Language Lecture 21 Character Strings
Introduction to Computer Programming Lecture 17 Structures (Part 2)
CSI 121 Structured Programming Language Lecture 5: C Primitives 2
EKT150 : Computer Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
EECE.2160 ECE Application Programming
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Structures, Unions, and Typedefs
Topics discussed in this section:
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
EECE.2160 ECE Application Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Structures CSE 2031 Fall February 2019.
EECE.2160 ECE Application Programming
Structures CSE 2031 Fall April 2019.
Structures CSE 2031 Fall May 2019.
Introduction to Computer Programming Lecture 16 Structures (Part 1)
ETE 132 Lecture 6 By Munirul Haque.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
EECE.2160 ECE Application Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
EECE.2160 ECE Application Programming
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 2 of 3 Topics Functions That Return a Value
Structures, Unions, and Enumerations
Presentation transcript:

CSI 121 Structured Programming Language Lecture 24 Structures (Part 2) CSE1301 Semester 2 - 2003 CSI 121 Structured Programming Language Lecture 24 Structures (Part 2) Lecture 24 - Structures 2

Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions arrays of structures

Structs - revision collection of members of different types good for storing related values to refer to a member of a struct you need the struct name and the member name, joined by a '.' : eg. studentA.name, studentA.id

Structs - revision (cont) structs can contain members of any type (basic types, arrays, other structs, pointers, etc) A struct declaration declares a type to use it, you need to declare a variable of that type Can use typedef to rename the struct type Can't compare structs directly, can only compare members

Passing structs as parameters Like any other variable, you can pass a struct as a parameter to a function First we'll look at passing by value Pass the struct in, use the values of the members, but don't change them.

Passing a struct to a Function CSE1301 Semester 2 - 2003 Passing a struct to a Function As always, the formal parameters are copies of the actual parameters void printRecord ( Student item ) { printf("Last name: %s\n", item.lastname); printf(" Mark: %.1f\n\n", item.mark); } main() { Student studentA = {“Gauss”, 99.0}; printRecord(studentA); } Lecture 24 - Structures 2

Function Returning a struct A “package” containing several values Student readRecord ( void ) { Student newStudent; printf("Enter last name and mark: "); scanf("%s %f",newStudent.lastname,&(newStudent.mark)); return newStudent; } main() { Student studentA; studentA = readRecord(); }

Example: Structs and Functions-1 #include <stdio.h> #include <stdlib.h> #define MAXLEN 50 #define MAXN 20 struct StudentRec { char lastname[MAXLEN]; float mark; }; typedef struct StudentRec Student; Student readRecord ( void ) Student newStudent; printf("Enter last name and mark: "); scanf("%s %f", newStudent.lastname, &(newStudent.mark)); return newStudent; } void printRecord ( Student item ) printf("Last name: %s\n", item.lastname); printf(" Mark: %.1f\n\n", item.mark);

Example: Structs and Functions-2 int main() { int count = 0; Student class[MAXN]; int i; printf("How many students? "); scanf("%d", &count); if (count > MAXN) printf("Not enough space.\n"); exit(1); } for (i=0; i < count; i++) class[i] = readRecord(); printf("\nClass list:\n\n"); printRecord(class[i]); return 0;

Passing structs as parameters You can also pass structs by reference Pass the struct in, change the value of some or all of the members, changes are visible in the calling function as well as the called function. This time you're passing a pointer to a struct

Passing structs by reference void readStudent ( Student* item ) { printf(“Please enter name and ID\n”); scanf(“%s”, (*s).name) /*parens needed*/ scanf(“%ld”, &((*s).id) ); /* Yes! */ } int main() { Student studentA; readStudent(&studentA); }

Passing structs by reference The parentheses around the (*s) are needed because of precedence We frequently have (*sptr).item If you need the address of an item referred to this way, you need another set of parentheses We frequently have &((*sptr).item)

Arrays of structs You can have an array of structs Each element of the array is a whole struct, with all the members of that struct. So to access a single value, you need to know which element of the array you're dealing with, and which member of the struct: studentList[0].name studentList[i].id

Array of structs studentList id:123456789 name: "fred" id:123456788 1 name: "ralph" id: 123456787 name: "fong" id: 123456786 name: "rachel" 1 2 3

studentList[0]gives you the whole struct Array of structs studentList[0]gives you the whole struct studentList id:123456789 name: "fred" id:123456788 name: "ralph" id: 123456787 name: "fong" id: 123456786 name: "rachel" 1 2 3

studentList[3].name gives you the struct member Array of structs studentList id:123456789 name: "fred" id:123456788 name: "ralph" id: 123456787 name: "fong" id: 123456786 name: "rachel" 1 2 3 studentList[3].name gives you the struct member

Arrays of structs typedef struct { long int id; char name[20]; } Student; ... Student sem2Class[150];

Arrays of structs Student sem2Class[MAXCLASS]; int i; for (i=0;i<MAXCLASS;i++) { printf("enter name\n"); scanf("%s",sem2Class[i].name); printf("enter id\n"); scanf("%d",&(sem2Class[i].id)); } name of array

Arrays of structs Student sem2Class[MAXCLASS]; int i; for (i=0;i<MAXCLASS;i++) { printf("enter name\n"); scanf("%s",sem2Class[i].name); printf("enter id\n"); scanf("%d",&(sem2Class[i].id)); } index into array

the structure at this position in the array Arrays of structs Student sem2Class[MAXCLASS]; int i; for (i=0;i<MAXCLASS;i++) { printf("enter name\n"); scanf("%s",sem2Class[i].name); printf("enter id\n"); scanf("%d",&(sem2Class[i].id)); } the structure at this position in the array

name of the member of the structure at that position in the array Arrays of structs Student sem2Class[MAXCLASS]; int i; for (i=0;i<MAXCLASS;i++) { printf("enter name\n"); scanf("%s",sem2Class[i].name); printf("enter id\n"); scanf("%d",&(sem2Class[i].id)); } name of the member of the structure at that position in the array

Using arrays and pointers Some struct: s[13] An item in some struct: s[4].age A character in some name: s[7].name[3] Address of some struct: &(s[2]) An item in some struct pointed to: (*sptr).age Address of above: &((*sptr).age) This is an address within a struct We won’t use a subscript on a pointer (Whew!)

Reading King Deitel and Deitel Section 16.3 Section 10.1 to 10.7 CSE1301 Semester 2 - 2003 Reading King Section 16.3 Deitel and Deitel Section 10.1 to 10.7 Kernighan & Ritchie Section 6.2, 6.4 Lecture 24 - Structures 2