Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Custom String Operations and Advanced Pointer Concepts in C

Tech May 17 1

Custom String Functions

Implementations of common string operatiosn without standard library functions.

#include <stdio.h>
#include <stddef.h>

// Calculate string length
size_t custom_strlen(const char *str) {
    size_t len = 0;
    while (str[len] != '\0') {
        len++;
    }
    return len;
}

// Compare two strings
int custom_strcmp(const char *a, const char *b) {
    while (*a && *b && *a == *b) {
        a++;
        b++;
    }
    return *(const unsigned char*)a - *(const unsigned char*)b;
}

// Copy string from source to destination
char *custom_strcpy(char *dest, const char *src) {
    char *start = dest;
    while ((*dest++ = *src++)) {}
    return start;
}

// Concatenate source string to destination
char *custom_strcat(char *dest, const char *src) {
    char *ptr = dest + custom_strlen(dest);
    while ((*ptr++ = *src++)) {}
    return dest;
}

// Find substring in a string
char *custom_strstr(const char *haystack, const char *needle) {
    if (!*needle) return (char*)haystack;
    for (; *haystack; haystack++) {
        const char *h = haystack;
        const char *n = needle;
        while (*h && *n && *h == *n) {
            h++;
            n++;
        }
        if (!*n) return (char*)haystack;
    }
    return NULL;
}

int main() {
    char text1[50] = "Hello World";
    char text2[] = "Programming";
    
    printf("Length of text1: %zu\n", custom_strlen(text1));
    
    int cmp = custom_strcmp(text1, text2);
    if (cmp > 0) printf("'%s' is greater\n", text1);
    else if (cmp < 0) printf("'%s' is less\n", text1);
    else printf("Strings are equal\n");
    
    custom_strcpy(text1, text2);
    printf("After copy: text1='%s', text2='%s'\n", text1, text2);
    
    char *found = custom_strstr(text1, "gram");
    if (found) printf("Substring found: %s\n", found);
    
    custom_strcat(text1, " is fun!");
    printf("After concatenation: text1='%s'\n", text1);
    
    return 0;
}

Array Pointers and 2D Arrays

An array pointer holds the address of an entire array. When incremented, it moves by the size of the array.

#include <stdio.h>

int main() {
    int grid[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
    
    printf("Address of grid[0][0]: %p\n", (void*)&grid[0][0]);
    printf("Value of grid[0]: %p\n", (void*)grid[0]);
    printf("Value of grid: %p\n", (void*)grid);
    
    printf("After incrementing by 1:\n");
    printf("&grid[0][0]+1: %p\n", (void*)(&grid[0][0]+1));
    printf("grid[0]+1: %p\n", (void*)(grid[0]+1));
    printf("grid+1: %p\n", (void*)(grid+1));
    
    int (*ptr)[4] = grid; // Pointer to an array of 4 integers
    
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            printf("%d\t", *(*(ptr + i) + j));
        }
        printf("\n");
    }
    
    return 0;
}

Pointer Arrays

A pointer array is an array where each element is a pointer.

Definition: data_type *array_name[size];

Functions Returning Pointers

These functions return a memory address. The returned address should point to static, global, dynamically allocated, or caller-provided memory.

Definition: return_type *function_name(parameters);

Example:

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

#define BUFFER_SIZE 100

char *create_message(const char *name) {
    static char buffer[BUFFER_SIZE];
    snprintf(buffer, BUFFER_SIZE, "Greetings, %s!", name);
    return buffer;
}

int main() {
    char *msg = create_message("Developer");
    printf("%s\n", msg);
    return 0;
}

Function Pointers and Callbacks

A function pointer stores the address of a function. It enables callbacks, where a function is past as an argument.

Definition: return_type (*pointer_name)(parameter_types);

Example:

#include <stdio.h>

int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }

void calculate(int x, int y, int (*op_func)(int, int)) {
    printf("Result: %d\n", op_func(x, y));
}

int main() {
    int a = 20, b = 8;
    
    calculate(a, b, add);
    calculate(a, b, subtract);
    calculate(a, b, multiply);
    
    return 0;
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.