a Can structure can be nested inside another structure b Can
a. Can structure can be nested inside another structure?
b. Can an union be nested in a structure?
c. Which operator is used to access structure elements using a structure variable?
d. Which operator can be used to access structures elements using a pointer to a structure variable only?
e. Can you create an array of pointer to structures?
f. Diffecrence between calloc() and malloc()?
g. Use malloc to allocate space.
Solution
a) YES
Example:
Structure 1:
struct struct_address
{
int street;
char *state;
char *city;
char *country;
}
Structure 2:
struct struct_data
{
int struct_id;
int struct_age;
char *struct_name;
struct struct_address structAddress;
}
b) YES
Example:
#include<stdio.h>
int main()
{
stct s
{
int a;
union u
{
char c;
} u1;
}s1;
s1.u1.c=\'m\';
s1.a= 20;
printf(\"%d\\t%c\",s1.a,s1.u1.c);
}
Output: 20 m
c) . operator is used to access structure elements using a structure variable.
d) -> operator used to access structures elements using a pointer to a structure variable.
e) YES
Example:
struct students
{
int roll;
char name[10];
}*ptr[20];
f)
i. malloc stands for memory allocation where as calloc stands for contiguous allocation.
ii. void *malloc(size_t n) returns a pointer to n bytes of un-initialized storage or NULL if the request cannot be satisfied. On the other side,
void *calloc(size_t n, size_t size) returns a pointer to enough free space for an array of n objects or NULL if the request cannot be satisfied.
iii. Syntax of malloc is malloc(): where as syntax of calloc is calloc():
g)
ptr = (int*) malloc(200 * sizeof(int));
The above statement will allocate either 400 or 800 according to size of int 2 or 4 bytes respectively.

