0% found this document useful (0 votes)
4 views3 pages

C Programming: Pointers & Structures Guide

The document covers various concepts in C programming, including pointer arithmetic, structures for employee and student records, memory pool types, function pointers, and accessing nested structure members using pointers. It provides code examples for each concept, demonstrating how to implement them in C. Overall, it serves as a guide for understanding and applying these fundamental programming techniques.

Uploaded by

Shivu M
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)
4 views3 pages

C Programming: Pointers & Structures Guide

The document covers various concepts in C programming, including pointer arithmetic, structures for employee and student records, memory pool types, function pointers, and accessing nested structure members using pointers. It provides code examples for each concept, demonstrating how to implement them in C. Overall, it serves as a guide for understanding and applying these fundamental programming techniques.

Uploaded by

Shivu M
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

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;
}

You might also like