0% found this document useful (0 votes)
5 views

## - C basics - struct VS union

The document provides an overview of C programming concepts focusing on structs and unions, highlighting their definitions, differences, and usage. It explains how to define and access struct and union members, as well as how to use structs as pointers. Additionally, it mentions related video resources for further learning on user-defined data types and pointers.

Uploaded by

rosahshhsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

## - C basics - struct VS union

The document provides an overview of C programming concepts focusing on structs and unions, highlighting their definitions, differences, and usage. It explains how to define and access struct and union members, as well as how to use structs as pointers. Additionally, it mentions related video resources for further learning on user-defined data types and pointers.

Uploaded by

rosahshhsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C Basics

## – struct vs union
/AhmedHatemS

/c/AhmedHatemS
What is the content?
1. Quick review on struct.
2. Difference between struct and union.
3. Struct as a pointer.
struct quick review
struct date
Defines type struct date, with 3 fields of
{ type int.
The names of the fields are local in the context
int month; of the structure.
A struct declaration defines a type: if not
int day; followed by a list of variables it reserves no
storage; it merely describes a template or shape
int year; of a structure.
};
struct date today, purchaseDate; Defines variables of type struct date.

today.year = 2021; Accesses fields of a variable of type struct date.


today.month = 4; A member of a particular structure is referred to in an
expression by a construction of the form
today.day = 1; structurename.member
struct quick review con.
typedef struct date
{ What’s new
int month; here?

int day;
int year;
} Date;
Date today, purchaseDate;
today.year = 2021;
today.month = 4;
today.day = 1;
struct quick review con.
typedef struct bankAccount account1.name[0] = ‘A';
account1.name[1] = ‘m';
{ account1.name[2] = ‘e';
char name[15]; account1.name[3] = ‘e';
account1.name[4] = ‘r';
int accountNo; account1.name[5] = '\0';
double balance; account1.accountNo = 123456;
account1.balance = 500.0;
Date birthday; account1.birthday.day = 1;
} BankAccount; account1.birthday.month = 4;
account1.birthday.year = 2021;

BankAccount account1, account2;


union
union data
Defines type union date, with 3 fields of type
{ int, float, array of chars.
The names of the fields are local in the context
int i; of the structure.
A union declaration defines a type: if not
float f; followed by a list of variables it reserves no
storage; it merely describes a template or shape
char str[10]; of a structure.
};
union data d1, d2; Defines variables of type union data.

d1.i = 4; Accesses fields of a variable of type union data.


d1.f = 10.0; A member of a particular structure is referred to in an
expression by a construction of the form
d1.str = “hello”; unionname.member
union con.
typedef union data
{ What’s new
int i; here?

float f;
char str[10];
}Data;
Data d1, d2;
d1.i = 4;
d1.f = 10.0;
d1.str = “hello”;
Struct as a pointer
typedef struct date
{
int month;
int day;
int year;
}Date;
Date *todayPtr, today; todayPtr = &today;

todayPtr->year = 2021;
todayPtr->month = 4;
todayPtr->day = 1;
There is a video where we discussed the user defined data
Note types like, typedef, enumeration and struct. And, other
two videos where we discussed the pointers in detail. You
can watch them if having any problem with them.

You might also like