Wednesday, December 5, 2012

Structure solution


0Ans 10.3   Given is the structure declaration
                struct abc
                {              int x;
                                float y;
                                char z;
                }
                Following are the statements which declare and initialize the structure variable :
struct  a , b , c ; // Error The structure variable will not be declared as tag name abc  is         missing             
<!--[if !supportLists]-->II.                  <!--[endif]-->struct abc a , b , c ; //Correct Declaration
<!--[if !supportLists]-->III.                <!--[endif]-->abc  x , y , z ;   //Error the structure variable will not be declared as struct keyword is missing
<!--[if !supportLists]-->IV.                <!--[endif]-->struct abc a[] ;  //Error Array size is  missing
<!--[if !supportLists]-->V.                  <!--[endif]-->struct abc a = { }; // a is assigned a value which is never used
<!--[if !supportLists]-->VI.                <!--[endif]-->struct abc = b , { 1+2 , 3.0 , “xyz”}  //Error syntactically wrong
<!--[if !supportLists]-->VII.              <!--[endif]-->struct abc c = { 4 , 5 , 6 } //Error last member is a character it should be enclosed in single quotes
<!--[if !supportLists]-->VIII.            <!--[endif]-->struct abc a = 4 , 5.0 , “xyz” ; //Error Initialization values should be enclosed in curly braces
Ans 10.4   None of the given statements in the question  is  legal
Ans 10.5  For The given declaration
                struct item_bank
                {              int number;
                                Double cost;
                }
                Following are the correct statements
                (d)          struct item_bank items[10];
Ans 10.6   For the given declaration
                typedef struct abc
                {              char x;
                                int y ;
                                float z[10] ;
                }    ABC ;

Following declarations are invalid
<!--[if !supportLists]-->(a)    <!--[endif]-->struct ABC v3;
Ans 10.8 Purpose of the following is:
<!--[if !supportLists]-->a.       <!--[endif]-->Template
<!--[if !supportLists]-->b.      <!--[endif]-->struct keyword : is used declare , define and initialize a structure variable
<!--[if !supportLists]-->c.       <!--[endif]-->typedef keyword  : is used to declare the alias name for the existing datatype
<!--[if !supportLists]-->d.      <!--[endif]-->sizeof operator :  is used to get the size of the variable which is passed as parameter to the function
<!--[if !supportLists]-->e.      <!--[endif]-->tag name : it’s the name given to a structure defined by us using struct keyword
Ans 10.9  struct                                              // Tag name for structure is missing
                {              int number ;
                                float price ;
                }                                                              //; is missing
Ans 10.10 Use of the following is as given below:
<!--[if !supportLists]-->a.       <!--[endif]-->union -  The concept of union has been partially borrowed from the concept of structure the major difference being the members of union share same memory location whereas the members of structure share different memory location. We use union when we want a group of variables which we are using at different time but want them under a common data type we use union
<!--[if !supportLists]-->b.      <!--[endif]-->Bit fields – There are several instances when data requires much less than 16 bits space. For this we use bit fields which is a set of adjacent bits whose size can be from 1 to 16 bits in length
<!--[if !supportLists]-->c.       <!--[endif]-->The size of operator – this operator is used for determining the size occupied by  the variable in the memory which is provided as operand to the size of operator.
Ans 10.12  For th following declarations and definition
                struct abc
                {              int a;
                                float b ;
                };
                struct xyz
                {              int x;
                                float y; 
                };
                abc         a1  , a2;
                xyz         x1 , x2;
a1 = x1;    //Error two structure variables cannot be copied
<!--[if !supportLists]-->b.      <!--[endif]-->abc.a1 = 10.75; //Error structure variable cannot be initialized in this way
<!--[if !supportLists]-->c.       <!--[endif]-->int m  = a + x ; //Error structure members cannot be accessed in this way
<!--[if !supportLists]-->d.      <!--[endif]-->int n = x1.x + 10 // Correct
<!--[if !supportLists]-->e.      <!--[endif]-->a1 = a2 //Error two structure variables can be  copied only member wise not directly
<!--[if !supportLists]-->f.        <!--[endif]-->if (a.a1 > x.x1)   // Error the member should come after the variable name.
<!--[if !supportLists]-->g.       <!--[endif]-->if( a1.a < x1.x)   //Correct
<!--[if !supportLists]-->h.      <!--[endif]-->if (x1 != x2 )    //Error two structure variables cannot be compared directly




Ans 10.15   Computer stores structures using the concept of “ word boundary”. A  word is the natural/native amount of data fetched frommemory on a single access by a CPU. Now storing the data at the boundary of the word the data can be fetched very fast. The size of the word boundary is machine dependent. Now suppose  that the word boundary of the given machine is 4 byte. Now a structure having three data members char ,  int  ,  float will be stored in the memory as shown below . Now we can easily understand that the empty space will be there  this empty byte in between the two data members is termed as slack byte.






Ans 10.16 The various points to be considered while implementing bit field are as follows:
          i. We cannot take address of a bit field variable. Therefore we cannot use scanf function for reading     them and neither we can use pointer variable for such type of structure.
ii.     Bit fields cannot be arrayed.
iii.    Bit fields should be assigned values that are within the range of the of their size.
Ans 10.19 
typedef struct product
                {              char name[10];
                                float price ;
                }              PRODUCT products[10] ;                               // Error Immediately after the alias name variable
                                                                                                                cannot be declared
//Correct mode
typedef struct product
                {              char name[10] ;
                                float price;
                }              PRODUCT;
PRODUCT products[10];



Ans 10.20
Output:
8                                                                              Explanation union will occupy the space according to largest member of the same
10                                                                           At a time only one member can use the memory location of the union
1.23                                                                        Same reason as above

No comments:

Post a Comment