1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions.

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
C Language.
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.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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”)
Structures Spring 2013Programming and Data Structure1.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Structs H&K Chapter 11 Instructor – Gokcen Cilingir Cpt S 121 (July 18, 2011) Washington State University.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
CSE1301 Computer Programming: Lecture 33 Linked Lists.
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)
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.
1 CSE1301 Computer Programming Lecture 20 Structures (Part 1)
1 CSE1301 Computer Programming Lecture 24 Structures (Part 2)
1 CSE1301 Computer Programming Lecture 23 Structures (Part 1)
1 CSE1301 Computer Programming Lecture 23 Structures (Part 2)
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 With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters.
Functions, Pointers, Structures Keerthi Nelaturu.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Enumerated Data Type. An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
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
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
 Complex data types  Structures  Defined types  Structures and functions  Structures and pointers.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
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.
CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable.
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
CS1010 Programming Methodology
Visit for more Learning Resources
EECE.2160 ECE Application Programming
Buy book Online -
Array of Structures A structure holds only one record But a record
CSI 121 Structured Programming Language Lecture 24 Structures (Part 2)
Topics discussed in this section:
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
C Programming Lecture-13 Structures
A simple function.
EECE.2160 ECE Application Programming
C++ Parse Analysis Introduction
Structures, Unions, and Enumerations
Presentation transcript:

1 CSE1301: Structures 2 Linda M c Iver

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

3 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

4 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

5 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.

6 Passing structs as parameters Student fred; scanf("%s",fred.name); scanf("%ld",&fred.id); printStudent(fred);... void printStudent(Student s) { printf("Student's name is %s\n",s.name); printf("Student's id is %ld\n",s.id); }

7 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

8 Passing structs by reference Student fred; readStudent(&fred);... void readStudent(Student* s) { printf("Please enter name & ID\n"); scanf("%s",s->name); scanf("%ld",&(s->id)); }

9 Passing structs by reference Student fred; readStudent(&fred);... void readStudent(Student* s) { printf("Please enter name & ID\n"); scanf("%s",s->name); scanf("%ld",&(s->id)); }

10 Passing structs by reference Student fred; readStudent(&fred);... void readStudent(Student* s) { printf("Please enter name & ID\n"); scanf("%s",s->name); scanf("%ld",&(s->id)); }

11 Passing structs by reference So when you pass a struct as a pointer, the operator you use to access a member changes from '. ' to ' -> ' It's an arrow, because your parameter points to the struct Note that you still need the ampersand to get the address of a member of the struct

12 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

13 Array of structs id: name: "fred" id: name: "ralph" id: name: "fong" id: name: "rachel" studentList

14 Array of structs id: name: "fred" id: name: "ralph" id: name: "fong" id: name: "rachel" studentList studentList[0] gives you the whole struct

15 Array of structs id: name: "fred" id: name: "ralph" id: name: "fong" id: name: "rachel" studentList studentList[3].name gives you the struct member

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

17 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

18 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

19 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

20 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

21 Reading Deitel and Deitel - Sections 10.1 to 10.7