## - C basics - struct VS union
## - C basics - struct VS union
## – 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.
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;
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.