Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-235b-a22b-no-thinking· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

what is generic pointers in c

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Metallama-4-maverick-17b-128e-instruct’s answer.
  1. User

    what is generic pointers in c

  2. Qwen Assistant

    In C programming, a generic pointer is a pointer that can point to any data type. This concept is implemented using the void pointer (void *).


    🔹 What is a void Pointer?

    A void * pointer (also known as a generic pointer) is a special type of pointer that can hold the address of any data type.

    Example:

    int a = 10;
    float b = 3.14;
    
    void *ptr;
    
    ptr = &a;  // ptr points to integer
    ptr = &b;  // ptr now points to float
    

    Here, the same ptr can point to both an int and a float.


    🔸 Why Use Generic Pointers?

    Generic pointers are useful when you want to write functions or data structures that work with multiple data types, such as:

    • Memory allocation functions like malloc(), calloc() which return a void*.
    • Implementing generic data structures like linked lists, stacks, queues, etc.
    • Function pointers where the function may need to operate on different types of data.

    🔺 Important Points About void*

    1. Cannot Dereference Directly: You cannot dereference a void * pointer directly because the compiler doesn't know what type it's pointing to.

      ❌ Invalid:

      void *ptr = &a;
      printf("%d", *ptr);  // Error: invalid use of void expression
      

      ✅ Correct:

      int a = 10;
      void *ptr = &a;
      printf("%d", *(int *)ptr);  // Typecast before dereferencing
      
    2. No Pointer Arithmetic: Since void has no size, you cannot perform arithmetic operations like ptr++ on a void *.

      ❌ Invalid:

      ptr++;
      

      ✅ But this works:

      char *cptr = (char *)ptr;
      cptr++;
      

    🔹 Use Case: Generic Functions

    You can use void * in functions to make them accept any pointer type.

    Example:

    #include <stdio.h>
    
    void printValue(void *ptr, char type) {
        switch(type) {
            case 'i':
                printf("Integer value: %d\n", *(int *)ptr);
                break;
            case 'f':
                printf("Float value: %f\n", *(float *)ptr);
                break;
            case 'c':
                printf("Char value: %c\n", *(char *)ptr);
                break;
        }
    }
    
    int main() {
        int i = 10;
        float f = 3.14;
        char c = 'A';
    
        printValue(&i, 'i');
        printValue(&f, 'f');
        printValue(&c, 'c');
    
        return 0;
    }
    

    ✅ Summary

    FeatureDescription
    Typevoid *
    Can point toAny data type
    Can be assigned fromAny pointer type
    Can it be dereferenced?No (needs typecasting first)
    Pointer arithmeticNot allowed directly

    Let me know if you'd like an example of how void * is used in dynamic memory allocation or generic linked lists!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.