Professional Documents
Culture Documents
BASICS OF C
What will you learn?
Dynamic Memory Allocation
Structures
Unions
Structures versus Unions
MEMORY ALLOCATION
There are two types of memory allocations: static allocation and
dynamic allocation
1. malloc()
2. calloc()
3. free()
4. realloc()
MALLOC()
This function is used to allocate one block of storage.
example:
------
------
free (p);
REALLOC()
Reallocation is done when
Syntax:
---------
---------
Example: -
Eg:
struct employee
struct employee e = { 45,”Ajay Kumar”, 25000};
{
int empno;
char name[20];
float salary;
};
MEMORY REPRESENTATION
struct book_bank
{
char title[25];
char author[20];
int pages;
float price;
} book1;
{ union item
data-type member1; {
……………………….. float x;
}; };
DECLARING UNION VARIABLES
Below is the syntax for declaring the union
union <union_name ><variable-name>;
Ex:
Union:-
union item
{
int m;
float x;
char c;
};
Union variable:- union item code;
MEMORY REPRESENTATION
union item
{
int m;
float x;
char c;
};
65497 65498 65499 65500
c
STRUCTURES
VERSUS
UNIONS
Difference Structure Union