{"id":2928,"date":"2018-01-27T00:01:19","date_gmt":"2018-01-27T08:01:19","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2928"},"modified":"2018-01-27T08:28:33","modified_gmt":"2018-01-27T16:28:33","slug":"the-perils-of-typedef","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2928","title":{"rendered":"The Perils of <em>typedef<\/em>"},"content":{"rendered":"<p>The <em>typedef<\/em> keyword is both handy and dangerous. It&#8217;s handy because it allows the pantheon of C variable types to be expressed in different ways. It&#8217;s dangerous for the same reason.<br \/>\n<!--more--><br \/>\nIn higher-level languages, variable types sprout like weeds in a garden. If you need a special variable type to describe a  chunk of memory, that type is defined and used. In C, the type is an unsigned integer value that the <em>typedef<\/em> keyword creates: <em>size_t<\/em>. Here&#8217;s the statement that does so:<\/p>\n<p><code>typedef unsigned long size_t<\/code><\/p>\n<p>In the original K&#038;R book, the justification for <em>typedef<\/em> is that it can create machine-specific variable types that are easily ported. For example, <em>size_t<\/em> might have been defined as an <em>unsigned int<\/em> for the old IBM PC, but it&#8217;s an <em>unsigned long<\/em> for current machines, or perhaps it&#8217;s even an <em>unsigned long long<\/em>. It doesn&#8217;t matter because the C language header file that defines <em>size_t<\/em> can be changed. The programmer just accepts the <em>size_t<\/em> variable as a valid type.<\/p>\n<p>You can use <em>typedef<\/em> inside your code similar to the preprocessor directive <code>#define<\/code>, though <em>typedef<\/em> is handled by the compiler itself, not the preprocessor. Here&#8217;s the format:<\/p>\n<p><code>typedef <em>variable_type new_name<\/em><\/code><\/p>\n<p>The <em>variable_type<\/em> is an existing C language variable, though it could also be an alias to that variable that <em>typedef<\/em> already created. The <em>new_name<\/em> is the alias. For example:<\/p>\n<p><code>typedef char byte<\/code><\/p>\n<p>Like a <code>#define<\/code> value, the <em>typedef<\/em> variable alias is valid only in the code where it&#8217;s created. Due to this limited scope, I encourage you to document the use of <em>typedef<\/em> within your code (or header file).<\/p>\n<p>Most commonly, the <em>typedef<\/em> keyword is used to shorten structure definitions. For example:<\/p>\n<pre class=\"screen\">\r\ntypedef struct human {\r\n    char *name;\r\n    int age;\r\n} person;<\/pre>\n<p>The <code>struct human<\/code> structure is aliased to <code>person<\/code>. So you can replace:<\/p>\n<p><code>struct human myself;<\/code><\/p>\n<p>with:<\/p>\n<p><code>person myself;<\/code><\/p>\n<p>In both examples, <code>myself<\/code> is a <code>struct human<\/code> structure variable.<\/p>\n<p>The example given in the K&#038;R manual uses <em>typedef<\/em> in the same statement that defines the structure, as shown above. I recommend using two statements instead:<\/p>\n<pre class=\"screen\">\r\nstruct human {\r\n    char *name;\r\n    int age;\r\n};\r\ntypedef struct human person;<\/pre>\n<p>I believe this method is more readable, plus it avoids a common error made by budding programmers who become overly infatuated with <em>typedef<\/em>, especially with linked-lists and other self-referencing structures:<\/p>\n<pre class=\"screen\">\r\ntypedef struct greek {\r\n    gr *prev;\r\n    char *letter;\r\n    gr *next;\r\n} gr;<\/pre>\n<p>Above, the <code>greek<\/code> structure is aliased to <code>gr<\/code>; effectively <code>struct greek<\/code> is replaced by <code>gr<\/code>. The problem is that the new <code>gr<\/code> variable type is used <em>inside<\/em> the <code>greek<\/code> structure. Some compilers catch this error (using an alias before it&#8217;s defined), but some don&#8217;t.<\/p>\n<p>The proper solution is to define the new <code>gr<\/code> structure variable like this:<\/p>\n<pre class=\"screen\">\r\nstruct greek {\r\n    struct greek *prev;\r\n    char *letter;\r\n    struct greek *next;\r\n};\r\ntypedef struct greek gr;<\/pre>\n<p>From this point on, you can use <code>gr<\/code> as a substitute for <code>struct greek<\/code>. <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/01\/0127.c\">Click here<\/a> to view the double-linked list program from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2905\">a previous Lesson<\/a>, though in this Lesson&#8217;s code <em>typedef<\/em> is used to create a variable alias, <code>gr<\/code>. Judge for yourself whether it&#8217;s a useful trick.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I suggest understanding this keyword, but never using it. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2928\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2928","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2928"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2928\/revisions"}],"predecessor-version":[{"id":2947,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2928\/revisions\/2947"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}