C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
C Language.
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.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Programming and Data Structure
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures Spring 2013Programming and Data Structure1.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Structures, Unions, and Typedefs CIS 1057 Fall Structures, Unions, and Typedefs CIS 1057 Computer Programming in C Fall 2013 (Many slides based on/borrowed.
Kernighan/Ritchie: Kelley/Pohl:
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Structures C and Data Structures Baojian Hua
Declarations and Expressions C and Data Structures Baojian Hua
CS Winter 2011 Further Introduction to the C programming language.
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
Character Input and Output C and Data Structures Baojian Hua
CS Winter 2011 Introduction to the C programming language.
Pointers and Arrays C and Data Structures Baojian Hua
Structures and UnionsCS-2301 B-term Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
C language issues CSC 172 SPRING 2002 EXTRA LECTURE.
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
Programming in C Structs and Unions. No Classes in C Because C is not an OOP language, there is no way to combine data and code into a single entity.Because.
8. Structures, File I/O, Recursion 18 th October IIT Kanpur 1C Course, Programming club, Fall 2008.
Working with Structures. Structure Declaration ä Creates a user-defined data type. ä Gives the compiler a “head’s up” as to what the structure contains.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
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:
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.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Today’s Material Aggregate Data Types: Structures and Unions
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
CS 261 – Data Structures Introduction to C Programming.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
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.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 12 – C: structs, linked lists, and casts.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
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)
C Structures and Memory Allocation
User-Written Functions
CSE 374 Programming Concepts & Tools
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
CS111 Computer Programming
C Structures and Memory Allocation
Structures, Unions, and Enumerations
Presentation transcript:

C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Why need “Structure”? So far, we’d discussed two kinds of data: Simple: char, int, long, double Large: array, pointer It’s inconvenient in some applications: See next slides for an example

Example // We want to represent year/month/date int f (){ int year1, year2, month, date; year1 = 2050; year2 = 2020; month = 12; date = 30; date++; // ??? Should we increase year1 or year2? } // The problem is that there is no logical // connection between them. We need “structure”!

Structure Declaration // Structure declaration starts with key word // “struct” and followed by an optional struct // tag, and a list (one or more) fields. // For instance, to represent a two-dimensional // point: struct point2d { int x; // the x coordinate int y; // the y coordinate }; // note the semicolon // point2d contains two fields x and y, both of // type int. // struct point2d is a (user-defined) new type.

Variable Definition // Given the declaration above, we may write struct point2d pt; // to define a variable pt to be of type point2d, // just as we write int i;

Structure Fields // Given the variable definition struct point2d pt; // the following syntax fetch its fields x or y: pt.x; pt.y; // And they act as ordinary variables, such as: pt.x = 9; pt.y = 10; // or: printf (“%d\n”, pt.x + pt.y);

Structure in Structure // Having known that structures are types, we may // know its layout in memory. Technically, a // structure occupies a piece of continuous space: // So, we may even nest structures // in other structures, as in: struct rect { point2d pt1; point2d pt2; }; x y x y x y

Structures and Functions // A function creating a point2d structure: struct point2d create (int x, int y) { struct point2d pt; pt.x = x; pt.y = y; return pt; } // A sample call: struct point2d mypt = create (3, 4); x=??? y=???

Structures and Functions // A function creating a point2d structure: struct point2d create (int x, int y) { struct point2d pt; pt.x = x; pt.y = y; return pt; } // A sample call: struct point2d mypt = create (3, 4); x=3 y=4

Structures and Functions // A function creating a point2d structure: struct point2d create (int x, int y) { struct point2d pt; pt.x = x; pt.y = y; return pt; } // A sample call: struct point2d mypt = create (3, 4); x=3 y=4 x=3 y=4

Structures as Functions Arguments // Like the structure return value, passing // structures to functions copy the whole // structure (call-by-value): struct point2d doublee (struct point2d pt) { pt.x *= 2; pt.y *= 2; return pt; } // A sample call: struct point2d mypt = doublee (initPt); x=3 y=4 x=3 y=4

Structures as Functions Arguments // Like the structure return value, passing // structures to functions copy the whole // structure (call-by-value): struct point2d doublee (struct point2d pt) { pt.x *= 2; pt.y *= 2; return pt; } // A sample call: struct point2d mypt = doublee (initPt); x=6 y=8 x=3 y=4

Structures as Functions Arguments // Like the structure return value, passing // structures to functions copy the whole // structure: struct point2d doublee (struct point2d pt) { pt.x *= 2; pt.y *= 2; return pt; } // A sample call: struct point2d mypt = double (initPt); x=6 y=8 x=6 y=8 x=3 y=4

