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


Please Help Members By Posting Answers For Below Questions

What are global variables?

640


How do we make a global variable accessible across files? Explain the extern keyword?

1416


Take an MxN matrice from user and then sum upper diagonal in a variable and lower diagonal in a separate variables. Print the result

1467


Explain what are linked list?

617


Disadvantages of C language.

651






the number of measuring units from a arbitrary starting point in a record area or control block to some other point a) branching b) recording pointer c) none d) offset

637


write a c program to print the next of a particular no without using the arithmetic operator or looping statements?

3180


please explain clearly about execution of c program in detail,in which stage are the printf sacnf getting into exeecutable code

1701


What is difference between structure and union with example?

590


What are the different types of errors?

635


Do you know what are the properties of union in c?

577


Why is c so popular?

644


What is #include cctype?

574


What are local static variables?

616


Why doesnt long int work?

607