{"id":3388,"date":"2015-04-05T21:39:01","date_gmt":"2015-04-05T21:39:01","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=3388"},"modified":"2019-04-25T13:08:50","modified_gmt":"2019-04-25T13:08:50","slug":"how-to-create-a-list-that-holds-different-object-types-using-void-in-c","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/how-to-create-a-list-that-holds-different-object-types-using-void-in-c\/","title":{"rendered":"How to create a list that holds different object types using `void*` in C."},"content":{"rendered":"<p>I remember being in school back around 1998 and not knowing enough about C to do this. After coding in other languages, then going back to C++ and understanding at a lower level how references and pointers work, this was pretty easy to figure out.<\/p>\n<p>In this exercise I store elements of different types in a forward linked list.<br \/>\nIn order to know what to dereference as I iterate through the list&#8217;s elements, I&#8217;ve put a &#8220;.type&#8221; field, which has an int value representing the type of the object stored.<\/p>\n<p>The &#8220;.value&#8221; is simply a <code>void*<\/code>, which lets me store a pointer of any kind, and it works pretty well.<\/p>\n<p>Here&#8217;s the code for your enjoyment, I hope this is useful to C apprentices.<\/p>\n<p>The example shows how you can store native types like int, or more complex <code>char*<\/code> or even a <code>struct person*<\/code> (which is the more useful probably to your purposes)<\/p>\n<p>It&#8217;s a good exercise to see the uses of the &#8220;address of&#8221; operator &#8220;&amp;&#8221;, which is used to initialize pointers (the &#8216;&amp;&#8217; can also be used differently to create references, which I call in my mind as &#8216;aliases&#8217;, but this is not shown in this example)<\/p>\n<p>I also play with a not so popular syntax to access a pointer&#8217;s sub-fields:<\/p>\n<p><code>(*myPointer).structField == myPointer-&amp;gt;structField<\/code><\/p>\n<p>to teach you that the <code>-&amp;gt;<\/code> is a short hand for dereferencing a pointer and accessing one of its fields.<\/p>\n<p>[pastacode lang=&#8221;c&#8221; manual=&#8221;%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%2F%2F%20An%20exercise%20to%20play%20with%20a%20struct%20that%20stores%20anything%20using%20a%20void*%20field.%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%0A%23include%20%3Cstdio.h%3E%0A%0A%23define%20TRUE%201%0A%0Aint%20TYPE_INT%20%3D%200%3B%0Aint%20TYPE_STRING%20%3D%201%3B%0Aint%20TYPE_BOOLEAN%20%3D%202%3B%0Aint%20TYPE_PERSON%20%3D%203%3B%0A%0Astruct%20node%20%7B%0A%20%20struct%20node*%20next%3B%0A%20%20int%20type%3B%0A%20%20void*%20value%3B%0A%7D%3B%0A%0Astruct%20person%20%7B%0A%20%20char*%20name%3B%0A%20%20int%20age%3B%0A%7D%3B%0A%0Aint%20main(int%20args%2C%20char%20**argv)%20%7B%0A%0A%20%20struct%20person%20aPerson%3B%0A%20%20aPerson.name%20%3D%20%22Angel%22%3B%0A%20%20aPerson.age%20%3D%2035%3B%0A%0A%20%20%2F%2F%20Define%20a%20linked%20list%20of%20objects.%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%2F%2F%20We%20use%20that%20.type%20field%20to%20know%20what%20we&#8217;re%20dealing%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%2F%2F%20with%20on%20every%20iteration.%20On%20.value%20we%20store%20our%20values.%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20struct%20node%20nodes%5B%5D%20%3D%20%7B%0A%20%20%20%20%7B%20.next%20%3D%20%26nodes%5B1%5D%2C%20.type%20%3D%20TYPE_INT%20%20%20%20%2C%20.value%3D1%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%7B%20.next%20%3D%20%26nodes%5B2%5D%2C%20.type%20%3D%20TYPE_STRING%20%2C%20.value%3D%22anyfing%2C%20anyfing!%22%20%7D%2C%0A%20%20%20%20%7B%20.next%20%3D%20%26nodes%5B3%5D%2C%20.type%20%3D%20TYPE_PERSON%20%2C%20.value%3D%26aPerson%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%7B%20.next%20%3D%20NULL%20%20%20%20%20%2C%20.type%20%3D%20TYPE_BOOLEAN%2C%20.value%3DTRUE%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%7D%3B%0A%0A%20%20%2F%2F%20We%20iterate%20through%20the%20list%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20for%20(%20struct%20node%20*currentNode%20%3D%20%26nodes%5B0%5D%3B%20currentNode%3B%20%20currentNode%20%3D%20currentNode-%3Enext)%20%7B%0A%20%20%20%20int%20currentType%20%3D%20(*currentNode).type%3B%0A%20%20%20%20if%20(currentType%20%3D%3D%20TYPE_INT)%20%7B%0A%20%20%20%20%20%20printf(%22%25s%3A%20%25dn%22%2C%20%22-%20INTEGER%22%2C%20(*currentNode).value)%3B%20%2F%2F%20just%20playing%20with%20syntax%2C%20same%20as%20currentNode-%3Evalue%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%20else%20if%20(currentType%20%3D%3D%20TYPE_STRING)%20%7B%0A%20%20%20%20%20%20printf(%22%25s%3A%20%25sn%22%2C%20%22-%20STRING%22%2C%20currentNode-%3Evalue)%3B%0A%20%20%20%20%7D%20else%20if%20(currentType%20%3D%3D%20TYPE_BOOLEAN)%20%7B%0A%20%20%20%20%20%20printf(%22%25s%3A%20%25dn%22%2C%20%22-%20BOOLEAN%20(true%3A1%2C%20false%3A0)%22%2C%20currentNode-%3Evalue)%3B%0A%20%20%20%20%7D%20else%20if%20(currentType%20%3D%3D%20TYPE_PERSON)%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20since%20we&#8217;re%20using%20void*%2C%20we%20end%20up%20with%20a%20pointer%20to%20struct%20person%2C%20which%20we%20*dereference%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20into%20a%20struct%20in%20the%20stack.%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20struct%20person%20currentPerson%20%3D%20*(struct%20person*)%20currentNode-%3Evalue%3B%0A%20%20%20%20%20%20%20%20printf(%22%25s%3A%20%25s%20(%25d)n%22%2C%22-%20TYPE_PERSON%22%2C%20currentPerson.name%2C%20currentPerson.age)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%7D%0A%0A%20%20%20%20return%200%3B%0A%7D&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\n<p>The output is this:<\/p>\n<pre><code>- INTEGER: 1\n- STRING: anyfing, anyfing!\n- TYPE_PERSON: Angel (35)\n- BOOLEAN (true:1, false:0): 1\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I remember being in school back around 1998 and not knowing enough about C to do this. After coding in other languages, then going back to C++ and understanding at a lower level how references and pointers work, this was pretty easy to figure out. In this exercise I store elements of different types in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3408,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15,30,1],"tags":[239,1437,1432,1431,1429,810,1430,1433],"class_list":["post-3388","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","category-geeklife","category-uncategorized","tag-c","tag-code","tag-heterogeneous-list","tag-list","tag-pointers","tag-programming","tag-references","tag-void-pointer"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-12-20-at-7.05.10-PM.png?fit=1416%2C517&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-SE","jetpack-related-posts":[{"id":799,"url":"https:\/\/www.gubatron.com\/blog\/function-callbacks-in-c\/","url_meta":{"origin":3388,"position":0},"title":"Function callbacks in C","author":"gubatron","date":"May 2, 2008","format":false,"excerpt":"Ever since I started programming in Javascript, and doing asynchronous function calls, I've found myself to be addicted to passing functions as parameters. I do it a lot in python and php, it's very easy to do this on all these dynamic typed languages. I never had this concept of\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2029,"url":"https:\/\/www.gubatron.com\/blog\/reference-passing-functions-as-parameters-in-c\/","url_meta":{"origin":3388,"position":1},"title":"Reference: Passing functions as parameters in C","author":"gubatron","date":"July 18, 2010","format":false,"excerpt":"[c] #include <stdio.h> \/\/Example to show how to pass \/\/functions as parameters in C. \/\/simple function that returns the sum of two ints int sum(int i, int j) { return i+j; } \/\/simple function that prints a char* void printSomething(char* something) { printf(\"%sn\",something); } \/\/function that takes \/\/ 2 ints\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2047,"url":"https:\/\/www.gubatron.com\/blog\/map-function-in-java\/","url_meta":{"origin":3388,"position":2},"title":"Map function in Java","author":"gubatron","date":"August 31, 2010","format":false,"excerpt":"I read on some email signature something along the lines of: \"If I had a dollar for every for(int i=0; i < size; i++) { ... } I've written I'd be rich\" After coding on Android and learning about some of the tips for performance, like \"With an ArrayList, a\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":980,"url":"https:\/\/www.gubatron.com\/blog\/javareflection-notes-invoking-a-static-main-method-from-a-dinamically-loaded-class\/","url_meta":{"origin":3388,"position":3},"title":"Java\/Reflection notes: Invoking a static main() method from a dinamically loaded class.","author":"gubatron","date":"November 22, 2008","format":false,"excerpt":"Maybe for some wild reason, your Java application will need to execute a pre launcher that won't know about the Main class it's supposed to invoke until it's being executed. For example, you have distributed your Java application but you used pack200 to compress your jars, and your new application\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3383,"url":"https:\/\/www.gubatron.com\/blog\/things-to-remember-when-compilinglinking-cc-software\/","url_meta":{"origin":3388,"position":4},"title":"# Things to remember when compiling\/linking C\/C++ software","author":"gubatron","date":"March 18, 2015","format":false,"excerpt":"by Angel Leon. March 17, 2015. Include Paths On the compilation phase, you will usually need to specify the different include paths so that the interfaces (.h, .hpp) which define structs, classes, constans, and functions can be found. With gcc and llvm include paths are passed with -I\/path\/to\/includes, you can\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/03\/make-all-executables.png?fit=680%2C510&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/03\/make-all-executables.png?fit=680%2C510&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/03\/make-all-executables.png?fit=680%2C510&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3241,"url":"https:\/\/www.gubatron.com\/blog\/setting-up-eclipse-as-your-ide-for-bitcoin-c-development-on-macosx\/","url_meta":{"origin":3388,"position":5},"title":"Setting up Eclipse as your IDE for Bitcoin C++ development on MacOSX.","author":"gubatron","date":"February 9, 2014","format":false,"excerpt":"If you are a Java developer used to the productivity levels achieved by working with eclipse's code navigation, code completion and refactoring tools, it's worth your time staying in eclipse for any sort of C++ development. This post refers specifically to getting your eclipse environment to work with a particular\u2026","rel":"","context":"In &quot;BitCoin&quot;","block_context":{"text":"BitCoin","link":"https:\/\/www.gubatron.com\/blog\/category\/bitcoin-2\/"},"img":{"alt_text":"Screen Shot 2014-02-09 at 1.09.39 PM","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-09-at-1.09.39-PM.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-09-at-1.09.39-PM.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-09-at-1.09.39-PM.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3388"}],"version-history":[{"count":9,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3388\/revisions"}],"predecessor-version":[{"id":3783,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3388\/revisions\/3783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media\/3408"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}