How to use structure and union in C? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

How to use structure and union in C?

24th Jun 2020, 12:16 PM
Manoj Kumar Mahato
Manoj Kumar Mahato - avatar
2 Antworten
+ 4
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; } 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; }
24th Jun 2020, 12:51 PM
Ankit Yadav
Ankit Yadav - avatar