What is the difference between structure & union?

How can we multiply this A*B without using "*" operator?

Showing Answers 1 - 39 of 39 Answers

Sree Bhaarath

  • Oct 24th, 2007
 

A structure is a union in which each defined data type will have its own memory.Ex
Struct{
      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.

  Was this answer useful?  Yes

jintojos

  • Jun 9th, 2008
 

The difference between structure and union is that......
 if we declared two structure variables as
struct 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

  Was this answer useful?  Yes

laxmii.s

  • Aug 22nd, 2008
 

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 

  Was this answer useful?  Yes

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.

kbjarnason

  • Jul 1st, 2010
 

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;
     ...
   }


  Was this answer useful?  Yes

Elanchezhian

  • Aug 10th, 2011
 

int a,b,c=0;
for(int i=0;i {
c=b+c;
}

  Was this answer useful?  Yes

smm90

  • 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.  

  Was this answer useful?  Yes

the_code

  • Aug 24th, 2011
 

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. }

  Was this answer useful?  Yes

sounak pandey

  • 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);

  Was this answer useful?  Yes

SEETU SAHU

  • 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

  Was this answer useful?  Yes

Nitesh Raj

  • Apr 22nd, 2015
 

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.

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.

Union is best in the environment where memory is less as it shares the memory allocated.But structure can not implemented in shared memory.

As memory is shared,ambiguity are more in union,but less in structure.
Self referential union can not be implemented in any data structure ,but self referential structure can be implemented.

  Was this answer useful?  Yes

Alejandro Visiedo

  • Mar 5th, 2016
 

In an "union" type, all the members are mapped to the same base address, so that the size of the type is determinated by the biggest member.
In a "structure" type, each member is mapped in sequence, depending of the size of the previous member, and depending if the members are aligned or not. The size of the type will be the sum of all members, if they are not aligned (or more, in the case of the members would be aligned).

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions