Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

Void Pointer Examples in C

Void Pointer Examples in C

πŸ“˜ What is a Void Pointer in C?

A void pointer in C is a special type of pointer that can store the address of any data type. It is also known as a generic pointer. However, before dereferencing a void pointer, you must explicitly typecast it to the correct type.

πŸ’‘ Example 1: Basic Void Pointer Usage

#include<stdio.h>

int main() {
    void *ptr;
    int a = 10;
    ptr = &a;
    printf("%d", *(int*)ptr);
}

πŸ’‘ Example 2: Void Pointer with Multiple Data Types

#include<stdio.h>

int main() {
    int a = 5;
    float b = 3.14;
    char c = 'X';

    void *ptr;

    ptr = &a;
    printf("Integer: %d\\n", *(int*)ptr);

    ptr = &b;
    printf("Float: %.2f\\n", *(float*)ptr);

    ptr = &c;
    printf("Char: %c\\n", *(char*)ptr);

    return 0;
}

πŸ’‘ Example 3: malloc and Void Pointer

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

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

    if (ptr != NULL) {
        *(int*)ptr = 42;
        printf("Value: %d", *(int*)ptr);
        free(ptr);
    }

    return 0;
}

πŸ’‘ Example 4: Generic Function using void*

#include<stdio.h>

void printValue(void *ptr, char type) {
    if (type == 'i')
        printf("Integer: %d\\n", *(int*)ptr);
    else if (type == 'f')
        printf("Float: %.2f\\n", *(float*)ptr);
    else if (type == 'c')
        printf("Char: %c\\n", *(char*)ptr);
}

int main() {
    int x = 10;
    float y = 5.5;
    char z = 'A';

    printValue(&x, 'i');
    printValue(&y, 'f');
    printValue(&z, 'c');

    return 0;
}

πŸ’‘ Example 5: Array of Void Pointers

#include<stdio.h>

int main() {
    int a = 10;
    float b = 3.14;
    char c = 'Z';

    void *arr[3];

    arr[0] = &a;
    arr[1] = &b;
    arr[2] = &c;

    printf("Integer: %d\\n", *(int*)arr[0]);
    printf("Float: %.2f\\n", *(float*)arr[1]);
    printf("Char: %c\\n", *(char*)arr[2]);

    return 0;
}

πŸ’‘ Example 6: Generic Swap Function Using void*

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

void swap(void *a, void *b, size_t size) {
    char temp[size];
    memcpy(temp, a, size);
    memcpy(a, b, size);
    memcpy(b, temp, size);
}

int main() {
    int x = 10, y = 20;
    swap(&x, &y, sizeof(int));
    printf("x = %d, y = %d\\n", x, y);

    float f1 = 1.1, f2 = 2.2;
    swap(&f1, &f2, sizeof(float));
    printf("f1 = %.1f, f2 = %.1f\\n", f1, f2);

    return 0;
}

πŸ“Œ Hashtags:

#VoidPointer #CProgramming #PointerBasics #GenericPointer #malloc #FunctionPointers #SwapFunction #ArrayOfPointers #TypeCastingInC #AdvancedC

πŸ”‘ Keywords:

void pointer in C, generic pointer in C, pointer typecasting, C pointer examples, malloc with void pointer, function using void pointer, array of void pointers, generic swap function C, dynamic memory C, void pointer real time example

Comments

Popular Posts

πŸŒ™