# stddef

***

**1. Array Size Computation:**

```c
#include <stddef.h>

int main() {
    int arr[5];
    size_t size = sizeof(arr) / sizeof(arr[0]);
    printf("Array size: %zu\n", size);
    return 0;
}
```

**2. Pointer Arithmetic:**

```c
#include <stddef.h>

int main() {
    int* ptr = malloc(sizeof(int) * 5);
    ptr += 2;  // Move the pointer to the third element
    printf("Value at third element: %d\n", *ptr);
    free(ptr);
    return 0;
}
```

**3. Null Pointer Checking:**

```c
#include <stddef.h>

int main() {
    int* ptr = NULL;
    if (ptr == NULL) {
        printf("Pointer is null\n");
    } else {
        printf("Pointer is not null\n");
    }
    return 0;
}
```

**4. Offsets in Structures:**

```c
#include <stddef.h>

struct Point {
    int x;
    int y;
};

int main() {
    struct Point pt;
    printf("Offset of x: %zu\n", offsetof(struct Point, x));
    printf("Offset of y: %zu\n", offsetof(struct Point, y));
    return 0;
}
```

**5. Enumeration Size:**

```c
#include <stddef.h>

enum Color {
    RED,
    GREEN,
    BLUE
};

int main() {
    printf("Size of enum Color: %zu\n", sizeof(enum Color));
    return 0;
}
```

**6. Byte-Oriented Operations:**

```c
#include <stddef.h>

int main() {
    int value = 0x12345678;
    printf("First byte: %02x\n", *(((unsigned char*) &value) + 0));
    printf("Second byte: %02x\n", *(((unsigned char*) &value) + 1));
    return 0;
}
```

**7. Bitwise Operations:**

```c
#include <stddef.h>

int main() {
    int value = 0x12345678;
    printf("Lowest bit: %x\n", value & 1);
    printf("Clear lowest 4 bits: %x\n", value & ~0x0F);
    return 0;
}
```

**8. String Length:**

```c
#include <stddef.h>

int main() {
    char* str = "Hello";
    ptrdiff_t len = strlen(str);
    printf("String length: %td\n", len);
    return 0;
}
```

**9. String Concatenation:**

```c
#include <stddef.h>

int main() {
    char* str1 = "Hello";
    char* str2 = "World";
    size_t len = strlen(str1) + 1 + strlen(str2) + 1;  // Include null-terminator
    char* result = malloc(len);
    strcpy(result, str1);
    strcat(result, " ");
    strcat(result, str2);
    printf("Concatenated string: %s\n", result);
    free(result);
    return 0;
}
```

**10. Memory Allocation and Deallocation:**

```c
#include <stddef.h>

int main() {
    int* ptr = malloc(sizeof(int) * 5);  // Allocate memory for an array of 5 integers
    if (ptr != NULL) {
        ptr[0] = 1;  // Initialize the first element
        free(ptr);  // Deallocate the memory
    }
    return 0;
}
```

**11. Dynamic Array:**

```c
#include <stddef.h>

struct DynamicArray {
    int* data;
    size_t size;
    size_t capacity;
};

int main() {
    struct DynamicArray* array = malloc(sizeof(struct DynamicArray));
    array->data = NULL;
    array->size = 0;
    array->capacity = 0;
    
    // ... Operations on the dynamic array ...
    
    free(array->data);
    free(array);
    return 0;
}
```

**12. Linked List:**

```c
#include <stddef.h>

struct Node {
    int data;
    struct Node* next;
};

int main() {
    struct Node* head = NULL;
    
    // ... Operations on the linked list ...
    
    while (head != NULL) {
        struct Node* temp = head;
        head = head->next;
        free(temp);
    }
    return 0;
}
```

**13. Binary Search Tree:**

```c
#include <stddef.h>

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

int main() {
    struct Node* root = NULL;
    
    // ... Operations on the binary search tree ...
    
    // Deallocate the tree
    freeTree(root);
}

void freeTree(struct Node* root) {
    if (root == NULL) {
        return;
    }
    freeTree(root->left);
    freeTree(root->right);
    free(root);
}
```

**14. Graph:**

