Structures and Union.

Slides:



Advertisements
Similar presentations
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Programming in C Chapter 10 Structures and Unions
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.
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Unions and Bitfields Gabriel Hugh Elkaim Spring 2013.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
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)
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Lecture 10 union, enumeration, bit
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
Data types Data types Basic types
A bit of C programming Lecture 3 Uli Raich.
Pointers, Enum, and Structures
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
Java Primer 1: Types, Classes and Operators
C Structures, Unions, Bit Manipulations and Enumerations
CS1010 Programming Methodology
CISC/CMPE320 - Prof. McLeod
C Basics.
Introduction to C Programming
Data Type.
DATA HANDLING.
Pointers and References
Lecture 9 Structure 1. Concepts of structure Pointers of structures
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Data Type.
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to Abstract Data Types
Structure ការណែនាំអំពី Structure
Derived types.
Built-In (a.k.a. Native) Types in C++
Lecture 18 Arrays and Pointer Arithmetic
C Operators, Operands, Expressions & Statements
CSE 100 Data Types Declarations Displays.
Structures, Unions, and Typedefs
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Chapter 1: Introduction to Data Structures(8M)
Pointers Lecture 2 Tue, Jan 24, 2006.
CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported
Structures and Union.
Initializing variables
C Structures, Unions, Bit Manipulations and Enumerations
Data Type.
Structures and Union.
C Language B. DHIVYA 17PCA140 II MCA.
Pointers and References
Structures Chapter 4.
Structures, Unions, and Enumerations
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

Structures and Union

Review bitwise operations enumeration preprocessor directives you need them for performance in terms of space and time shifts are equivalent to arithmetics enumeration you can define a set each member is represented as an integer preprocessor directives process your program before it is compiled

Structures Like enum, it may define a new type unnamed named Like enum, it may define a new type Aggregate variables of different types Each member of a structure can be array structure arrays of structures

Accessing a member dot ( . ) operator structure_name.member_name e.g) yesterday.year -> operator (same expressions) pointer_to_structure->member_name (*pointer_to_structure).member_name

Using structures assignment works (NOT for arrays) as long as two variables are of the same structure type structure is more like a primitive type when used as a function parameter call by value – the whole structure is copied inefficient this is one of reasons why there exists the -> operator if it contains an array, the whole array is copied

to write a function to update employee information pass a structure pass a pointer to structure (this is more efficient because ...)

1. 2.

Initialization

unions similar to structure, but it defines a set of alternative values that may be stored in a shared location The programmer is responsible for interpreting the value correctly

Unions to access a union member (same as a structure) . -> the memebers of a structure and or a union can be array, structure, union

#include <stdio.h> typedef union int_or_float { int i; float f; } number; int main(void) { number n; n.i = 4444; printf("i: %10d f: %16.10e \n", n.i, n.f); n.f = 4444.0; return 0; }

bit field struct floating_number { unsigned sign_bit : 1, exponent : 8, significand : 23; } r1, r2; A bit field is an int or unsigned member of a structure or a union bit fields may be unnamed unnamed bit field of width 0 is for alignment of the next word restrictions array of bit fields address operator &

#include <limits.h> #include <stdio.h> typedef struct { unsigned b0 : 8, b1 : 8, b2 : 8, b3 : 8; } word_bytes; unsigned b0 : 1, b1 : 1, b2 : 1, b3 : 1, b4 : 1, b5 : 1, b6 : 1, b7 : 1, b8 : 1, b9 : 1, b10 : 1, b11 : 1, b12 : 1, b13 : 1, b14: 1, b15 : 1, b16 : 1, b17 : 1, b18 : 1, b19 : 1, b20 : 1, b21: 1, b22 : 1, b23 : 1, b24 : 1, b25 : 1, b26 : 1, b27 : 1, b28: 1, b29 : 1, b30 : 1, b31; } word_bits; typedef union { int i; word_bits bit; word_bytes byte; } word; word w = {0}; w.bit.b8 = 1; w.byte.b0 = ‘a’;