Tuesday, 13 February 2018

STRUCTURES



1. Explain Structure padding,bit fields,#pragma in detail?
2. Difference between union & structures?
3. Nested  union possible or not?if possible explain the senario.
4. Explain self referencial structures & self referencial union?
5. Real time application of union?
6. Why padding?
7. Is there any  relation between padding and internal hardware?
8. What is bit field?why we need bitfeilds?
9.Declare a structure variable is of pointer type and allocate memory   for that
10.Declare array of structures and allocate memory for that
11. what is structure padding and why it is used , structure packing.
12. size of struct and union
13. Array vs structure
14. Structure padding, packing
18. How to access 8 bits as bit by bit or all at once.
19. What is structure padding? Explain #pragma and #error in details.
20. What is bit-fields, explain with an example ?
21. Can we find the address of bit feilds?
22. How can you pass the pack value through gcc?
23. A union contains two elements , say a and b, suppose a is initialised in main and union object is passed to a function say func(), then how wil u identify which element of union is initaialised ( a or b?) inside func().
24. How to avoid stracture padding without changing the code ?
25. Which data structure can be used to implement sudoko puzzle.

1. What is the output of the following code?
#include<stdio.h>
struct A
{
        int ary[2];
};

struct B
{
        struct A ary[2];
};
struct C
{
        struct B ary[2];
}D[2];
int main()
{
        printf("\n%d\n",(int)&(D[1].ary[0].ary[1].ary[0])-(int)D);
}

2. What is the output of the following code?
#include<stdio.h>
struct A
{
        union U *next;
        int data[10];
};
struct B
{
        union U *next;
        int data[5];
};
union U
{
        struct A a;
        struct B b;
};
int main()
{
        union U u1,u2,u3,u4,u5;
        union U *ptr;
        int i;
        for(i=0;i<10;i++)
                u1.a.data[i]=i;
        u1.a.next=&u2;
        for(i=0;i<5;i++)
                u2.b.data[i]=10*i;
        u2.b.next=&u3;
        for(i=0;i<10;i++)
                u3.a.data[i]=100*i;
        u3.a.next=&u4;
        for(i=0;i<5;i++)
                u4.b.data[i]=1000*i;
        u4.b.next=&u5;
        for(i=0;i<10;i++)
                u5.a.data[i]=10000*i;
        u5.a.next=NULL;
        ptr=&u1;
        i=0;
        while(ptr)
        {
                printf(" %d ",ptr->a.data[i]);
                i++;
                ptr=ptr->a.next;
       }
}

3. What is the output of the following code?
#include<stdio.h>
struct A
{
        double d;
};
struct bitfields
{
        int i;
        int a:16;
        float j;
        int b:16;
        struct A k;
        unsigned int c:7;
};

int main()
{
        printf(" %d ",sizeof(struct bitfields));
}


4. Structure Smaller Than a Word:
        struct st{
                char c;
        };

5. Structure with no padding
      struct st{
        char a;
        char b;
        short c;
        long d;
      };

6. Half word alignment
      struct st{
        char a;
        short b;
      };

7. struct tag{
        short s:9;
        int j:9;
        char c;
        short t:9;
        short u:9;
        char d;
};
size of the structure?

8. struct st{
        char c;
        int :0;
        char d;
        short :9
        char e;
        char :0;
};
 size of structure?

9. struct st1 {
        int a;
       
        struct {
                int c;
        };
}obj;

sizeof obj?

10. struct st1 {
        int a;
        struct st2{
                int c;
        };
}obj;
sizeof obj?
               
11. struct st1 {
        int a;
       
        struct {
                int c;
        }obj2;
}obj;

sizeof obj?
                                               
12. struct st{
        char a;
        static int b;
};

can we do this?if not why?

13. struct st{
        #include <stdio.h>
        char a;
        #include <string.h>
        static int b;
};
can we do this?if not why?

14. struct st{
        #define MAX 50
        char a;
       
        static int b;
};
can we do this?if not why?

15. struct st{
       
        int d:3;
      }obj;

 16. struct st {
        int a:2;
        int b;
}obj;
int main()
{
        obj.a = 2;
        printf("%d  %d\n",obj.a,obj.b);
}

17. Size of a structure given that size of the integer
            struct abc {
                        int a;
                        char b;
                        struct abc x;
            }

            struct abc {
                        int a;
                        char b;
                        struct abc *x;
            }

18. struct st {
        unsigned int a:2;
        int b;
}obj;
int main()
{
        obj.a = 2;
        printf("%d  %d\n",obj.a,obj.b);
}


19. struct tag {
                int a:0;
                char a;
        };
        can we do this ? if not why?

20. struct tag {
                        int a:-1;
                        char a;
        };
        can we do this ? if not why?

21. struct tag {
                        int a:33;
                        char a;
        };
        can we do this ? if not why?

22. Size of a structure given that size of the integer
            struct abc {
                        int a;
                        char b;
                        struct abc x;
            }

            struct abc {
                        int a;
                        char b;
                        struct abc *x;
            }

23. Size of a structure given that size of the integer
            struct abc {
                        int a;
                        char b;
                        struct abc x;
            }

            struct abc {
                        int a;
                        char b;
                        struct abc *x;
            }
Struct MyList {
                Int c;
                char *p;
                struct MyList list;
};
what is the size of struct Mylist
storing a structure in a file it will be in not readable format why is it?

24. struct exp{
int a;
int b;
int c;
}
int fun(struct exp *s1)
{
      printf("%d",s1->a);
      printf("%d",s1->b);
      printf("%d",(*s1).c);
}
int main()
{
      struct exp s1;
      s1.a=10;
      s1.b=20;
      s1.c=30;
      fun(&s1);
}

25. #include       <stdio.h>
int main()
{
      struct abc {
            int x;
            int y;
            int z;
      }
      struct abc v = {1, 2, 3};
      struct abc *ptr = &v;
      printf("%d ", v.x);
      printf("%d ", ptr->z);
     printf("%d ", (*ptr).z);
}
What is the output of the above code:

27. Given two structure variables of same structure, how will you compare each of them? If you say memcmp they will tell memcmp will not always work and you will be asked to explain why it will not always work.

28. Size of a structure given that size of the integer
            struct abc {
                        int a;
                        char b;
                        struct abc x;
            }

            struct abc {
                        int a;
                        char b;
                        struct abc *x;
            }
     
29. struct exp{
      int a;
      int b;
      int c;
      }
      int fun(struct exp *s1)
      {
      printf("%d",s1->a);
      printf("%d",s1->b);
      printf("%d",(*s1).c);
}

30. int main()
      {
      struct exp s1;
      s1.a=10;
      s1.b=20;
      s1.c=30;
      fun(&s1);
}

31. what is structure padding and why it is used , structure packing.
32. struct and union diff
33. size of struct and union
15) structure padding and packing and why nedded
56) how to access 8 bits as bit by bit or all at once
Answer:
    union a{
        char byte b;
        struct bit {
            char b0:1;
            char b1:1;
            char b2:1;
            char b3:1;
            char b4:1;
            char b5:1;
            char b6:1;
            char b7:1;
    };
}
55) bit fields
30) int main ( )
{
      union a
      {
            int a;
            int b;
      }a;
      a.a = 10;
       a.b = 5;
      printf ( "%d%d\n", a.a, a.b );
}

48) array vs structure
        structure padding, packing
        indexing in array
        array elements accessing for loop, struct directly

No comments:

Post a Comment