```c
#include <stddef.h>

struct Node {
    int data;
    struct Node** neighbors;
    size_t num_neighbors;
};

int main() {
    struct Node** nodes = malloc(sizeof(struct Node*) * 5);  // Array of 5 nodes
    
    // ... Operations on the graph ...
    
    for (size_t i = 0; i < 5; i++) {
        free(nodes[i]->neighbors);
        free(nodes[i]);
    }
    free(nodes);
    return 0;
}
```

**15. Hash Table:**

```c
#include <stddef.h>

struct HashTable {
    size_t size;
    struct Node** buckets;
};

struct Node {
    int key;
    int value;
    struct Node* next;
};

int main() {
    struct HashTable* table = createHashTable(10);  // Create a hash table with 10 buckets
    
    // ... Operations on the hash table ...
    
    for (size_t i = 0; i < table->size; i++) {
        struct Node* node = table->buckets[i];
        while (node != NULL) {
            free(node);
            node = node->next;
        }
    }
    free(table->buckets);
    free(table);
    return 0;
}
```

**16. Stack:**

```c
#include <stddef.h>

struct Stack {
    int* data;
    size_t size;
    size_t capacity;
};

int main() {
    struct Stack* stack = createStack(10);  // Create a stack with a capacity of 10 elements
    
    // ... Operations on the stack ...
    
    free(stack->data);
    free(stack);
    return 0;
}
```

**17. Queue:**

```c
#include <stddef.h>

struct Queue {
    int* data;
    size_t front;
    size_t rear;
    size_t capacity;
};

int main() {
    struct Queue* queue = createQueue(10);  // Create a queue with a capacity of 10 elements
    
    // ... Operations on the queue ...
    
    free(queue->data);
    free(queue);
    return 0;
}
```

**18. Circular Buffer:**

```c
#include <stddef.h>

struct CircularBuffer {
    int* data;
    size_t head;
    size_t tail;
    size_t capacity;
};

int main() {
    struct CircularBuffer* buffer = createCircularBuffer(10);  // Create a circular buffer with a capacity of 10 elements
    
    // ... Operations on the circular buffer ...
    
    free(buffer->data);
    free(buffer);
    return 0;
}
```

**19. Memory Mapping:**

```c
#include <stddef.h>

int main() {
    void* addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (addr != MAP_FAILED) {
        // Perform operations on the mapped memory
        munmap(addr, 4096);
    }
    return 0;
}
```

**20. Shared Memory:**

```c
#include <stddef.h>
#include <sys/shm.h>

int main() {
    int shmid = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
    if (shmid != -1) {
        void* addr = shmat(shmid, NULL, 0);
        if (addr != (void*) -1) {
            // Perform operations on the shared memory
            shmdt(addr);
            shmctl(shmid, IPC_RMID, NULL);
        }
    }
    return 0;
}
```

**21. Thread-Local Storage:**

```c
#include <stddef.h>
#include <pthread.h>

pthread_key_t key;

void cleanup(void* data) {
    // Perform cleanup operations on the thread-local data
    free(data);
}

int main() {
    pthread_key_create(&key, cleanup);
    void* data = malloc(4096);
    pthread_setspecific(key, data);
    
    // Perform operations using the thread-local data
    
    pthread_key_delete(key);
    return 0;
}
```

**22. Function Pointers:**

```c
#include <stddef.h>

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

int main() {
    int (*func)(int, int) = &add;
    int result = func(1, 2);
    printf("Result: %d\n", result);
    return 0;
}
```

**23. Type Casting:**

```c
#include <stddef.h>

int main() {
    int value = 123;
    char* str = (char*) &value;  // Cast the integer to a character array
    printf("First character: %c\n", str[0]);
    return 0;
}
```

**24. Pointer to Member:**

```c
#include <stddef.h>

struct Point {
    int x;
    int y;
};

int main() {
    struct Point* pt = malloc(sizeof(struct Point));
    int Point_x = offsetof(struct Point, x);
    int* pt_x = (int*) ((char*) pt + Point_x);  // Get the pointer to the x member
    *pt_x = 10;
    printf("x: %d\n", pt->x);
    free(pt);
    return 0;
}
```

**25. Pointer to Function:**

```c
#include <stddef.h>

void printHello() {
    printf("Hello\n");
}

int main() {
    void (*func)() = &printHello;  // Get the pointer to the printHello function
    func();  // Call the function using the pointer
    return 0;
}
```

