Week 9 - Monday CS222.

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

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Structures Spring 2013Programming and Data Structure1.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
Learners Support Publications Classes and Objects.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CS 261 – Data Structures Introduction to C Programming.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Week 5 - Monday.  What did we talk about last time?  Processes  Lab 4.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Pointers and Classes.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Week 9 - Monday CS222.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
Lesson #8 Structures Linked Lists Command Line Arguments.
Phil Tayco Slide version 1.0 Created Sep 10, 2017
C Primer.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CS1010 Programming Methodology
TMF1414 Introduction to Programming
EECE.2160 ECE Application Programming
C Structures, Unions, Bit Manipulations and Enumerations
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Dynamic Data, and Reference Types
EECE.2160 ECE Application Programming
Classes and Objects.
Structures, Unions, and Typedefs
Programming in C Pointer Basics.
Pointers.
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
EECE.2160 ECE Application Programming
Java Programming Language
Structures.
Standard Version of Starting Out with C++, 4th Edition
Programming in C Pointer Basics.
Pointers and References
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
The Three Attributes of an Identifier
15213 C Primer 17 September 2002.
Pointers, Dynamic Data, and Reference Types
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Chapter 11 Structure and Union Types.
Week 6 - Monday CS221.
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
Structures, Unions, and Enumerations
Week 7 - Monday CS 121.
Week 5 - Wednesday CS222.
Presentation transcript:

Week 9 - Monday CS222

Last time What did we talk about last time? Users and groups Time Lab 8

Questions?

Project 4

Quotes On two occasions I have been asked: "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. Charles Babbage

Structs

Structs A struct in C is: They were called records in Pascal A collection of one or more variables Possibly of different types Grouped together for convenient handling. They were called records in Pascal They have similarities to classes in Java Except all fields are public and there are no methods Struct declarations are usually global They are outside of main() and often in header files

struct name { type1 member1; type2 member2; type3 member3; ... }; Anatomy of a struct struct name { type1 member1; type2 member2; type3 member3; ... };

Why should we bother? Some data is naturally grouped together For example, a roster of students where each student has a name, GPA, ID number You could keep an array of strings, double values, and int values that corresponded to each other But then sorting by GPA would mean moving values in three different arrays Also, we'll need structs for linked lists and trees

Java examples In Java, a struct-like class would be used to group some data conveniently Examples: A class to hold a point in space A class to hold student data public class Point { private double x; private double y; //constructor //methods } public class Student { private String name; private double GPA; private int ID; //constructor //methods }

C examples The C equivalents are similar Just remember to put a semicolon after the struct declaration A string can either be a char* (the memory for it is allocated elsewhere) or a char array with a maximum size Examples: A struct to hold a point in space A struct to hold student data struct point { double x; double y; }; struct student { char name[100]; double GPA; int ID; };

Declaring a struct variable Type: struct The name of the struct The name of the identifier You have to put struct first! struct student bob; struct student jameel; struct point start; struct point end;

Accessing members of a struct Once you have a struct variable, you can access its members with dot notation (variable.member) Members can be read and written struct student bob; strcpy(bob.name, "Bob Blobberwob"); bob.GPA = 3.7; bob.ID = 100008; printf("Bob's GPA: %f\n", bob.GPA);

Initializing structs There are no constructors for structs in C You can initialize each element manually: Or you can use braces to initialize the entire struct at once: struct student julio; strcpy(julio.name, "Julio Iglesias"); julio.GPA = 3.9; julio.ID = 100009; struct student julio = { "Julio Iglesias", 3.9, 100009 };

Assigning structs It is possible to assign one struct to another Doing so is equivalent to using memcpy() to copy the memory of julio into the memory of bob bob is still separate memory: it's not like copying references in Java struct student julio; struct student bob; strcpy(julio.name, "Julio Iglesias"); julio.GPA = 3.9; julio.ID = 100009; bob = julio;

Putting arrays and pointers in structs It is perfectly legal to put arrays of values, pointers, and even other struct variables inside of a struct declaration If it's a pointer, you will have to point it to valid memory yourself struct point { double x; double y; }; struct triangle struct point vertices[3];

Dangers with pointers in structs With a pointer in a struct, copying the struct will copy the pointer but will not make a copy of the contents Changing one struct could change another struct person { char* firstName; char* lastName; }; struct person bob1; struct person bob2; bob1.firstName = strdup("Bob"); bob1.lastName = strdup("Newhart"); bob2 = bob1; strcpy(bob2.lastName, "Hope"); printf("Name: %s %s\n", bob1.firstName, bob1.lastName); //prints Bob Hope

Using arrays of structs An array of structs is common Student roster List of points Like any other array, you put the name of the type (struct name) before the variable, followed by brackets with a fixed size An array of structs is filled with uninitialized structs whose members are garbage struct student students[100];

Pointers to structs Similarly, we can define a pointer to a struct variable We can point it at an existing struct We can dynamically allocate a struct to point it at This is how linked lists are going to work struct student bob; struct student* studentPointer; strcpy(bob.name, "Bob Blobberwob"); bob.GPA = 3.7; bob.ID = 100008; studentPointer = &bob; (*studentPointer).GPA = 2.8; studentPointer = (struct student*) malloc(sizeof(struct student));

Arrow notation As we saw on the previous slide, we have to dereference a struct pointer and then use the dot to access a member This is cumbersome and requires parentheses Because this is a frequent operation, dereference + dot can be written as an arrow (->) struct student* studentPointer = (struct student*) malloc(sizeof(struct student)); (*studentPointer).ID = 3030; studentPointer->ID = 3030;

Passing structs to functions If you pass a struct directly to a function, you are passing it by value A copy of its contents is made It is common to pass a struct by pointer to avoid copying and so that its members can be changed void flip(struct point* value) { double temp = value->x; value->x = value->y; value->y = temp; }

Gotchas Always put a semicolon at the end of a struct declaration Don't put constructors or methods inside of a struct C doesn't have them Assigning one struct to another copies the memory of one into the other Pointers to struct variables are usually passed into functions Both for efficiency and so that you can change the data inside

Example Write a function that takes two point structs and returns the distance between them struct point { double x; double y; };

Example Read in 100 student names, GPAs, and ID numbers Sort them by ID numbers Print out the values struct student { char name[100]; double GPA; int ID; };

Upcoming

Next time… More practice with structs Linked lists typedef

Reminders Keep working on Project 4 Keep reading K&R chapter 6 Due Friday Keep reading K&R chapter 6