+ 1

Generic Variable

If the variable "int" was changed to "void" in the typedef int data within the list_node struct, could the linked list then be used for any variable type? https://code.sololearn.com/cpyZR1nTZBiD/?ref=app

3rd Jan 2023, 12:43 AM
William Owens
William Owens - avatar
1 Respuesta
+ 2
If I understand the question correctly, you are asking if we change typedef int ll_data_t; to typedef void ll_data_t; would we be able to use the linked list with any type? The answer to that would be, no. I think you might be thinking of 'void pointers' which can point to any type. But even with them, "generics in C" are not possible. void pointers don't have any information about the type they are pointing to, so it would be impossible to access the data that the pointer points to (since we don't know how many bytes to read from that memory location, and how to interpret it as data). It can be related to the 'object' type in other languages. Just because you can assign anything to them, doesn't mean they can be used as generics, since generics always refer to a specific but non-specified type
3rd Jan 2023, 7:09 AM
XXX
XXX - avatar