**26. Macros:**

```c
#include <stddef.h>

#define max(a, b) ((a) > (b) ? (a) : (b))

int main() {
    int a = 10;
    int b = 5;
    int max_value = max(a, b);
    printf("Maximum value: %d\n", max_value);
    return 0;
}
```

**27. Bit Manipulation Macros:**

```c
#include <stddef.h>

#define bit_set(value, bit) ((value) |= (1 << (bit)))
#define bit_clear(value, bit) ((value) &= ~(1 << (bit)))

int main() {
    int value = 0x0F;
    bit_set(value, 3);  // Set the 3rd bit to 1
    bit_clear(value, 1);  // Clear the 1st bit to 0
    printf("Bitwise operations result: %x\n", value);
    return 0;
}
```

**28. Conditional Compilation:**

```c
#include <stddef.h>

#ifdef DEBUG
    printf("Debug mode enabled\n");
#else
    printf("Debug mode disabled\n");
#endif

int main() {
    return 0;
}
```

**29. Memory Barriers:**

```c
#include <stddef.h>
#include <stdatomic.h>

int shared_variable = 0;

void thread_1() {
    shared_variable = 1;
    atomic_thread_fence(memory_order_release);  // Ensure that the write to shared_variable is visible to other threads
}

void thread_2() {
    atomic_thread_fence(memory_order_acquire);  // Ensure that the read from shared_variable reflects the latest value written by other threads
    int value = shared_variable;
    printf("Value read from shared_variable: %d\n", value);
}

int main() {
    // ... Create and run the threads ...
    return 0;
}
```

**30. Aligned Memory Allocation:**

```c
#include <stddef.h>

int main() {
    void* ptr = aligned_alloc(4096, sizeof(int) * 100);  // Allocate memory aligned to 4096 bytes
    if (ptr != NULL) {
        // Perform operations on the aligned memory
        free(ptr);
    }
    return 0;
}
```

**31. Non-Null Assertions:**

```c
#include <stddef.h>

int main() {
    int* ptr = NULL;
    ptr = nonnull(ptr);  // Non-null assertion: ensure that ptr is not NULL
    *ptr = 10;
    printf("Value pointed to by ptr: %d\n", *ptr);
    return 0;
}
```

**32. Pointer Comparisons:**

```c
#include <stddef.h>

int main() {
    int* ptr1 = NULL;
    int* ptr2 = NULL;
    int* ptr3 = ptr2;  // Both ptr2 and ptr3 point to the same location
    
    if (ptr1 == NULL) {
        printf("ptr1 is null\n");
    }
    if (ptr2 == ptr3) {
        printf("ptr2 and ptr3 point to the same location\n");
    }
    return 0;
}
```

**33. Pointer Arithmetic with Arrays:**

```c
#include <stddef.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int* ptr = arr;  // Pointer to the first element of the array
    
    ptr += 2;  // Move the pointer to the third element
    printf("Value at the third element: %d\n", *ptr);
    return 0;
}
```

**34. Pointer Decrement**:

```c
#include <stddef.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int* ptr = &arr[4];  // Pointer to the last element of the array
    
    ptr--;  // Move the pointer to the previous element
    printf("Value at the fourth element: %d\n", *ptr);
    return 0;
}
```

**35. Dereferencing Pointers:**

```c
#include <stddef.h>

int main() {
    int* ptr = malloc(sizeof(int));
    *ptr = 10;  // Dereference the pointer to assign a value
    printf("Value pointed to by ptr: %d\n", *ptr);
    return 0;
}
```

**36. Pointer Initialization:**

```c
#include <stddef.h>

int main() {
    int* ptr = NULL;  // Initialize a pointer to null
    ptr = malloc(sizeof(int));  // Allocate memory and assign it to the pointer
    *ptr = 10;  // Dereference the pointer to assign a value
    printf("Value pointed to by ptr: %d\n", *ptr);
    return 0;
}
```

**37. Pointer Reassignment:**

```c
#include <stddef.h>

int main() {
    int* ptr = malloc(sizeof(int));
    *ptr = 10;  // Assign a value to the pointer
    int* ptr2 = ptr;  // Reassign the pointer
    *ptr2 = 20;  // Change the value through the reassigned pointer
    printf("Value pointed to by ptr: %d\n", *ptr);
    return 0;
}
```

