Saturday, 21 July 2018

Array

Arrays





An array is a collection of similar data type stored at continuous memory locations.
Variable (Data type) can be int,char,float

Declaration Syntax;
int a[10]; (integer array)
char b[10]; (character array)
int *p[10]; (array of pointer)
int *fptr[10] (int,int);(array of function pointer)






Tuesday, 5 June 2018

Typedef



typedef is a reserved keyword in the C and C++ programming languages. It is used to create an alias name for another data type.



Limitations :-


  • typedef is limited to giving symbolic names to types only, where as #define can be used to define alias for values as well, e.g., you can define 1 as ONE, 3.14 as PI, etc.

  • define should not be terminated with semicolon, but typedef should be terminated with semicolon.

  • typedef follows the scope rule which means if a new type is defined in a scope (inside a function), then the new type name will only be visible till the scope is there.




  • Enum

    An enum is a keyword, it is an user defined data type. All properties of integer are apples to Enum. It is used for creating an user defined data type of integer. Using enum we can create sequence of integer constant value.

    Key Points :


    • With the help of enumeration we can put a name for constant.
    • User readability improves;
    • Default first member value is 0(zero)
    • We cannot increment or decrement.
    Syntax :

    enum { red, black = 5, white, green };


    Union

    A union is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes.

    Key Points :

    • It is an user defined data type;
    • Union is a collection of different type of memory but not in contiguous form;
    • In union all member will share same location;
    • Size of union depends upon the size of the largest member;
    Syntax :

               union tagname 
                          {
                            member 1;
                            member 2;
                            member 3;
                            .
                            .
                            .
                           };

    STRUCTURE - 3 (Structure padding)


                         

                  "Allocating the memory for a structure variable more then the required memory is called structure padding, that extra byte of memory is called hole in a structure;"






    Some more examples:

    struct A
             {
             int a;
             char c;
             float f;
             };
    Size of struct A should be 9 but it is 12, that is known as structure padding and extra 3 byte are known as hole.

    For removing the hole we can use below keyword:

    #pragma pack(1)   //only in GCC compiler 



    STRUCTURE - 2

    Now we discuss some key features of structure :- 

    Structure Pointer :

                    Pointer size is dependent upon OS ,same applies for structure pointer. we will assume a 32-bit system.

    Exp :

    struct st
             {
              int a;
              char b;
              };
    main()
    {
    struct st a1 ={10, p} , *ST;
    ST = &a1;

    for access :-

    printf("%d %c",ST->a , ST->b );

    Ways to Pass a structure in function :

    1) print1(a1.a, a1.b);
    2) print2(ST);
    3) print3(&a1);

    Structure Bit Field :

    •  Bit field is a mechanism in C-Language where we can allocate a memory in the form of  Bits.
    • Bit field can only be used in Structure and Union.
    struct A
             {
             int a:6;
             chat ch:3;
             };

    Here int take b bit and char take only 3;

    Limitation of bit Field :
    • We cannot allocate more bit size, then the actual size.
    • bit field are applicable only for char and int.
    • Getting address of bit field variable is possible.
      

    Monday, 4 June 2018

    STRUCTURE - 1

    Structure is an user define data type which is collection of different primitive data types.

    Syntax :

    struct tagname
           {
           member 1;
           member 2;
           member 3;
           .
           .
           .
           };



    • tagname is nothing but new data type;
    • Until and unless we create a variable for structure there is no memory allocated for the member;
    • If we want to access the member of structure through structure variable we use dot(.) operator;
    • If we want to access the member of structure through structure pointer, we use arrow (->) operator;

    Limitation of structure :-

    • It is not possible to initialize the member of structure.
    • Storage classes are not allowed for structure members but storage classes are allowed for variable of structure.
    • In a structure we can't declare the same structure type but we can create a same structure pointer. (Self referential structure)
    • Structure within structure is possible but the changed name (using typedef) structure is not possible in that structure.