C Programming Assignment
1. Pointer Arithmetic and Operations
Pointer arithmetic refers to the ability to perform arithmetic operations on pointers. When a pointer
is incremented or decremented, it moves to the next or previous memory location of its base data
type.
The operations that can be performed on pointers are:
1. Increment (++): Moves the pointer to the next memory location.
2. Decrement (--): Moves the pointer to the previous memory location.
3. Addition (+): Adds an integer to a pointer.
4. Subtraction (-): Subtracts an integer from a pointer.
5. Comparison: Compares two pointers of the same type.
2. Program: Structure for Employee
#include <stdio.h>
struct Employee {
char name[50];
int id;
float salary;
};
int main() {
struct Employee emp;
printf("Enter employee name: ");
scanf("%s", [Link]);
printf("Enter employee ID: ");
scanf("%d", &[Link]);
printf("Enter employee salary: ");
scanf("%f", &[Link]);
printf("\nEmployee Details:\n");
printf("Name: %s\n", [Link]);
printf("ID: %d\n", [Link]);
printf("Salary: %.2f\n", [Link]);
return 0;
}
3. Types of Memory Pool
A memory pool is a block of pre-allocated memory that can be reused to manage dynamic memory
efficiently. The main types of memory pools are:
1. Fixed-size Block Pool: Allocates equal-sized blocks for objects of the same size.
2. Variable-size Block Pool: Allocates memory blocks of varying sizes based on the request.
3. Object Pool: Stores a collection of pre-created objects to minimize dynamic allocation overhead.
4. Slab Allocator: Divides memory into slabs, each storing objects of the same type.
4. Program: Store and Display Student Records
#include <stdio.h>
struct Student {
int roll_no;
char name[50];
float marks;
};
int main() {
int n;
printf("Enter number of students: ");
scanf("%d", &n);
struct Student s[n];
for(int i = 0; i < n; i++) {
printf("\nEnter details for student %d:\n", i + 1);
printf("Roll No: ");
scanf("%d", &s[i].roll_no);
printf("Name: ");
scanf("%s", s[i].name);
printf("Marks: ");
scanf("%f", &s[i].marks);
}
printf("\nStudent Details:\n");
for(int i = 0; i < n; i++) {
printf("Roll No: %d, Name: %s, Marks: %.2f\n", s[i].roll_no, s[i].name, s[i].marks);
}
return 0;
}
5. Program: Function Pointers with Parameters
#include <stdio.h>
void add(int a, int b) {
printf("Addition: %d\n", a + b);
}
void subtract(int a, int b) {
printf("Subtraction: %d\n", a - b);
}
void operate(void (*func)(int, int), int x, int y) {
func(x, y);
}
int main() {
void (*fptr)(int, int);
fptr = add;
operate(fptr, 10, 5);
fptr = subtract;
operate(fptr, 10, 5);
return 0;
}
6. Program: Access Nested Structure Members using Pointers
#include <stdio.h>
struct Address {
char city[50];
int pincode;
};
struct Employee {
char name[50];
struct Address addr;
};
int main() {
struct Employee emp = {"Shivu", {"Bangalore", 560068}};
struct Employee *ptr = &emp;
printf("Employee Name: %s\n", ptr->name);
printf("City: %s\n", ptr->[Link]);
printf("Pincode: %d\n", ptr->[Link]);
return 0;
}