**38. Pointer Casting**:

```c
#include <stddef.h>

int main() {
    int* ptr = malloc(sizeof(int));
    char* char_ptr = (char*) ptr;  // Cast the pointer to a character pointer
    char_ptr[0] = 'A';  // Access the memory through the casted pointer
    printf("Character stored at the memory location: %c\n", char_ptr[0]);
    return 0;
}
```

**39. Pointer Comparison:**

```c
#include <stddef.h>

int main() {
    int* ptr1 = malloc(sizeof(int));
    int* ptr2 = malloc(sizeof(int));
    if (ptr1 == ptr2) {
        printf("ptr1 and ptr2 point to the same memory location\n");
    } else {
        printf("ptr1 and ptr2 point to different memory locations\n");
    }
    return 0;
}
```

**40. Pointer Arithmetic with Pointers**:

```c
#include <stddef.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int* ptr = arr;
    ptr += 2;  // Move the pointer to the third element
    printf("Value at the third element: %d\n", *ptr);
    return 0;
}
```

**41. Pointer Addition:**

```c
#include <stddef.h>

int main() {
    int* ptr = malloc(sizeof(int));
    int* new_ptr = ptr + 1;  // Add 1 to the pointer to move it to the next memory location
    printf("New memory location: %p\n", new_ptr);
    return 0;
}
```

**42. Pointer Subtraction**:

```c
#include <stddef.h>

int main() {
    int* ptr1 = malloc(sizeof(int));
    int* ptr2 = malloc(sizeof(int));
    ptr2 += 2;  // Move ptr2 to the next memory location
    ptrdiff_t diff = ptr2 - ptr1;  // Calculate the difference between the two pointers
    printf("Difference between the two pointers: %td\n", diff);
    return 0;
}
```

**43. Pointer Increment:**

```c
#include <stddef.h>

int main() {
    int* ptr = malloc(sizeof(int));
    ptr++;  // Increment the pointer to move it to the next memory location
    printf("New memory location: %p\n", ptr);
    return 0;
}
```

**44. Pointer Decrement**:

```c
#include <stddef.h>

int main() {
    int* ptr = malloc(sizeof(int));
    ptr += 2;  // Move the pointer to the next memory location
    ptr--;  // Decrement the pointer to move it back to the previous memory location
    printf("New memory location: %p\n", ptr);
    return 0;
}
```

**45. Pointer Comparison with Null:**

```c
#include <stddef.h>

int main() {
    int* ptr = NULL;
    if (ptr == NULL) {
        printf("Pointer is null\n");
    } else {
        printf("Pointer is not null\n");
    }
    return 0;
}
```

**46. Pointer Dereferencing:**

```c
#include <stddef.h>

int main() {
    int* ptr = malloc(sizeof(int));
    *ptr = 10;  // Dereference the pointer to assign a value
    printf("Value pointed to by ptr: %d\n", *ptr);
    return 0;
}
```

**47. Pointer Type Casting:**

```c
#include <stddef.h>

int main() {
    void* void_ptr = malloc(sizeof(int));
    int* int_ptr = (int*) void_ptr;  // Cast the void pointer to an integer pointer
    *int_ptr = 10;  // Dereference the casted pointer to assign a value
    printf("Value pointed to by int_ptr: %d\n", *int_ptr);
    return 0;
}
```

**48. Using size\_t for Loop Bounds:**

```c
#include <stddef.h>

int main() {
    size_t i;
    for (i = 0; i < 10; i++) {
        // ...
    }
    return 0;
}
```

**49. Pointer-to-Member Function:**

```c
#include <stddef.h>

struct Point {
    int x;
    int y;
};

int main() {
    struct Point* pt = malloc(sizeof(struct Point));
    int Point_x = offsetof(struct Point, x);
    int* pt_x = (int*) ((char*) pt + Point_x);  // Get the pointer to the x member
    *pt_x = 10;
    printf("x: %d\n", pt->x);
    return 0;
}
```

**50. Using ptrdiff\_t for Pointer Arithmetic:**

```c
#include <stddef.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    ptrdiff_t index = 2;
    int value = arr[index];  // Use ptrdiff_t for pointer arithmetic
    printf("Value at index %td: %d\n", index, value);
    return 0;
}
```
