CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 6 : September 6.

Slides:



Advertisements
Similar presentations
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
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.
Programming in C Chapter 10 Structures and Unions
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
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.
Programming and Data Structure
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structure.
Structures Spring 2013Programming and Data Structure1.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Programming Languages -1 (Introduction to C) structures Instructor: M.Fatih AMASYALI
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
C language issues CSC 172 SPRING 2002 EXTRA LECTURE.
Create your own types: typedef #define N 3 typedef double scalar; /* note defs outside fns */ typedef scalar vector[N]; typedef scalar matrix[N][N]; /*
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
 Review structures  Program to demonstrate a structure containing a pointer.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
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:
Programming Languages -1 (Introduction to C) structures
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Today’s Material Aggregate Data Types: Structures and Unions
 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.
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.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
Structures and Unions in C Alan L. Cox
Pointers *, &, array similarities, functions, sizeof.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 12 – C: structs, linked lists, and casts.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Variables and memory addresses
1 Lecture06: Complex Types 4/18/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
CE-2810 Dr. Mark L. Hornick 1 “Classes” in C. CS-280 Dr. Mark L. Hornick 2 A struct is a complex datatype that can consist of Primitive datatypes Ints,
Structs & typedef. Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example:
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers What is the data type of pointer variables?
Instructor: Ioannis A. Vetsikas
DATA HANDLING.
Homework / Exam Continuing K&R Chapter 6 Exam 2 after next class
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Instructor: Ioannis A. Vetsikas
Built-In (a.k.a. Native) Types in C++
Programming Language C Language.
CSCE 206 Lab Structured Programming in C
Structures, Unions, and Enumerations
Presentation transcript:

CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 6 : September 6

2 Create your own types: typedef #define N 3 typedef double scalar; typedef scalar vector[N]; typedef scalar matrix[N][N]; void add_vectors( vector x, vector y, vector z ) { int i; for( i = 0; i < N; i++ ) { x[i] = y[i] + z[i]; } Syntax: typedef Way to remember it: typedef

3 Structures struct card_struct { int pips; char suit; }; typedef struct card_struct card; The structure mechanism allows us to aggregate elements of different types Think of objects (but not quite!) Your struct definition generally goes outside all functions struct card_struct { int pips; char suit; } c1, c2; typedef struct card_struct card; void some_function() { struct card_struct a; card b; /* a, b have same types */ b.pips = 3; }

4 Structures (continued) Syntax: struct [ ] { component-declarations } [ ]; Access elements/fields with dot operator: –b.pips or a.suit Structure Names/Tags –If a structure type does not have a name, then only a finite number of structures can be created –Structure tags form their own namespace! Structure fields also form their own namespace…

5 Structures (continued) Once a structure is declared you can use it as though it were a default data type (e.g. int) struct point {int x,y;}; struct rect {struct point pt1, pt2;}; Can return from and also pass structures to functions (call-by-value) int ptinrect (struct point p, struct point r) { return p.x>=r.pt1.x && p.x<r.pt2.x && p.y>=r.pt1.y && p.y<r.pt2.y }

6 Structures (continued) Another example of a function: struct point midpoint (struct point a, struct point b) { struct m = {(a.x+b.x)/2, (a.y+b.y)/2}; return m; } Usually one uses typedef to name the struct in some way and thus does not have to put struct all the time What does a=b do, when we have defined struct point a=b ? Can also define pointers to structures in the same way as for simple data types: –struct point pt, *point_ptr; We can use the &,* operators in the same way as before: –*point_ptr or &pt

7 Structures – accessing via pointer If p is a pointer to a structure and x is an element of the structure then to access this element one puts: (*p).x or more commonly p->x struct card_struct { int pips; char suit; }; typedef struct card_struct card; void set_card( card *c ) { c->pips = 3; c->suit = ‘A’; } void main() { card a; set_card( &a ); }

8 Pointers to Structures (continued) Call by value: expensive (and slow) to pass structures between functions –Use pointer instead (this simulates call by reference) Operators -> and. are left-to-right associative and have maximum precedence along with () and [] –e.g. ++pp->x increments field x, not pointer pp

9 Structures – initialize You may initialize a variable corresponding to a structure that was defined by initializing all its elements as follows: struct name var = {init_element_1, …, init_element_n} #include struct address_struct { char *street; char *city_and_state; long zip_code; }; typedef struct address_struct address; void main() { address a = { "1449 Crosby Drive", "Fort Washington, PA", }; }

10 Unions Similar to structures, but they can only hold one of the elements at a time So they use the same spot in memory to save any of the possible elements. Memory for union is max of memory needed for each element union int_or_float { int i; float f; }; union int_or_float a; /* need to keep track of, on own, which type is held */ /* a.i always an int, a.f always a float */

11 sizeof Operator sizeof expression/object or sizeof returns the size of the type of the expression or the type in bytes –the expression is not evaluated –E.g. sizeof a, sizeof a[13] (a is array) or sizeof int –Typing sizeof a/sizeof a[0] returns? Cannot be applied to functions

12 Read from K&R Covered today Other issues Homework 2 due on Friday You MUST print the output exactly in the same way as in the examples which are given Do not forget to include your name and student ID# when you submit your programs by + give me printout