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

Explain what is the best way to comment out a section of code that contains comments?

952


What is function prototype in c language?

834


How can I sort a linked list?

828


how can f be used for both float and double arguments in printf? Are not they different types?

861


What is realloc in c?

853


How do I send escape sequences to control a terminal or other device?

865


in case any function return float value we must declare a) the function must be declared as 'float' in main() as well b) the function automatically returned float values c) function before declared 'float' keyword d) all the above

833


What are the complete rules for header file searching?

883


Explain low-order bytes.

846


What is the need of structure in c?

821


PROGRAM TO WRITE CONTENTS OF 1 FILE IN REVERSE TO ANOTHER FILE,PROGRAM TO COPY 1 FILE TO ANOTHER BY SPECIFYING FILE NAMES AS COMMAND LINE

1741


Define circular linked list.

815


Write a c program to build a heap method using Pointer to function and pointer to structure ?

4438


What is New modifiers?

907


what will be maximum number of comparisons when number of elements are given?

1688