-1

the print statementthe output I get

    for(i = 0; i<n; i++) //printing
    {printf("Name: %s\nAddress: %s\nOccupation: %s\nAge: %d", p[i].name, p[i].add, p[i].occ, p[i].age);}
timrau
  • 22,578
  • 4
  • 51
  • 64
Lakshita
  • 3
  • 1
  • Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – MikeCAT Feb 23 '21 at 08:58
  • Also [please do not post images of text (the output in this case) because they are hard to use.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) Texts should be posted directly **as text** in your question. – MikeCAT Feb 23 '21 at 08:59
  • You may want to use a structure instead of a union. – MikeCAT Feb 23 '21 at 09:00
  • Try putting string values after assigning the integer value. – MikeCAT Feb 23 '21 at 09:01
  • Learn what is union. [c - Difference between a Structure and a Union - Stack Overflow](https://stackoverflow.com/questions/346536/difference-between-a-structure-and-a-union) – MikeCAT Feb 23 '21 at 09:03

1 Answers1

1

If p[i] is a union, then it can hold only one of its members. However, in your print:

printf("Name: %s\nAddress: %s\nOccupation: %s\nAge: %d", p[i].name, p[i].add, p[i].occ, p[i].age);

you seem to think it holds all of the values. For that you don't need a union but a struct.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41