GeekInterview.com
Series: Subject: Topic:
Question: 242 of 827

What is the difference between structure & union?

How can we multiply this A*B without using "*" operator?
Asked by: Interview Candidate | Asked on: Oct 21st, 2007
Showing Answers 1 - 11 of 11 Answers
Sree Bhaarath

Answered On : Oct 24th, 2007

A structure is a union in which each defined data type will have its own memory.ExStruct{      int a;      float b;} In the above structure b has highest memory but bith a,b are having personalised memory,Where as in  union the highest memory data type will be memory for entire union.Suppose if a  float element is intialaised and an int element, the int will be first located on 2 bytes and then the next 2 bytes will be the float.

  
Login to rate this answer.
jintojos

Answered On : Jun 9th, 2008

View all answers by jintojos

The difference between structure and union is that...... if we declared two structure variables asstruct strct1 x,y;then the two structure variables x and  have different memory location.But we declared tow unio variables as union uni1 x, y;then the two unio variables x and  have same memory location.that is the main difference between an structure and union

  
Login to rate this answer.
laxmii.s

Answered On : Aug 22nd, 2008

View all answers by laxmii.s

Size of the structure is sum of the size of each member in the structure. But size of the union is size of largest member in the union, because the union members are overlaps on each other in memory.bye 

  
Login to rate this answer.
rashmi.mohanty

Answered On : Sep 10th, 2008

View all answers by rashmi.mohanty

Difference are:1. union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members. 2. In union, one block is used by all the member of the union but in case of structure, each member have their own memory space.3. union is best in the environment where memory is less as it shares the memory allocated.But structure can not implemented in shared memory.4. As memory is shared,ambiguity are more in union,but less in structure.5. self referencial union can not be implemented in any datastructure ,but self referencial structure can be implemented.

Yes  1 User has rated as useful.
  
Login to rate this answer.
rashmi.mohanty

Answered On : Sep 10th, 2008

View all answers by rashmi.mohanty

Void main(){  int n1=3,n2=4;  int i,mul=0;  for(i=1;i<=n2;i++)          mul=mul+n1;  printf("MULTIPLICATION VALUE:%d",mul);}I think this is the best way to represent the program

Yes  1 User has rated as useful.
  
Login to rate this answer.
kbjarnason

Answered On : Jul 1st, 2010

View all answers by kbjarnason

A structure's elements are all distinct, without overlap:struct x{   char x;   int    y;   long  z;};You can modify x or y or z without affecting the values of the others; they each have their own storage.By contrast, a union overlaps:union {   char x;   int    y;   long z;} u;Here, each element overlaps the others.  Modifying one renders the others undefined - if you assign to x, don't try to read from y.Why would you use this?  Perhaps to manage variant data:struct variant{    char type;    union    {        char c;        int    i;        long l;        float f;    } data;};If you write functions which can handle varying types, you might then do something like:   struct variant v;   v.type = 'c';     /* specify this is a char type */   v.data.c = 'z';  /* value is 'z' */   ...   v.type = 'i';       /* Now it's an int type */   v.data.i = 117; /* value is 117 */And your handling function might be something like:  switch( v.type )  {     case 'c' : printf( "%c", v.data.c ); break;     case 'i' : printf( "%d", v.data.i); break;     ...   }

  
Login to rate this answer.
Elanchezhian

Answered On : Aug 10th, 2011

Int a,b,c=0;
for(int i=0;i

  
Login to rate this answer.
smm90

Answered On : Aug 13th, 2011

Code
  1. void main()
  2. {
  3.  int r=0,i,a,b;
  4.  scanf("%d%d",&a,&b);
  5.  
  6. for(i=0;i<b;i++)
  7.  r+=a;
  8.  
  9. a*b=%d",r);
  10. }
  11.  

  
Login to rate this answer.
the_code

Answered On : Aug 24th, 2011

View all answers by the_code

If the only thing is not to use '*' operator then I would prefer below method: A: mutiplier B: multilpicand mul: multiplication mul= A/(1/B); Sample C code:

Code
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int A=3, B=4,mul;
  5. mul=(float)A/(1/(float)B);
  6. printf("multiplication is %d",mul);
  7. return 0;
  8. }

  
Login to rate this answer.
sounak pandey

Answered On : May 21st, 2013

All the members of the structure can be accessed at once,where as in an union only one member can be used at a time. Another important difference is in the size allocated to a structure and an union. for eg:

Code
  1. struct example
  2. {
  3. int integer;
  4. float floating_numbers;
  5. }
  6. the size allocated here is sizeof(int)+sizeof(float);
  7. where as in an union
  8. union example
  9. {
  10. int integer;
  11. float floating_numbers;
  12. }
  13. size allocated is the size of the highest member.
  14. so size is=sizeof(float);

  
Login to rate this answer.
SEETU SAHU

Answered On : Oct 24th, 2013

1.union allocates the memory equal to the maximum memory required by the member of union,Structure allocate the memory to the total memory required by the member.

2. in union one block is used by all member of union,in structure each member have their own memory space

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.