Moral Structures returned from function and passed to functions are also call-by-value Pros: a simple style of functional programming result in elegant and easy to reason code ideas from functional languages (lisp, ML), but may also be useful in imperative languages (C, Java) Cons: may be too inefficient copy a big value inconsistent with array Next, we’d see a more imperative style Update in place

Pointers to Structures // Pointers to structures are just like pointers // to any other kind of data: struct point2d *pt; // declares pt to be a pointer to a structure // ponit2d, which looks like: // To reference x and y, we use: (*pt).x; (*pt).y; // or a more concise notation: pt -> x; pt -> y; pt x=3 y=4

Structure Pointers as Functions Arguments // Address passing: void doublee (struct point2d *pt) { (*pt).x *= 2; (*pt).y *= 2; return; } // A sample call (no return value): doublee (&initPt); pt x=3 y=4

Structure Pointers as Functions Arguments // Address passing: void doublee (struct point2d *pt) { (*pt).x *= 2; (*pt).y *= 2; return; } // A sample call: doublee (&initPt); pt x=6 y=8

Or // Address passing: void doublee (struct point2d * pt) { pt->x *= 2; pt->y *= 2; return; } // A sample call: doublee (&initPt); pt x=6 y=8

Self-referential Structures // With structures pointer mechanism, we may // write self-referential structures (structures // fields point to same type of structures): struct node { int data; struct node *next; }; data p data p data p

Union A union is a variable that may hold (at different times) objects of different types and sizes compilers take care of the space allocation, alignment, etc. Unions provide a way to manipulate different kinds of data in a single area of storage

Union // To declare a union type, we use: union intOrArray { int i; int a[2]; } // which declares intOrArray to have two fields: // integer i and int array a of length 2. i, a[0] a[1]

Union // To declare a union type, we use: union intOrArray { int i; int a[2]; }; // which declares intOrArray to have two fields: // integer i and int array a of length 2. union intOrArray u; u.a[0] = 88; u.a[1] = 99; // u.i = ??? i, a[0] a[1]

Union // To declare a union type, we use: union intOrArray { int i; int a[2]; }; // which declares intOrArray to have two fields: // integer i and int array a of length 2. union intOrArray u; u.i = 77;; // u.a[2] = ??? i, a[0] a[1]

Union Union gives you a magical bag to let you bypass the C’s type system As we’ve seen, we may store an int, but take out an array It’s the programmers’ responsibility to ensure that union data is consistent But what if the union value is written by others or from a library, which we may know nothing about?

Tagged Union // In order to distinguish union state, we // annotate union with tags: struct ss { enum {INT, ARRAY} tag; union intOrArray int i; int a[2]; } u; }; i, a[0] a[1] tag

Tagged Union // And for variable u: struct ss temp; // we may assign temp’s fields (note the tag): temp.u.i = 99; temp.tag = INT; // data reading is guarded: if (INT == temp.tag) printf (“%d”, temp.u.i); else if (ARRAY == temp.tag) printf (“%d, %d”, temp.u.a[0], temp.u.a[1]); else printf (“impossible\n”); i, a[0] a[1] tag

typedef---Define Our Own Types And it’s rather stupid and annoying to always write like this: struct point2d pt; struct point2d *pp; And some types are rather crazy: int (*f[10])(int, int); int (*f(char))(int, int); Is there some better methods? Yes! It’s the “typedef”

typedef---Define Our Own Types // C has a mechanism called “typedef” allowing us // to define our own types (abbreviations). // For instance: typedef struct point2d pt2d; // define “pt2d” to be a type equivalent to // struct point2d. And in the following, “point” // could be used just as any other types: pt2d pt; pt.x = 3; pt.y = 4;

typedef---Define Our Own Types // In essence, not only structures can be // typedefed, but also any type name, even the // pre-defined ones in C, as typedef int size; size i; i = 99; typedef size size2; size2 j; j = i;

typedef---Define Our Own Types // More example of typedef: typedef int *ptrToInt; typedef int (*ptrToFunc); typedef int func(int); typedef double a[5]; typedef int a[]; // Exercises: explain the meanings of variables: ptrToInt p; func f; ptrToFunc *g; a *arr[10];

Summary of Typedefs Typedef doesn’t create any new type name it just adds a shorthand for known types Typedef is an important mechanism: to make modular programming easy to enable information hiding But the physical representation is visible So it’s weak