Day 9 C-Programming
Day 9 C-Programming
Structure
A Structure is a collection of elements, which are of different data types.
It is a user-defined data type that allows grouping variables of different data types under a single
name.
Syntax:
struct StructureName {
dataType member1;
dataType member2;
...
};
Example:
#include <stdio.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
struct Student s1 = {1, "Alice", 95.5};
return 0;
}
Accessing Members:
● Use the dot operator (.) for direct access and use arrow operator(->) to access
members through pointer
● Example: s1.id or s1->id (We will see this after malloc function
today)
Data Type Can store multiple variables of Stores multiple variables of the same
different data types. data type.
Definition Defined using the struct keyword. Declared with the data type followed
Syntax by the size. int a[10]
Accessing Accessed using the dot (.) operator Accessed using an index (e.g.,
Elements and the member name. array[index]).
Example:
#include <stdio.h>
struct Address {
char city[50];
int pin;
};
struct Employee {
int id;
char name[50];
struct Address addr; // Nested structure
};
int main() {
struct Employee e1 = {1, "John", {"New York", 12345}};
return 0;
}
Array of Structures
An array of structures allows storing multiple records of the same structure type.
Example:
#include <stdio.h>
struct Book {
int id;
char title[50];
float price;
};
int main() {
struct Book books[3] = {
{1, "C Programming", 300.50},
{2, "Data Structures", 450.75},
{3, "Algorithms", 500.00}
};
return 0;
}
Union
A union is similar to a structure, but it uses a single shared memory location for all its members.
Only one member can store a value at a time.
Syntax:
union UnionName {
dataType member1;
dataType member2;
...
};
Structure Union
Size The size is the sum of the The size is equal to the size of the
sizes of all members, with largest member, with possible
padding if necessary. padding.
Example:
#include <stdio.h>
union Data {
int i;
float f
char str[20];
};
int main() {
union Data data;
data.i = 10;
printf("Integer: %d\n", data.i);
data.f = 220.5;
printf("Float: %.2f\n", data.f);
return 0;
}
___________________________________________
Dynamic memory allocation allows memory to be allocated at runtime. The standard library
provides functions like malloc, calloc, realloc, and free in <stdlib.h>.
1. malloc()
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int *)malloc(5 * sizeof(int)); // Allocate memory for 5 integers, 5x2 =10 byte
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
Example:
Structure Using arrow operator(->)
#include <stdio.h>
#include <stdlib.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
// Dynamically allocate memory for a structure
struct Student *s1 = (struct Student *)malloc(sizeof(struct Student));
return 0;
}
2. calloc()
Allocates memory in multiple contiguous blocks(like in Array) and initializes all elements to zero.
0 0 0 0 0 0
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int *)calloc(5, sizeof(int)); // Allocate memory for 5 integers, 5 blocks each of 2 Byte
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(3 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
Important Notes:
1. Always use free to release memory allocated with malloc, calloc, or realloc.
2. Accessing freed memory leads to undefined behavior.
____________________________