Unit 2 Prractice Programs
Unit 2 Prractice Programs
#include <stdio.h>
*a = *b;
*b = temp;
int main() {
int x = 5, y = 10;
swap(&x, &y);
return 0;
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
return 0;
}
Pointer arithmetic: (L)
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;
return 0;
}
int main() {
int *ptr;
ptr = (int *)malloc(sizeof(int));
*ptr = 10;
free(ptr);
return 0;
}
int main() {
int x = 5;
int *ptr = multiplyByTwo(x);
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptrArr[5];
return 0;
}
int main() {
int (*ptr)(int, int) = &add;
int main() {
int x = 5;
square(&x);
int main() {
char str[] = "Hello, World!";
char *ptr = str;
return 0;
}
int main() {
// Declare a pointer to a function that takes two ints and returns an int
int (*ptr)(int, int) = add;
return 0;
}
int main() {
// Declare an array of pointers to functions with the same signature
int (*ptr[2])(int, int) = {add, subtract};
return 0;
}
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!";
return 0;
}
C program on malloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
return 0;
}
C program on calloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
return 0;
}
C program on realloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, new_size;
return 0;
}
C program on free()
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
return 0;
}
Pointer to Structure
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
// Declare a variable of type struct Person
struct Person person1;
return 0;
}