Header Ads

3.7 - User Defined Datatypes.


UDDs – Structures
  • Sometimes it becomes tough to build a whole software that works only with integers, floating values, and characters. 
  • In circumstances such as these, you can create your own data types which are based on the standard ones 
  • There are some mechanisms for doing this in C:
    • Structures 
    • Unions 
    • Typedef 
    • Enums
  •  Composite (or compound) data type : 
  • Any data type which can be constructed from primitive data types and other composite types 
  • It is sometimes called a structure or aggregate data type 
  • Primitives types – int, char, float, double
 
Example
struct Student
{
 int id;
         char name[30];
         char address[150];
};

int main()
{
struct Student s1 = {10, “Tingu”, “Bangalore”};
          printf(“Struture starts at %p\n”, &s1);
          printf(“Member id is at %p\n”, &s1.id);
          printf(“Member name is at %p\n”, s1.name);
          printf(“Member address is at %p\n”, s1.address);
return 0;
}
Structures - Functions
  • The structures can be passed as parameter and can be returned from a function
  • This happens just like normal datatypes.
  • The parameter passing can have two methods again a normal
    • Pass by value
    • Pass by reference
Structures – Functions – Pass by Value  "Not recommended on larger structures"
Example:
struct Student
{
int id;
        char name[30];
        char address[150];
};
void data(struct Student s)
{
       s.id = 10;
}
int main()
{
        struct Student s1;
        data(s1);
return 0;
} 
Structures – Functions – Pass by Reference  "Recommended on larger structures" Example 
struct Student
{
int id;
       char name[30];
       char address[150]
};
void data(struct Student *s)
{
        s->id = 10;
}
int main()
{
         struct Student s1;
         data(&s1);
return 0;
}
Data Alignment
  • A way the data is arranged and accessed in computer memory 
  • When a modern computer reads from or writes to a memory address, it will do this in word sized chunks (4bytes in 32 bit system) or larger. 
  • The main idea is to increase the efficiency of the CPU, while handling the data, by arranging at a memory address equal to some multiple of the word size 
  • So, Data alignment is an important issue for all programmers who directly use memory. 
  • If you don't understand data and its address alignment issues in your software, the following scenarios, in increasing order of severity, are all possible:
    • – Your software will run slower.
    • – Your application will lock up.
    • – Your operating system will crash.
    • – Your software will silently fail, yielding incorrect results.
  • Fetching the character by the CPU will be like shown below
Example
int main()
{
    char ch = 'A';
    int num = 0x12345678;
} 
     ● Fetching the integer by the CPU will be like shown below..

Structures – Bit Fields

  • The compiler generally gives the memory allocation in multiples of bytes, like 1, 2, 4 etc., 
  • What if we want to have freedom of having getting allocations in bits?!. 
  • This can be achieved with bit fields. 
  • But not that
    • The minimum memory allocation for a bit field member would be a byte that can be broken in max of 8 bits
    • The maximum number of bits assigned to a member would depend on the length modifier
    • The default size is equal to word size
Example
    struct Nibble
    {
        unsigned char upper     : 4;
        unsigned char lower    : 4;
    }; 
  • The above structure divides a char into two nibbles
  • We can access these nibbles independently
UDDs – Unions
  • Like structures, unions may have different members with different data types.
  • The major difference is, the structure members get different memory allocation, and in case of unions there will be single memory allocation for the biggest data type
Example
    union DummyVar
    {
        char option;
        int id;
        double height;
    }; 
  • The above union will get the size allocated for the type double 
  • The size of the union will be 8 bytes. 
  • All members will be using the same space when accessed 
  • The value the union contain would be the latest update 
  • So as summary a single variable can store different type of data as required
UDTs - Typedefs
  • Typedef is used to create a new name to the existing types.
  • K&R states that there are two reasons for using a typedef.
  1. It provides a means to make a program more portable. Instead of having to change a type everywhere it appears throughout the program's source files, only a single typedef statement needs to be changed
  2. Second, a typedef can make a complex definition or declaration easier to understand.
UDTs – Enums
  • Set of named integral values
  • The above example has two members with its values starting from 0. i.e, e_false = 0 and e_true = 1. 
  • The member values can be explicitly initialized 
  • The is no constraint in values, it can be in any order and same values can be repeated 
  • Enums does not have name space of its own, so we cannot have same name used again in the same scope.
Examples

     enum Boolean
    {
        e_false,
        e_true
    };
     typedef enum
    {
        e_red = 1,
        e_blue = 4,
        e_green
    } Color;
 typedef enum
{
    red,
    blue
} Color;
int blue; 
...

No comments