Week 9 - Monday.  What did we talk about last time?  Time  GDB.

Slides:



Advertisements
Similar presentations
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
Advertisements

Programming in C Chapter 10 Structures and Unions
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.
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.
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
CS Winter 2011 Introduction to the C programming language.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
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 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
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.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
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.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CPS120: Introduction to Computer Science Lecture 15A Structures.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
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.
Week 15 - Wednesday.  What did we talk about last time?  Review up to Exam 1.
Week 9 - Monday CS222.
Classes and Objects.
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Programming in C Pointer Basics.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Structures, Unions, and Enumerations
Week 9 - Monday CS222.
Presentation transcript:

Week 9 - Monday

 What did we talk about last time?  Time  GDB

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

 A struct in C is:  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;... };

 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

 In Java, a struct-like class would be used to group some data conveniently  Examples: public class Point { private double x; private double y; //constructor //methods } 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 } public class Student { private String name; private double GPA; private int ID; //constructor //methods } A class to hold a point in spaceA class to hold student data

 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: struct point { double x; double y; }; struct point { double x; double y; }; struct student { char name[100]; double GPA; int ID; }; struct student { char name[100]; double GPA; int ID; }; A struct to hold a point in spaceA struct to hold student data

 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; struct student bob; struct student jameel; struct point start; struct point end;

 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 = ; printf("Bob's GPA: %f\n", bob.GPA); struct student bob; strcpy(bob.name, "Bob Blobberwob"); bob.GPA = 3.7; bob.ID = ; printf("Bob's GPA: %f\n", bob.GPA);

 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 = ; struct student julio; strcpy(julio.name, "Julio Iglesias"); julio.GPA = 3.9; julio.ID = ; struct student julio = { "Julio Iglesias", 3.9, }; struct student julio = { "Julio Iglesias", 3.9, };

 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 = ; bob = julio; struct student julio; struct student bob; strcpy(julio.name, "Julio Iglesias"); julio.GPA = 3.9; julio.ID = ; bob = julio;

 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]; }; struct point { double x; double y; }; struct triangle { struct point vertices[3]; };

 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 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 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 struct person { char* firstName; char* lastName; }; struct person bob1; struct person bob2; struct person { char* firstName; char* lastName; }; struct person bob1; struct person bob2;

 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];

 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 = ; studentPointer = &bob; (*studentPointer).GPA = 2.8; studentPointer = (struct student*) malloc(sizeof(struct student)); struct student bob; struct student* studentPointer; strcpy(bob.name, "Bob Blobberwob"); bob.GPA = 3.7; bob.ID = ; studentPointer = &bob; (*studentPointer).GPA = 2.8; studentPointer = (struct student*) malloc(sizeof(struct student));

 As we saw on the previous page, 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; struct student* studentPointer = (struct student*) malloc(sizeof(struct student)); (*studentPointer).ID = 3030; studentPointer->ID = 3030;

 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; } void flip(struct point* value) { double temp = value->x; value->x = value->y; value->y = temp; }

 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

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

 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; }; struct student { char name[100]; double GPA; int ID; };

 More practice with structs  Linked lists  typedef

 Start on Project 4  Keep reading K&R chapter 6