0% found this document useful (0 votes)
21 views9 pages

Unit 2 Prractice Programs

Uploaded by

Vaikul Gandi
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)
21 views9 pages

Unit 2 Prractice Programs

Uploaded by

Vaikul Gandi
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
You are on page 1/ 9

Unit 2 Practice programs in C language

Difficulty level (Low - L, Medium – M, High – H )


Swap two numbers using pointers: (L)

#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {

int x = 5, y = 10;

printf("Before swap: x = %d, y = %d\n", x, y);

swap(&x, &y);

printf("After swap: x = %d, y = %d\n", x, y);

return 0;

Array manipulation using pointers: (L)


#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;

for (int i = 0; i < 5; i++) {


printf("%d ", *ptr);
ptr++;
}

return 0;
}
Pointer arithmetic: (L)
#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;

for (int i = 0; i < 5; i++) {


printf("Value: %d, Address: %p\n", *ptr, ptr);
ptr++;
}

return 0;
}

Dynamic memory allocation using pointers: (M)


#include <stdio.h>
#include <stdlib.h>

int main() {
int *ptr;
ptr = (int *)malloc(sizeof(int));
*ptr = 10;

printf("Value: %d\n", *ptr);

free(ptr);
return 0;
}

Function returning a pointer: (M)


#include <stdio.h>

int *multiplyByTwo(int num) {


int result = num * 2;
return &result;
}

int main() {
int x = 5;
int *ptr = multiplyByTwo(x);

printf("Result: %d\n", *ptr);


return 0;
}

Array of pointers: (L)


#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptrArr[5];

for (int i = 0; i < 5; i++) {


ptrArr[i] = &arr[i];
printf("Value: %d, Address: %p\n", *ptrArr[i], ptrArr[i]);
}

return 0;
}

Pointer to functions: (L)


#include <stdio.h>

int add(int a, int b) {


return a + b;
}

int main() {
int (*ptr)(int, int) = &add;

int result = (*ptr)(10, 5);

printf("Result: %d\n", result);


return 0;
}

Pointer as function parameter: (M)


#include <stdio.h>

void square(int *num) {


*num = (*num) * (*num);
}

int main() {
int x = 5;
square(&x);

printf("Square: %d\n", x);


return 0;
}

String manipulation using pointers: (L)


#include <stdio.h>

int main() {
char str[] = "Hello, World!";
char *ptr = str;

while (*ptr != '\0') {


printf("%c", *ptr);
ptr++;
}

return 0;
}

Pointer and Function:


Function Pointer with Parameters:
#include <stdio.h>

// Function that adds two numbers


int add(int a, int b) {
return a + b;
}

int main() {
// Declare a pointer to a function that takes two ints and returns an int
int (*ptr)(int, int) = add;

// Call the function using the pointer


int result = ptr(3, 4);

// Print the result


printf("Result: %d\n", result);

return 0;
}

Array of Function Pointers:


#include <stdio.h>

// Function to add two numbers


int add(int a, int b) {
return a + b;
}

// Function to subtract two numbers


int subtract(int a, int b) {
return a - b;
}

int main() {
// Declare an array of pointers to functions with the same signature
int (*ptr[2])(int, int) = {add, subtract};

// Call the functions using the array of pointers


printf("Addition result: %d\n", ptr[0](5, 3));
printf("Subtraction result: %d\n", ptr[1](5, 3));

return 0;
}

Function Pointer as a Parameter:


#include <stdio.h>

// Function that takes a function pointer as a parameter


void performOperation(int (*operation)(int, int), int a, int b) {
int result = operation(a, b);
printf("Result: %d\n", result);
}

// Functions for different operations


int add(int a, int b) {
return a + b;
}

int subtract(int a, int b) {


return a - b;
}

int main() {
// Call the performOperation function with different operations
performOperation(add, 8, 3);
performOperation(subtract, 8, 3);

return 0;
}

pointer to string:
#include <stdio.h>

int main() {
// Declare a pointer to a string (array of characters)
char *message = "Hello, World!";

// Print the string using the pointer


printf("Message: %s\n", message);

// Modify the string through the pointer


message = "Modified String";
printf("Modified Message: %s\n", message);

return 0;
}

C program on malloc()
#include <stdio.h>
#include <stdlib.h>

