Usefulness of void pointer in C and C++

I know that void type means no value at all https://www.gnu.org/software/c-intro-and-ref/manual/html_node/The-Void-Type.html in C. But, I have seen some code in C in which some parameter variables were passed as void pointer :

return_type my_function_signature(void* param1, int num_count, double real_value){
      type_to_cast_to var_param1 = (type_to_cast_to *)param1;
      // write something here
    enter code here
    return return_value;

}

I wonder, what’s the use of having void* param1 in parameter list, when in the body of the function one knows exactly the type to cast ?

It’s not like template functions in C++ https://en.cppreference.com/w/cpp/language/variable_template.

Any help from the connoisseurs of C and C++ ?

18

Using a void * as a parameter can be useful for callback functions, where the exact type of the parameter isn’t necessarily known to the function that takes the callback, but is known to the programmer who instantiates it.

It can also be useful when working on generic data when the exact type isn’t known.

A well-known example of both is the qsort function from the standard library which has the following declaration:

   void qsort(void *base, size_t nmemb, size_t size,
              int (*compar)(const void *, const void *));

Here, the base parameter points to the start of an array, while the function pointer compar takes pointers to two objects to compare. In both cases the qsort function does not know what the array/element type is.

Suppose you wanted to sort an array of int and an array of float. You would create the following callback functions:

int comp_int(const void *p1, const void *p2)
{
    const int *i1 = p1;
    const int *i2 = p2;

    if (*i1 < *i2) {
        return -1;
    } else if (*i1 > *i2) {
        return 1;
    } else {
        return 0;
    }
}

int comp_float(const void *p1, const void *p2)
{
    const float *f1 = p1;
    const float *f2 = p2;

    if (*f1 < *f2) {
        return -1;
    } else if (*f1 > *f2) {
        return 1;
    } else {
        return 0;
    }
}

And the qsort function would be called like this:

int a1[5] = { ... };
float a2[10] = { ... };
qsort(a1, 5, sizeof *a1, comp_int);
qsort(a2, 10, sizeof *a2, comp_float);

I wonder, what’s the use of having void* param1 in parameter list, when in the body of the function one knows exactly the type to cast ?

There’s little, if any use for that. If the function is expecting a specific pointer type then I can’t think of a situation where it is not better for it to declare the parameter as that specific type, instead of as type void *.

Instead, the use of a parameter or return value of type void * is when the function doesn’t know the particular type to which the pointer will point, and doesn’t depend on that. In C++, many of those cases are better handled by templating the type or by using a polymorphic type, but those alternatives are not available in C.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật