Structures and Unions in C. Alan L. Cox

Similar documents
Low-Level Programming in C

Fundamental of Programming (C)

typedef int Array[10]; String name; Array ages;

Structures. 5 th March Spring 2012 Programming and Data Structure 1

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

6.096 Introduction to C++ January (IAP) 2009

Chapter 10 C Structures, Unions, Bit Manipulations

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

Arrays and Pointers in C. Alan L. Cox

C Programming Review CSC 4320/6320

CS201 - Introduction to Programming Glossary By

High Performance Computing in C and C++

Short Notes of CS201

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Motivation was to facilitate development of systems software, especially OS development.

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Chapter-8 DATA TYPES. Introduction. Variable:

Motivation was to facilitate development of systems software, especially OS development.

FUNCTION POINTERS. int (*pf)(); // pf is a pointer to a function returning an int.

CSE 230 Intermediate Programming in C and C++

CS 376b Computer Vision

CS349/SE382 A1 C Programming Tutorial

Objects and Classes. Objects and Classes

12 CREATING NEW TYPES

CS240: Programming in C

Structures. Today s Goals. Structure Operations. Structure Type Declaration. Struct Instance. typedef. CS246 Lec12. Structures

Programming 2. Object Oriented Programming. Daniel POP

Special Topics for Embedded Programming

A Fast Review of C Essentials Part I

CSE 374 Programming Concepts & Tools

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Computer System and programming in C

CS Programming In C

CS3157: Advanced Programming. Announcement

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Pointers, Dynamic Data, and Reference Types

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

C PROGRAMMING Lecture 4. 1st semester

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Everything Else C Programming and Software Tools. N.C. State Department of Computer Science

Final CSE 131B Spring 2004

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

EL6483: Brief Overview of C Programming Language

Programming Language Concepts: Lecture 2

CS2141 Software Development using C/C++ C++ Basics

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Creating a C++ Program

COMP 2355 Introduction to Systems Programming

Type Checking. Prof. James L. Frankel Harvard University

Input And Output of C++

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Fundamental of Programming (C)

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Review of the C Programming Language for Principles of Operating Systems

A brief introduction to C programming for Java programmers

Fundamentals of Programming

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Lecture 12: Data Types (and Some Leftover ML)

BLM2031 Structured Programming. Zeyneb KURT

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Computational Physics Operating systems

Today s lecture. CS 314 fall 01 C++ 1, page 1

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Introduction to C++ Systems Programming

Intro to C: Pointers and Arrays

UEE1302 (1102) F10: Introduction to Computers and Programming

Arrays and Pointers in C & C++

C Language Programming

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Chapter 2: Basic Elements of C++

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Topics. bool and string types input/output library functions comments memory allocation templates classes

Exercise Session 2 Simon Gerber

CS242 COMPUTER PROGRAMMING

Programming in C. What is C?... What is C?

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

INTRODUCTION 1 AND REVIEW

Bits and Bytes January 13, 2005

CSE I-Sem)

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Pointer Declarations

Data Representation and Storage. Some definitions (in C)

Pragma intrinsic and more

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

September 10,

Transcription:

Structures and Unions in C Alan L. Cox alc@rice.edu

Administrivia Assignment 1 is due tonight Textbook Lectures begin covering material that is also covered by the textbook on 1/30 Assignment 3 (assigned 2/1) requires use of the textbook Cox / Fagan Structures and Unions 2

Objectives Be able to use compound data structures in programs Be able to pass compound data structures as function arguments, either by value or by reference Be able to do simple bit-vector manipulations Cox / Fagan Structures and Unions 3

Structures Compound data: A date is an int month and an int day and an int year struct ADate { int month; int day; int year; }; struct ADate date; date.month = 1; date.day = 18; date.year = 2018; Unlike Java, C doesn t automatically define functions for initializing and printing Cox / Fagan Structures and Unions 4

Structure Representation & Size sizeof(struct ) = sum of sizeof(field) + alignment padding Processor- and compiler-specific struct CharCharInt { char c1; char c2; int i; } foo; foo.c1 = a ; foo.c2 = b ; foo.i = 0xDEADBEEF; c1 c2 padding i 61 62 EF BE AD DE x86 uses little-endian representation Cox / Fagan Structures and Unions 5

Typedef Mechanism for creating new type names New names are an alias for some other type May improve clarity and/or portability of the program Overload existing typedef long int64_t; type names for typedef struct ADate { clarity and int month; portability int day; int year; } Date; int64_t i = 100000000000; Date d = { 1, 18, 2018 }; Simplify complex type names Cox / Fagan Structures and Unions 6

Constants Allow consistent use of the same constant throughout the program Improves clarity of the program Reduces likelihood of simple errors Easier to update constants in the program Constant names are Preprocessor capitalized by directive convention #define SIZE 10 Define once, int array[10]; int array[size]; use throughout the program for (i=0; i<10; i++) { for (i=0; i<size; i++) { } } Cox / Fagan Structures and Unions 7

Arrays of Structures Array declaration Date birthdays[nfriends]; Constant bool check_birthday(date today) { int i; for (i = 0; i < NFRIENDS; i++) { if ((today.month == birthdays[i].month) && (today.day == birthdays[i].day)) return (true); Array index, then structure field } return (false); Cox / Fagan Structures and Unions 8

Pointers to Structures Date create_date1(int month, int day, int year) { Date d; d.month = month; d.day = day; d.year = year; void create_date2(date *d, int month, Pass-by-reference int day, int year) { d->month = month; d->day = day; d->year = year; } } return (d); Date today; Copies date today = create_date1(1, 18, 2018); create_date2(&today, 1, 18, 2018); Cox / Fagan Structures and Unions 9

Pointers to Structures (cont.) void create_date2(date *d, int month, 0x30A8 year: 2018 { int day, int year) 0x30A4 0x30A0 day: 18 month: 1 d->month = month; d->day = day; 0x3098 d: 0x1000 d->year = year; } void fun_with_dates(void) { 0x1008 0x1004 today.year: today.day: 2018 18 Date today; create_date2(&today, 1, 18, 2018); 0x1000 today.month: 1 } Cox / Fagan Structures and Unions 10

Pointers to Structures (cont.) Date * create_date3(int month, int day, int year) { Date *d; What is d pointing to?!?! (more on this later) d->month = month; d->day = day; d->year = year; } return (d); Cox / Fagan Structures and Unions 11

Abstraction in C From the #include file widget.h: Definition is hidden! struct widget; struct widget *widget_create(void); int widget_op(struct widget *widget, int operand); void widget_destroy(struct widget *widget); From the file widget.c: #include widget.h struct widget { int x; }; Cox / Fagan Structures and Unions 12

Collections of Bools (Bit Vectors) Byte, word,... can represent many Booleans One per bit, e.g., true,..., true 00100101 = false, false, Bit-wise operations: Bit-wise AND: 00100101 & 10111100 == 00100100 Bit-wise OR: 00100101 10111100 == 10111101 Bit-wise NOT: ~ 00100101 == 11011010 Bit-wise XOR: 00100101 ^ 10111100 == 10011001 Cox / Fagan Structures and Unions 13

Operations on Bit Vectors const unsigned int low_three_bits_mask = 0x7; unsigned int bit_vec = 0x15; 0 00 0111 0 01 0101 A mask indicates which bit positions we are interested i Always use C s unsigned types for bit vectors electing bits: important_bits = bit_vec & low_three_bits_mask; 0 00 0101 == 0 01 0101 & 0 00 0111 Result =? Cox / Fagan Structures and Unions 14

Operations on Bit Vectors const unsigned int low_three_bits_mask = 0x7; unsigned int bit_vec = 0x15; 0 00 0111 0 01 0101 Setting bits: bit_vec = low_three_bits_mask; Result =? 0 01 0111 == 0 01 0101 0 00 0111 Cox / Fagan Structures and Unions 15

Operations on Bit Vectors const unsigned int low_three_bits_mask = 0x7; unsigned int bit_vec = 0x15; 0 00 0111 0 01 0101 Clearing bits: bit_vec &= ~low_three_bits_mask; Result =? 0 01 0000 == 0 01 0101 & ~0 00 0111 Cox / Fagan Structures and Unions 16

Bit-field Structures Special syntax packs structure values more tightly Similar to bit vectors, but arguably easier to read Nonetheless, bit vectors are more commonly used. struct Flags { int f1:3; unsigned int f2:1; unsigned int f3:2; } my_flags; my_flags.f1 = -2; my_flags.f2 = 1; my_flags.f3 = 2; Padded to be an integral number of words Placement is compilerspecific. f1 f2 f3 1 1 0 1 1 0 Cox / Fagan Structures and Unions 17

Unions Choices: An element is an int i or a char c sizeof(union ) = maximum of sizeof(field) union AnElt { int i; char c; } elt1, elt2; elt1.i = 4; elt2.c = a ; elt2.i = 0xDEADBEEF; c padding EF BE AD DE i Cox / Fagan Structures and Unions 18

Unions A union value doesn t know which case it contains union AnElt { int i; char c; } elt1, elt2; elt1.i = 4; elt2.c = a ; elt2.i = 0xDEADBEEF; if (elt1 currently has a char) How should your program keep track whether elt1, elt2 hold an int or a char??? Basic answer: Another variable holds that info Cox / Fagan Structures and Unions 19

Tagged Unions Tag every value with its case I.e., pair the type info together with the union Implicit in Java, Scheme, ML, enum Union_Tag { IS_INT, IS_CHAR }; struct TaggedUnion { enum Union_Tag tag; union { int i; char c; } data; }; Enum must be external to struct so constants are globally visib Struct field must be named. Cox / Fagan Structures and Unions 20

Next Time Memory Allocation Cox / Fagan Structures and Unions 21