int main() {
int n;

// Get the size of the array from the user


printf("Enter the size of the array: ");
scanf("%d", &n);

// Dynamically allocate memory for an array of integers


int *dynamicArray = (int *)malloc(n * sizeof(int));

// Check if memory allocation is successful


if (dynamicArray == NULL) {
printf("Memory allocation failed.\n");
return 1; // Exit with an error code
}

// Input elements into the dynamically allocated array


printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &dynamicArray[i]);
}

// Display the elements of the dynamically allocated array


printf("Elements in the dynamically allocated array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", dynamicArray[i]);
}

// Deallocate the dynamically allocated memory


free(dynamicArray);

return 0;
}

C program on calloc()

#include <stdio.h>
#include <stdlib.h>

int main() {
int n;

// Get the size of the array from the user


printf("Enter the size of the array: ");
scanf("%d", &n);

// Dynamically allocate memory for an array of integers using calloc


int *dynamicArray = (int *)calloc(n, sizeof(int));

// Check if memory allocation is successful


if (dynamicArray == NULL) {
printf("Memory allocation failed.\n");
return 1; // Exit with an error code
}

// Input elements into the dynamically allocated array


printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &dynamicArray[i]);
}

// Display the elements of the dynamically allocated array


printf("Elements in the dynamically allocated array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", dynamicArray[i]);
}

// Deallocate the dynamically allocated memory


free(dynamicArray);

return 0;
}

C program on realloc()

#include <stdio.h>
#include <stdlib.h>

int main() {
int n, new_size;

// Get the initial size of the array from the user


printf("Enter the initial size of the array: ");
scanf("%d", &n);

// Dynamically allocate memory for an array of integers


int *dynamicArray = (int *)malloc(n * sizeof(int));

// Check if memory allocation is successful


if (dynamicArray == NULL) {
printf("Memory allocation failed.\n");
return 1; // Exit with an error code
}

// Input elements into the dynamically allocated array


printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &dynamicArray[i]);
}

// Display the elements of the dynamically allocated array


printf("Elements in the dynamically allocated array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", dynamicArray[i]);
}

// Ask the user for the new size of the array


printf("\nEnter the new size of the array: ");
scanf("%d", &new_size);

// Resize the dynamically allocated array using realloc


int *resizedArray = (int *)realloc(dynamicArray, new_size * sizeof(int));

// Check if reallocation is successful


if (resizedArray == NULL) {
printf("Memory reallocation failed.\n");
free(dynamicArray); // Free the initial memory allocation
return 1; // Exit with an error code
}

// Update the pointer to the resized array


dynamicArray = resizedArray;

// Input additional elements into the resized array


printf("Enter %d additional elements:\n", new_size - n);
for (int i = n; i < new_size; i++) {
scanf("%d", &dynamicArray[i]);
}

// Display the elements of the resized array


printf("Elements in the resized array:\n");
for (int i = 0; i < new_size; i++) {
printf("%d ", dynamicArray[i]);
}

// Deallocate the dynamically allocated memory


free(dynamicArray);

return 0;
}

C program on free()

#include <stdio.h>
#include <stdlib.h>

int main() {
int n;

// Get the size of the array from the user


printf("Enter the size of the array: ");
scanf("%d", &n);

// Dynamically allocate memory for an array of integers


int *dynamicArray = (int *)malloc(n * sizeof(int));

// Check if memory allocation is successful


if (dynamicArray == NULL) {
printf("Memory allocation failed.\n");
return 1; // Exit with an error code
}

// Input elements into the dynamically allocated array


printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &dynamicArray[i]);
}

// Display the elements of the dynamically allocated array


printf("Elements in the dynamically allocated array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", dynamicArray[i]);
}

// Deallocate the dynamically allocated memory using free


free(dynamicArray);

// The pointer dynamicArray is now invalid, do not use it anymore

return 0;
}

Pointer to Structure

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Define a structure for a person


struct Person {
char name[50];
int age;
float height;
};

int main() {
// Declare a variable of type struct Person
struct Person person1;

// Declare a pointer to struct Person


struct Person *ptrPerson;

// Initialize the pointer to point to person1


ptrPerson = &person1;

// Access and modify members using the pointer


strcpy(ptrPerson->name, "John");
ptrPerson->age = 30;
ptrPerson->height = 6.0;

// Access members using the pointer


printf("Person's name: %s\n", ptrPerson->name);
printf("Person's age: %d\n", ptrPerson->age);
printf("Person's height: %.2f\n", ptrPerson->height);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy