Showing posts with label Structures. Show all posts
Showing posts with label Structures. Show all posts

Structures in C

Structure is user defined data type which is used to store heterogeneous data items under unique name. Keyword 'struct' is used to declare structure.

Each variable in structure is known as a structure member. A structure can contain any valid data types like int, char, float, array or even other structures.

Syntax:

        Struct <structure name>
        {
            <data type 1> <element 1>;
            <data type 2> <element 2);

             -     -           -           -
             -       -     -           -           -
            <data type n> <element n>;
        }<structure Variable>;

 Example:
        Struct sample
        {
            int num;
            float var;
            char ch;
        };

Instance of structures:
Instances of structure can be created in two ways

           1)  Struct sample
                 {
                          int num[10];
                          float var;
                 }obj;

           2)  Struct sample
                {
                         int num[10];
                        float var;
                };
               Struct sample obj;

Initializing and accessing a Structure:
Structure members can be initialized when you declare a variable of your structure:
              
               Struct sample
               {
                        char name[10];
                        float x;
                        int num;
               };
             Struct sample var={“Program”,1.4,20};

The above declaration will create a struct object called var with an name equal to “Program”, x equal to 1.4, and num equal to 20.
Structure members can be accessed using member operator '.' . It is also called as 'dot operator' or 'period operator'.


/*  Program to Explain about a structure.
Author : http://ccppcoding.blogspot.com   */
#include <stdio.h>
struct example
{
            char EmpName[10];
            int id;
};
void main()
{
            Struct sample obj;
            obj.EmpName=”King”;
            obj.id=2100;
            printf("\n\n Employee Name : %s",obj.EmpName);
            printf("\n\n ID : %d",obj.id);
}

Structures within structures(Nested Structures):
Structures can also be used inside another structure. It is also called as “nesting of structures”.

Example:
            Struct company
            {
                            char Name[10];
                            char address[20];
                            int count;
                            struct employee
                            {
                                        char EmpName[10];
                                        int EmpId;
                            }empobj;
           }compobj;

Inner structure member variables can be accessed as:
           compobj.empobj.EmpName;
           compobj.empobj.EmpId;

How to find the size of the Structure without using sizeof() operator in C

#include<stdio.h>

struct sample{
       int num;
       char addr;
       float val;
};
int main(){
       struct sample *ptr = 0;
       ptr++;
       printf("Size of the structure sample: %d",ptr);
       return 0;

}
OUTPUT:
Size of the structure sample: 9

What is padding? How we can avoid padding in data structures?

Structure padding is adding extra bits at the end, so that the structure completes the word boundary.

Computer memory is usually aligned to a boundary equal in size to the system word size. On a hypothetical 32-bit system, imagine that this word size is four bytes long. In the C language, you can create structures that store data. In order to align them to the word size, the C compiler may add padding to the structure. You can remove this padding by using a preprocessor directive that tells it to align data differently.


Following is the way to disable structure padding using pragma in C.
#pragma pack(push, 1)
//Define your structure here
#pragma pack(pop)
//Structure padding is re enabled.

Some compilers (GNU c) don’t support #pragma directive __attribute__((__packed__)) using this structure padding can be avoided.

 An example of how you would do this is below.
           struct sample
           {
                         unsigned char m1 __attribute__((__packed__));
                         unsigned short m2 __attribute__((__packed__));
                         unsigned long m3 __attribute__((__packed__));
           };

  Another way to achieve the same is as shown below:
           struct sample
           {
                         unsigned char m1;
                         unsigned short m2;
                         unsigned long m3;
           } __attribute__((__packed__));