Windows Programming Lecture 04

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
C Programming Lecture 23 Enumeration Types Structures.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
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.
Structs CSE 2451 Rong Shi. Structures Structure – one or more values, called members, with possibly dissimilar types that are stored together – Group.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
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.
© 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.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
CS 202 Computer Science II Lab Fall 2009 October 15.
Variables and constants Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
Data Types.
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CISC105 – General Computer Science Class 9 – 07/03/2006.
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Array, Structure and Union
The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
© Oxford University Press All rights reserved. CHAPTER 8 STRUCTURES.
+ 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.
CPS120: Introduction to Computer Science Lecture 15A Structures.
Structures and Enumerations Introduction Structure definitions Nested structure Referring and initializing structure elements Passing structures to a function.
Socket address structures Byte ordering IP address conversion
1.2 Primitive Data Types and Variables
CPS120: Introduction to Computer Science Data Structures.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
Java Programming Language Lecture27- An Introduction.
Windows Programming Lecture 03. Pointers and Arrays.
Structures, Unions, Enumerations
Pointers, Enum, and Structures
Structure, Unions, typedef and enumeration
(Structures,Unions, Enumerations )
CSE101-Lec#3 Components of C Identifiers and Keywords Data types.
DATA HANDLING.
Pointers and References
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Using Arrays in C Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning.
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Derived types.
Built-In (a.k.a. Native) Types in C++
Chapter 1: Introduction to Data Structures(8M)
C Structures, Unions, Bit Manipulations and Enumerations
C Programming Lecture-13 Structures
Structures.
Engineering H192 Winter 2005 Structs and Enumeration Prof. Almas Ansari Dept of Computer Science Engineering Anjuman College of Engineering & Technology..
C Programming Lecture-14 Unions
C Language B. DHIVYA 17PCA140 II MCA.
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
C++ New Types.
Structures Chapter 4.
Structures, Unions, and Enumerations
Exercise 6 – Compound Types und Kontrollfluss
Presentation transcript:

Windows Programming Lecture 04

User-defined or Custom Data Types Structures Unions typedefs Enumerations

structure A structure is a collection of variables under a single name. A structure is a convenient way of grouping several pieces of related information together. These variables can be of different data types, and each has a name which is used to select it from the structure.

Pointer to Structures struct Person { char name[30]; //30 bytes int age; //4 bytes float length; //4 bytes }; struct Person abc, *ptr; ptr = &abc; ptr = ptr + 1; // skips 38 bytes

How to Access Members of a Structures Member of a structure is accessed using member access operator ( . ) which selects a member from a structure.

How to Access Members of a Structure… abc.name; // accesses first member of struct Person abc.age; // accesses second member of struct Person abc.length; // accesses third member of struct Person

Member Access Operator abc.age; member access operator

Indirect Member Access (Arrow) Operator Members of a structure can also be accessed using pointer to structures with ( -> ) operator.

Indirect Member Access (Arrow) Operator struct Person *ptr; ptr -> length; Indirect Member Access Operator

Member Access Operators When we have a pointer to a structure, we can dereference the pointer and then use dot as a member access operator. ptr -> age is equivalent to (* ptr).age

Nested Structures Struct Person { char name[30]; //30 bytes int age; //4 bytes float length; //4 bytes }; struct Student struct Person p; //38 bytes int rollno; //4 bytes

Nested Structures st . p . height = 5.42; ptr = &st; struct Student st, *ptr; st . p . height = 5.42; ptr = &st; ptr -> p .height; Must use ‘.’ operator

Un-named Nested Structures struct Person { char name[30]; //30 bytes int age; //4 bytes float length; //4 bytes }; struct Student struct char name[30]; int age; float length; }p; int rollno; //4 bytes

Union union model_name { type1 element1; type2 element2; Unions allows a same portion of memory to be accessed as different data types. Its declaration and use is similar to that of structures but its functionality is totally different. union model_name { type1 element1; type2 element2; type3 element3; ... } object_name; All the elements of the union declaration occupy the same space of memory. Its size is equal to the greatest element of the declaration.

Union Example: union mytypes_t { char c; int i; float f; }mytypes; This union defines three elements: mytypes.c mytypes.i mytypes.f Each one is of a different data type. All of them are referring to the same location in memory. The modification of one of the elements will affect the value of all of them.

Union union Person abc, * ptr; union Person { char name[30]; //30 bytes int age; float length; }; union Person abc, * ptr; ptr = &abc; ptr = ptr + 1; //skips 30 bytes

Little Endian and Big Endian In Little Endian format, least significant byte is stored on lower memory location In Big Endian format, most significant byte is stored on lower memory location

Nested Unions union MyUnion { long a; // contains 4 bytes struct short low; short high; }s; char c[4]; }; union MyUnion abc; abc.a = 1; abc.s.low = 2; abc.s.high = 3; abc.c[0] = ‘f’;

typedef C allows us to define our own types based on other existing data types. In order to do that keyword typedef is used. typedef   existing_type   new_type_name ; where existing_type is a C fundamental or any other defined type and new_type_name is the name that the new type we are going to define will receive.

typedef Example: typedef char C; C a[5]; // a is a pointer to // character typedef unsigned int WORD; WORD i; // i is an unsigned int

Advantages of typedef Long chain of keyword in declarations can be shortened. Actual definition of the datatype can be changed.

Examples of typedef typedef struct Person HUMAN; typedef char * pCHAR; typedef unsigned char byte; typedef struct Person HUMAN; typedef char * pCHAR;

Enumerations enum model_name { value1, value2, value3, ...}; Enumerations serve to create data types to contain something different that is not limited neither to numerical or character constants. enum model_name { value1, value2, value3, ...}; For example, we could create a new type of variable called color to store colors with the following declaration: enum colors {black, green, blue, red};

Enumerations enum colors {black, green, blue, red}; enum colors myColor; myColor = black; if(mycolor == green) { … … … }

Enumerations enum months { JANUARY, FEBRUARY, ….}; Internally treated as integers enum months { JANUARY, FEBRUARY, ….}; JANUARY stands for 0 FEBRUARY stands for 1 and so on An initial value can be specified for the enumeration constant enum months { JANUARY=1, FEBRUARY, ….}; JANUARY stands for 1 FEBRUARY stands for 2 and so on

Questions Nested unions struct in_addr { union { struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b; struct { u_short s_w1,s_w2; } S_un_w; u_long S_addr; } S_un; };

Questions Typedef char *(*FUNC_TYPE)(void) enum SQUARE {ONE=1, TWO=4, THREE=9};