انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

البرمجة المهيكلةIOther Data Types

Share |
الكلية كلية تكنولوجيا المعلومات     القسم قسم البرامجيات     المرحلة 1
أستاذ المادة سرى زكي ناجي علوان       4/17/2011 11:10:01 AM

Other Data Types

 

Defined data types (typedef)

 

C++ allows the definition of our own types based on other existing data types. We can do this using the keyword typedef, whose format is:

typedef existing_type new_type_name ;

where
existing_type is a C++ fundamental or compound type and new_type_name is the name for the new type we are defining. For example:

 

1
2
3
4

 

typedef char C;

 

typedef unsigned int WORD;

 

typedef char * pChar;

 

typedef char field [50];

 



In this case we have defined four data types:
C, WORD, pChar and field as char, unsigned int, char* and char[50] respectively, that we could perfectly use in declarations later as any other valid type:

 

1
2
3
4

 

C mychar, anotherchar, *ptc1;

 

WORD myword;

 

pChar ptc2;

 

field name;

 



typedef does not create different types. It only creates synonyms of existing types. That means that the type of myword can be considered to be either WORD or unsigned int, since both are in fact the same type.

typedef can be useful to define an alias for a type that is frequently used within a program. It is also useful to define types when it is possible that we will need to change the type in later versions of our program, or if a type you want to use has a name that is too long or confusing.

 

Unions

 

Unions allow one same portion of memory to be accessed as different data types, since all of them are in fact the same location in memory. Its declaration and use is similar to the one of structures but its functionality is totally different:

 

union union_name {

 

  member_type1 member_name1;

 

  member_type2 member_name2;

 

  member_type3 member_name3;

 

  .

 

  .

 

} object_names;

 


All the elements of the
union declaration occupy the same physical space in memory. Its size is the one of the greatest element of the declaration. For example:

 

1
2
3
4
5

 

union mytypes_t {

 

  char c;

 

  int i;

 

  float f;

 

  } mytypes;

 



defines three elements:

 

1
2
3

 

mytypes.c

 

mytypes.i

 

mytypes.f

 



each one with a different data type. Since 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. We cannot store different values in them independent of each other.

One of the uses a union may have is to unite an elementary type with an array or structures of smaller elements. For example:

 

1
2
3
4
5
6
7
8

 

union mix_t {

 

  long l;

 

  struct {

 

    short hi;

 

    short lo;

 

    } s;

 

  char c[4];

 

} mix;

 



defines three names that allow us to access the same group of 4 bytes:
mix.l, mix.s and mix.c and which we can use according to how we want to access these bytes, as if they were a single long-type data, as if they were two short elements or as an array of char elements, respectively. I have mixed types, arrays and structures in the union so that you can see the different ways that we can access the data. For a little-endian system (most PC platforms), this union could be represented as:


المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
الرجوع الى لوحة التحكم