Struct Union Ex C

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

#include <stdio.

h>

// Declaration of union is same as structures

union test {

int x, y;

};

int main()

// A union variable t

union test t;

t.x = 2; // t.y also gets value 2

printf("After making x = 2:\n x = %d, y = %d\n\n",t.x, t.y);

t.y = 10; // t.x is also updated to 10

printf("After making y = 10:\n x = %d, y = %d\n\n",t.x, t.y);

return 0;

}
#include <stdio.h>

union test {

int x;

char y;

};

int main()

union test p1;

p1.x = 65;

// p2 is a pointer to union p1

union test* p2 = &p1;

// Accessing union members using pointer

printf("%d %c", p2->x, p2->y);

return 0;

#include<stdio.h>

struct Point

int x, y;

};

int main()

struct Point p1 = {0, 1};

// Accessing members of point p1

p1.x = 20;

printf ("x = %d, y = %d", p1.x, p1.y);


return 0;

------------------------------------------------------------

#include<stdio.h>

struct Point

int x, y;

};

int main()

// Create an array of structures

struct Point arr[10];

// Access array members

arr[0].x = 10;

arr[0].y = 20;

printf("%d %d", arr[0].x, arr[0].y);

return 0;

You might also like