Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.

Slides:



Advertisements
Similar presentations
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Advertisements

6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Abstract Data Types and Encapsulation Concepts
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
C Basics Computer Organization I 1 May 2010 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell Labs.
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.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CS 261 – Data Structures Introduction to C Programming.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
More Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Allocating Arrays Dynamically You allocate an array by.
Structs in C Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens struct Properties The C struct mechanism is vaguely similar.
Lecture 5 – Function (Part 2) FTMK, UTeM – Sem /2014.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
Generic Programming in C
A History Lesson Adapted from Chapter 1 in C++ for Java Programmers by Weiss and C for Java Programmers: a Primer by McDowell Development of language by.
Pointers and Dynamic Arrays
Classes C++ representation of an object
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Abstract Data Types and Encapsulation Concepts
Linked Lists A linked list is a data structure that uses a "chain" of node objects, connected by pointers, to organize a collection of user data values.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to the C programming language
struct Copy Operation In C, variables are copied in three situations:
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Student Book An Introduction
C Basics.
Java Review: Reference Types
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
This pointer, Dynamic memory allocation, Constructors and Destructor
Pointers and References
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Abstract Data Types and Encapsulation Concepts
More About Data Types & Functions
Built-In (a.k.a. Native) Types in C++
A History Lesson Adapted from Chapter 1 in C++ for Java Programmers by Weiss and C for Java Programmers: a Primer by McDowell Development of language by.
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Classes and Objects.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Pointers Pointers point to memory locations
Pass-by-Pointer Pointers are also used in C to enable a function to modify a variable held by the caller: void findExtrema(const int *pA, int Sz, int *pMin,
Data Structures and Algorithms Introduction to Pointers
Java Programming Language
Course Overview PART I: overview material PART II: inside a compiler
The C Language: Intro.
CPS120: Introduction to Computer Science
Pointers and References
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
11.1 The Concept of Abstraction
Presentation transcript:

struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct types encapsulate data members struct Location { int X, Y; }; But there are vital differences: - struct data members are "public", in fact there is no notion of access control - struct types cannot have function members - there is no concept of inheritance or of polymorphism CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens

A struct Example Note: - assignment is supported for struct types struct Location { // declare type globally int X, Y; }; int main() { struct Location A; // declare variable of type Location A.X = 5; // set its data members A.X = 6; struct Location B; // declare another Location variable B = A; // copy members of A into B return 0; } Note: - assignment is supported for struct types - type declaration syntax used here requires specific use of struct in instance declarations CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens

Another struct Example struct locationType { // declare type globally int X, Y; }; typedef struct locationType Location; // alias a type name int main() { Location A; // declare variable of type Location A.X = 5; // set its data members A.X = 6; Location B; // declare another Location variable B = A; // copy members of A into B return 0; } Note: - use of typedef creates an alias for the struct type - simplifies declaration of instances CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens

struct Limitations What else is supported naturally for struct types? Not much… - no automatic support for equality comparisons (or other relational comparisons) - no automatic support for I/O of struct variables - no automatic support for deep copy - no automatic support for arithmetic operations, even if they make sense… - can pass struct variables as parameters (default is pass-by-copy of course) - can return a struct variable from a function - can implement other operations via user-defined (non-member) functions CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens

A struct Function Example struct locationType { // declare type globally int X, Y; }; typedef struct locationType Location; // alias a type name void initLocation(Location* L, int x, int y) { (*L).X = x; (*L).Y = y; } // call: initLocation(&A, 5, 6); Note: - must pass Location object by pointer so function can modify original copy - given a pointer to a struct variable, we access its members by dereferencing the pointer (to get its target) and then using the member selector operator '.' - the parentheses around the *L are necessary because * has lower precedence than . - however, we can write L->X instead of (*L).X. - use of address-of '&' operator in call to create pointer to A CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens

Another struct Function Example struct locationType { // declare type globally int X, Y; }; typedef struct locationType Location; // alias a type name Location updateLocation(Location Old, Location Move) { Location Updated; // make a local Location object Updated.X = Old.X + Move.X; // compute its members Updated.Y = Old.Y + Move.Y; return Updated; // return copy of local object; } Note: - we do not allocate Updated dynamically (via malloc); there is no need since we know at compile time how many we need (1) and we can just return a copy and avoid the cost of a dynamic allocation at runtime - in C, dynamic allocation should only be used when logically necessary CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens

Typical struct Code Organization // header file Location.h contains declaration of type and // supporting functions #ifndef LOCATION_H #define LOCATION_H struct locationType { // declare type globally int X, Y; }; typedef struct locationType Location; // alias a type name Location updateLocation(Location Old, Location Move); . . . #endif // Source file Location.c contains implementations of supporting // functions #include "Location.h" Location updateLocation(Location Old, Location Move) { . . . } CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens

More Complex struct Types // A struct type may contain array members, members of other // struct types, anything in fact: #ifndef QUADRILATERAL_H #define QUADRILATERAL_H #include "Location.h" #define NUMCORNERS 4 struct quadrilateralType { Location Corners[NUMCORNERS]; }; typedef struct quadrilateralType Quadrilateral; . . . #endif Note: - even though you cannot assign one array to another and you cannot return an array from a function, you can do both of those things with a struct variable that contains an array member - Why? CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens

A C Library Macro offsetof(type, member-designator) expands to an integer constant expression that has type size_t, the value of which is the offset in bytes, to the structure member (designated by member-designator), from the beginning of its structure (designated by type). For example, given the Location type defined earlier, you could do something like this: Location A; int *p = &A.Y; // p points to A.Y Location *q = (Location*)((uint8_t*)p – (uint8_t*)offsetof(Location, Y)); // q points to A // we need the pointer casts to make the arithmetic work CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens

More Complex struct Types #ifndef SNODE_H #define SNODE_H struct snodeType { void* Elem; struct snodeType* Next; }; typedef struct snodeType SNode; #endif There are two approaches to making a generic list in C: - use a void* member to point to a data object of any type, which loses all ability to do compile-time type checking - embed the node type as a member within the user data object… CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens

Another C Macro /* Converts pointer to list element LIST_ELEM into a pointer to the structure that LIST_ELEM is embedded inside. Supply the name of the outer structure STRUCT and the member name MEMBER of the list element. See the Pintos source distribution for examples. */ #define list_entry(LIST_ELEM, STRUCT, MEMBER) \ ((STRUCT *) ((uint8_t *) &(LIST_ELEM)->next \ - offsetof(STRUCT, MEMBER.next))) The pre-processor macro above provides the client a pointer to a user data object when given a pointer to a struct LIST_ELEM object that is contained within the user data object… adapted from the Pintos OS kernel code used in CS 3204. CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens