what is difference between array,strutter,union and pointers
Answer Posted / ravi
structure is collection of different data types.
union is same as the structures, the only difference is in
memory allocation.
struct
{
int a;
float b;
};
struct s;
s.a=10;
s.b=3.0;
printf("%d%f",s.a,s.b);
For the above structure it allocates 2bytes for integer
variable and 4 bytes for flaot variable. so totally it
allocates 6bytes of memory to that structure. we can print
the s.a and s.b values as 10 and 3.0
#include<stdio.h>
union
{
int a;
char ch;
}u;
u.a=100;
u.ch='a';
printf("%d\n%d",u.a,u.ch);
But incase of union, the memory allocated is maximum
memory required among the variables. so only 4 bytes is
allocated to unions.
if u try to print the u.a, u.ch values it prints 97, 97
where the value of u.a is being assigned by u.ch . so we
cannt get value of u.a at last. It wont happen incase of
structures. so mostly we use structures in programming.
| Is This Answer Correct ? | 31 Yes | 6 No |
Post New Answer View All Answers
Was 2000 a leap year?
What math functions are available for integers? For floating point?
What is data structure in c and its types?
Explain the difference between ++u and u++?
What is difference between && and & in c?
What is extern variable in c with example?
Why is sizeof () an operator and not a function?
Draw a diagram showing how the operating system relates to users, application programs, and the computer hardware ?
What is a volatile keyword in c?
Explain what is meant by 'bit masking'?
What is time complexity c?
What is a struct c#?
What is the difference between variable declaration and variable definition in c?
What is nested structure?
Why double pointer is used in c?