C Structs Programming in C++ Fall 2008 Dr. David A. Gaitros

Slides:



Advertisements
Similar presentations
Chapter 7 Completing a Program
Advertisements

StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Data Structures Using C++ 2E
EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]
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.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Structure.
Structures in C.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Informática II Prof. Dr. Gustavo Patiño MJ
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS150 Introduction to Computer Science 1
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
1 C++ Structures Starting to think about objects...
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
PART I CHAPTER 16 CS116 SENEM KUMOVA METİN 1. Structures Structures : Aggregate data types built using elements of other types struct Time { int hour;
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Array, Structure and Union
More C++ Features True object initialisation
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
Structures (L30) u Syntax of a struct Declaration u Structure Variables u Accessing Members of Structures u Initialize Structure Variables u Array of Structures.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
Struct Data Type in C++ What Are Structures?
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.
StructureStructure. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Structures  Structure types  Pointers and structure types  Structures.
C Structures and Memory Allocation
Procedural and Object-Oriented Programming
UNIT – I Linked Lists.
Command Line Arguments
Traversing a Linked List
Linked lists.
Data Structures and Abstract Data Types
Dynamic Memory Allocation Reference Variables
C Structures, Unions, and Enumerations
Learning Objectives Structures Structure types
Chapter 17: Linked Lists.
Object oriented programming (OOP) Lecture No. 7
C Programming Pointers
C Programming Lecture-14 Unions
Linked lists.
Arrays and Pointers.
Presentation transcript:

C Structs Programming in C++ Fall 2008 Dr. David A. Gaitros

C Structs Structures are aggregate types which means they are built from other types. Format struct { -- declaration list };

C Structs Keyword struct introduces the structure – The name you give the structure must be unique. – Difference structures may have member names with the same name without conflicting with other structures – One structure can contain members of many different types including other structures – A structure cannot contain a member of the same type as itself

C Structs However! – A structure may contain a pointer to itself – This is called a self-referential structure and it is the important basis for forming linked-list structures in C and C++ programming Each structure ends with a left bracket and a semicolon (;) A structure does not reserve space but creates a new type

Introduction to Object Oriented Programming (OOP) C “Struct” Struct Time { int hour; int minute; int second; }; Time Todays_Time; Time Time_Array[10];

C Structs Example with self referential pointer struct personnel { char name[20]; int age; char sex; struct personnel *tPrev; struct personnel *tNext; }; // Now to declare some variables struct Person, *Persons;

C structs Dot (.) operator used in accessing structure members. Used to access members of structures that are not pointers. – The variable structure name followed by a dot (.) and then the member name. – Example Person.age = 53; Person.sex= “M”;

C structs Using the -> notation. Used to access members of a structure (or class) member via a pointer reference. Example code using pointers and structures. // Allocate memory Persons = new struct personnel; Persons->age = 42. Cout age;

C structs A little link list stuff: // Allocate member struct personnel *person1, *person2; Person1 = new struct personnel; Person2 = new struct personnel; Person1->tNext = Person2; Person1->tPrev = NULL; Person2->tPrev = Person1; Person2->tNext = NULL; Person1->age = 40; Person2->age = 50; cout tNext->age; cout << (*Person2).age;