<p>I have some code with multiple functions very similar to each other to look up an item in a list based on the contents of one field in a structure.  The only difference between the functions is the type of the structure that the look up is occurring in.  If I could pass in the type, I could remove all the code duplication.</p>

<p>I also noticed that there is some mutex locking happening in these functions as well, so I think I might leave them alone...</p>

[{'answer_id': 67809, 'author': 'JohnnyLambada', 'author_id': 9648, 'author_profile': 'https://Stackoverflow.com/users/9648', 'pm_score': 0, 'selected': False, 'text': '<p>One way to do this is to have a type field as the first byte of the structure.  Your receiving function looks at this byte and then casts the pointer to the correct type based on what it discovers.  Another approach is to pass the type information as a separate parameter to each function that needs it. </p>\n'}, {'answer_id': 67824, 'author': 'foxxtrot', 'author_id': 10369, 'author_profile': 'https://Stackoverflow.com/users/10369', 'pm_score': 2, 'selected': False, 'text': "<p>Since structures are nothing more than predefined blocks of memory, you can do this.  You could pass a void * to the structure, and an integer or something to define the type.</p>\n\n<p>From there, the <em>safest</em> thing to do would be to recast the void * into a pointer of the appropriate type before accessing the data.</p>\n\n<p>You'll need to be very, very careful, as you lose type-safety when you cast to a void * and you can likely end up with a difficult to debug runtime error when doing something like this.</p>\n"}, {'answer_id': 67838, 'author': 'Charles Graham', 'author_id': 7705, 'author_profile': 'https://Stackoverflow.com/users/7705', 'pm_score': -1, 'selected': False, 'text': '<p>I\'m a little rusty on c, but try using a void* pointer as the variable type in the function parameter.  Then pass the address of the structure to the function, and then use it he way that you would.</p>\n\n<pre><code>void foo(void* obj);\n\nvoid main()\n{\n  struct bla obj;\n  ...\n  foo(&amp;obj);\n  ...\n}\n\nvoid foo(void* obj)\n{\n  printf(obj -&gt; x, "%s")\n}\n</code></pre>\n'}, {'answer_id': 67842, 'author': 'tialaramex', 'author_id': 9654, 'author_profile': 'https://Stackoverflow.com/users/9654', 'pm_score': 3, 'selected': False, 'text': '<p>If you ensure that the field is placed in the same place in each such structure, you can simply cast a pointer to get at the field. This technique is used in lots of low level system libraries e.g. BSD sockets.</p>\n\n<pre><code>struct person {\n  int index;\n};\n\nstruct clown {\n  int index;\n  char *hat;\n};\n\n/* we\'re not going to define a firetruck here */\nstruct firetruck;\n\n\nstruct fireman {\n  int index;\n  struct firetruck *truck;\n};\n\nint getindexof(struct person *who)\n{\n  return who-&gt;index;\n}\n\nint main(int argc, char *argv[])\n{\n  struct fireman sam;\n  /* somehow sam gets initialised */\n  sam.index = 5;\n\n  int index = getindexof((struct person *) &amp;sam);\n  printf("Sam\'s index is %d\\n", index);\n\n  return 0;\n}\n</code></pre>\n\n<p>You lose type safety by doing this, but it\'s a valuable technique.</p>\n\n<p>[ I have now actually tested the above code and fixed the various minor errors. It\'s much easier when you have a compiler. ]</p>\n'}, {'answer_id': 67903, 'author': 'Gordon Wrigley', 'author_id': 10471, 'author_profile': 'https://Stackoverflow.com/users/10471', 'pm_score': 0, 'selected': False, 'text': '<p>You can do this with a parameterized macro but most coding policies will frown on that.</p>\n\n<pre><code>\n#include \n#define getfield(s, name) ((s).name)\n\ntypedef struct{\n  int x;\n}Bob;\n\ntypedef struct{\n  int y;\n}Fred;\n\nint main(int argc, char**argv){\n    Bob b;\n    b.x=6;\n\n    Fred f;\n    f.y=7;\n\n    printf("%d, %d\\n", getfield(b, x), getfield(f, y));\n}\n</code></pre>\n'}, {'answer_id': 68003, 'author': 'gnkdl_gansklgna', 'author_id': 10470, 'author_profile': 'https://Stackoverflow.com/users/10470', 'pm_score': 0, 'selected': False, 'text': "<p>Short answer: no. You can, however, create your own method for doing so, i.e. providing a specification for how to create such a struct. However, it's generally not necessary and is not worth the effort; just pass by reference. (<code>callFuncWithInputThenOutput(input, &amp;struct.output);</code>)</p>\n"}, {'answer_id': 81113, 'author': 'Jonathan Leffler', 'author_id': 15168, 'author_profile': 'https://Stackoverflow.com/users/15168', 'pm_score': 1, 'selected': False, 'text': '<p>I think you should look at the C standard functions qsort() and bsearch() for inspiration.  These are general purpose code to sort arrays and to search for data in a pre-sorted array.  They work on any type of data structure - but you pass them a pointer to a helper function that does the comparisons.  The helper function knows the details of the structure, and therefore does the comparison correctly.</p>\n\n<p>In fact, since you are wanting to do searches, it may be that all you need is bsearch(), though if you are building the data structures on the fly, you may decide you need a different structure than a sorted list.  (You can use sorted lists -- it just tends to slow things down compared with, say, a heap.  However, you\'d need a general heap_search() function, and a heap_insert() function, to do the job properly, and such functions are not standardized in C.   Searching the web shows such functions exist - not by that name; just do not try "c heap search" since it is assumed you meant "cheap search" and you get tons of junk!)</p>\n'}, {'answer_id': 96369, 'author': 'fizzer', 'author_id': 18167, 'author_profile': 'https://Stackoverflow.com/users/18167', 'pm_score': 1, 'selected': False, 'text': '<p>If the ID field you test is part of a common initial sequence of fields shared by all the structs, then using a union guarantees that the access will work:</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\ntypedef struct\n{\n    int id;\n    int junk1;\n} Foo;\n\ntypedef struct\n{\n    int id;\n    long junk2;\n} Bar;\n\ntypedef union\n{\n    struct\n    {\n        int id;\n    } common;\n\n    Foo foo;\n    Bar bar;\n} U;\n\nint matches(const U *candidate, int wanted)\n{\n    return candidate-&gt;common.id == wanted;\n}\n\nint main(void)\n{\n    Foo f = { 23, 0 };\n    Bar b = { 42, 0 };\n\n    U fu;\n    U bu;\n\n    fu.foo = f;\n    bu.bar = b;\n\n    puts(matches(&amp;fu, 23) ? "true" : "false");\n    puts(matches(&amp;bu, 42) ? "true" : "false");\n\n    return 0;\n}\n</code></pre>\n\n<p>If you\'re unlucky, and the field appears at different offsets in the various structs, you can add an offset parameter to your function.  Then, offsetof and a wrapper macro simulate what the OP asked for - passing the type of struct at the call site:</p>\n\n<pre><code>#include &lt;stddef.h&gt;\n#include &lt;stdio.h&gt;\n\ntypedef struct\n{\n    int id;\n    int junk1;\n} Foo;\n\ntypedef struct\n{\n    int junk2;\n    int id;\n} Bar;\n\nint matches(const void* candidate, size_t idOffset, int wanted)\n{\n    return *(int*)((const unsigned char*)candidate + idOffset) == wanted;\n}\n\n#define MATCHES(type, candidate, wanted) matches(candidate, offsetof(type, id), wanted)\n\nint main(void)\n{\n    Foo f = { 23, 0 };\n    Bar b = { 0, 42 };\n    puts(MATCHES(Foo, &amp;f, 23) ? "true" : "false");\n    puts(MATCHES(Bar, &amp;b, 42) ? "true" : "false");\n\n    return 0;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/67790', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10394/']