Docsity

User Defined, Custom Data Types - Window Programming - Lecture Slides, Slides of Windows Programming

Partial preview of the text

Download User Defined, Custom Data Types - Window Programming - Lecture Slides and more Slides Windows Programming in PDF only on Docsity!

Windows

Programming

Lecture 04

User-defined

or

Custom Data Types

  • Structures
  • Unions
  • typedefs
  • Enumerations

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.

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.

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

};

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

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

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

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