MCQs
No answer description available for this question.
Both <struct type name> and <structure variables> are optional. Thus the structure
defined in the above program has no use and program executes in the normal way.
This program illustrates the transfer of a structure to a function by value. Also the altered structure is now returned directly to the calling portion of the program.
What will be output if you compile following c code ?
#include<stdio.h>
struct first
{
int a;
float b;
}s1={32760,12345.12345};
typedef struct
{
char a;
int b;
}second;
struct my_struct
{
float a;
usigned int b;
};
typedef struct my_struct third;
void main()
{
struct second s2={'A',- -4};
third s3;
s3.a=~(s1.a-32760);
s3.b=-++s2.b;
printf("%d %.2f\n%c %d\n%.2f %u",(s1.a)--,s1.b+0.005,s2.a+32,s2.b,++(s3.a),--s3.b);
}
Illustrating 3 different ways of declaring the structres : first, second and third are the user-defined structure type. s1, s2 and s3 are structure variables. Also an expression of the form ++variable.member is equivalent to ++(variable.member), i.e. ++ operator will apply to the structure member, not the entire structure variable.
No answer description available for this question.
In the above program, values is the user-defined structure type or the new user-defined data type. Structure variables can then be defined in terms of the new data type.
C language does not permit the initialization of individual structure members within the template. The initialization must be done only in the declaration of the actual variables.
What will be output if you compile following c code ?
#include<stdio.h>
struct
{
int i,val[25];
float f;
}var={1,2,3,4,5,6,7,8,9},*vptr=&var;
void main()
{
printf("%d %d %d\n",var.i,vptr->i,(*vptr).i);
printf("%d %d %d %d %d %d",var.val[4], var.val+4), vptr->val[4],*(vptr->val+4),
(*vptr).val[4], *((*vptr).val+4));
}
Since value of the member 'i' can be accessed using var.i, vptr->i and (*vptr).i Similarly 5th value of the member 'val' can be accessed using var.val[4], *(var.val+4), vptr->val[4], *(vptr->val+4), (*vptr).val[4] and *((*vptr).val+4)
This program illustrates the transfer of a structure to a function by passing the structure's address (a pointer) to the function.
What will be output if you compile following c code ?
#include<stdio.h>
struct names
{
char str[25];
struct names *next;
};
typedef struct names slist;
void main()
{
slist *list,*temp;
list=(slist *)malloc(sizeof(slist)); // Dynamic Memory Allocation
strcpy(list->str,"Hai");
list->next=NULL;
temp=(slist *)malloc(sizeof(slist)); // Dynamic Memory Allocation
strcpy(temp->str,"Friends");
temp->next=list;
list=temp;
while (temp != NULL)
{
printf("%s",temp->str);
temp=temp->next;
}
}
It is sometimes desirable to include within a structure one member i.e. a pointer to the parent structure type. Such structures are known as Self-Referencial structures. These structures are very useful in applications that involve linked data structures, such as lists and trees. [A linked data structure is not confined to some maximum number of components. Rather, the data structure can expand or contract in size as required.]