1. C Language Examples of Array Pointers, Pointer Arrays, Function Pointers, and Pointer Functions
Pointer Array
An array where each element is a pointer is called a pointer array.
int *ptr_arr[10];
#include <stdio.h>
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {6, 7, 8, 9, 0};
int arr3[] = {1, 2, 3, 4, 5};
int* ptr_arr[] = {arr1, arr2, arr3};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
printf("%d", *(ptr_arr[i] + j));
}
printf("\n");
}
return 0;
}
Array Pointer
An array pointer points to an array and stores its address.
int (*ptr)[10];
#include <stdio.h>
int main() {
int matrix[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int(*array_ptr)[4];
array_ptr = matrix;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%2d ", *(*(array_ptr + i) + j));
}
printf("\n");
}
return 0;
}
Function Pointer
A function pointer refers to a function.
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int main() {
int (*func_ptr)(int, int);
func_ptr = add;
printf("Addition: %d\n", func_ptr(1, 2));
func_ptr = subtract;
printf("Subtraction: %d\n", func_ptr(2, 1));
return 0;
}
Pointer Function
A functon that returns a pointer to another function.
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int(*get_add_func())(int, int) {
return add;
}
int(*get_func_by_choice(int choice))(int, int) {
if (choice == 1) {
return subtract;
}
return add;
}
int main() {
int(*func_ptr)(int, int);
func_ptr = get_add_func();
printf("%d\n", func_ptr(2, 1));
func_ptr = get_func_by_choice(1);
printf("%d\n", func_ptr(2, 1));
return 0;
}
2. Implementing a Singly Linked List in C
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node, *LinkedList;
// Initialize a node
void init_node(LinkedList* head) {
*head = (Node*)malloc(sizeof(Node));
(*head)->next = NULL;
}
// Insert at the beginning
void insert_front(LinkedList* head) {
Node* new_node;
int value, count;
printf("Enter number of elements to insert: ");
scanf("%d", &count);
printf("Enter values:\n");
for (int i = 0; i < count; i++) {
new_node = (Node*)malloc(sizeof(Node));
scanf("%d", &value);
new_node->data = value;
new_node->next = (*head)->next;
(*head)->next = new_node;
}
}
// Insert at the end
void insert_back(LinkedList* head) {
Node* new_node, *tail;
int value, count;
tail = *head;
printf("Enter number of elements to insert: ");
scanf("%d", &count);
printf("Enter values:\n");
for (int i = 0; i < count; i++) {
new_node = (Node*)malloc(sizeof(Node));
scanf("%d", &value);
new_node->data = value;
tail->next = new_node;
tail = new_node;
}
tail->next = NULL;
}
// Display list
void display_list(LinkedList head) {
Node* current = head->next;
printf("List contents:\n");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// Delete a node by position
void delete_node(LinkedList* head) {
int pos, index = 0;
printf("Enter position to delete: ");
scanf("%d", &pos);
Node* current = *head;
while (current != NULL && index < pos - 1) {
current = current->next;
index++;
}
if (current == NULL || current->next == NULL) {
printf("Invalid position.\n");
return;
}
Node* temp = current->next;
current->next = temp->next;
free(temp);
}
3. Definition and Usage Example of Delegates in C#
In C#, a delegate is a reference type that holds a reference to a method. It's similar to pointers in C/C++, but more type-safe.
using System;
public delegate void GreetingDelegate();
class Program {
static void Main() {
GreetingDelegate greeting = new GreetingDelegate(SayHello);
greeting += SayWorld;
greeting();
Console.WriteLine("--------");
greeting -= SayWorld;
greeting();
}
static void SayHello() => Console.WriteLine("Hello");
static void SayWorld() => Console.WriteLine("World");
}
4. Functional Definitions and Examples of Action, Func, and Predicate
Action
Represents a method with no parameters and no return value.
using System;
void Greet() {
Console.WriteLine("Greetings!");
}
Action greetAction = Greet;
greetAction();
Func
Represents a method that takes no parameters and returns a value of type T.
using System;
int GetNumber() {
return 42;
}
Func<int> numberFunc = GetNumber;
Console.WriteLine(numberFunc());
Predicate
Represents a method that accepts one paramter and returns a boolean.
using System;
bool IsGreaterThanZero(int num) {
return num > 0;
}
Predicate<int> predicate = IsGreaterThanZero;
Console.WriteLine(predicate(5)); // True
5. Reflection: Can Func Fully Replace Predicate?
No, Func cannot fully replace Predicate. Predicate<T> is a specialized version of Func<T, bool> designed specifically for filetring operations, offering better semantics and clarity in use cases involving conditions or tests.