<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Learn Programming The Easy Way - Medium]]></title>
        <description><![CDATA[Programming is not as hard as it is often portrayed to be. You do not need to be a super human genius to learn Programming and make cool stuffs. Do you have the willingness to learn? Do you like solving problems and breaking stuffs? Come join the awesome community of programmers. - Medium]]></description>
        <link>https://medium.com/learn-programming-the-easy-way?source=rss----2c49d069d51a---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Learn Programming The Easy Way - Medium</title>
            <link>https://medium.com/learn-programming-the-easy-way?source=rss----2c49d069d51a---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 19:24:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/learn-programming-the-easy-way" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Reading and Constructing complex declarations in C]]></title>
            <link>https://medium.com/learn-programming-the-easy-way/reading-and-constructing-complex-declarations-in-c-1818d9f8819d?source=rss----2c49d069d51a---4</link>
            <guid isPermaLink="false">https://medium.com/p/1818d9f8819d</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[c]]></category>
            <dc:creator><![CDATA[Sourabh Khandelwal]]></dc:creator>
            <pubDate>Wed, 14 Mar 2018 17:34:09 GMT</pubDate>
            <atom:updated>2024-06-15T16:23:17.762Z</atom:updated>
            <content:encoded><![CDATA[<p>There are a plethora of good books on C on the market (there was a question on Stackoverflow (<a href="https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list">https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list</a>), answers to which mentioned lists of some of the must read C books. For some reason, the question has now been removed, but the lists have since been posted on other websites, including <a href="http://bestprogrammingbooks.com/30-c-books-recommended-by-stackoverflow/">http://bestprogrammingbooks.com/30-c-books-recommended-by-stackoverflow/</a>) and I have read some of them (at least bits and pieces) like Head First C by David and Dawn Griffith, C Primer Plus by Stephen Prata, Pointers on C by Kenneth Reek, and also Let Us C by Yashwant Kanetkar and Programming in ANSI C by Balaguru Samy. While all of these are indeed great text to begin with, none of them, to the best of my knowledge, sheds light on how to read and construct complex declarations. To be bit more pedantic, none of them explains how to read declarations like:</p><pre><strong>int </strong>*(*ptr[5])[5];<br>void (*func(char, int(*)(int)))(float);</pre><p>In this article, I will explain how to read complex declarations like the two mentioned above and also how to construct declarations to answer questions like, <em>“Declare a function in C that returns pointer to an array of pointers to integer.”</em></p><h4>Hmmm… but why even bother about such complex declarations at all?</h4><p>You may question, “But what’s the need of such declarations at all?” And you’re somewhat correct, one is rarely going to construct or read such declarations, such a thing has hardly happened with me. But in some scenarios, you may need to read or construct such declarations, like:</p><p>“Declare a pointer to a function returning pointer to an array of integers.”</p><p>Function pointers too find some interesting applications, I will probably soon come up with an article on them.</p><p>By the end of this post, you will be able to answer the above question. So, let’s get started.</p><h3>Let’s cover some basics first</h3><h4>The bits and pieces of a C declaration</h4><p>The C declaration most of us generally stumble upon are of the format:-</p><pre><strong>T </strong>_var;<br><strong>T </strong>*_ptr;<br><strong>T </strong>_ar[10];<br><strong>T</strong> func(<strong>T</strong>, <strong>T</strong>);</pre><p>Where T is some primitive type (int, float, double, charetc). Declarations like these consists of two parts:</p><ol><li>A declaration specifying sequence. This part is mandatory and can not be omitted.</li><li>A list of declarators. You may skip this part.</li></ol><p>The first part is a declaration specifying sequence, this sequence itself consist of different parts such as storage specifiers (static, extern, etc.), function specifiers (inline). But the part which is not optional is a <strong>type specifier</strong>. As the name suggests, it indicates some type (int, float, char, double, etc.). declaration specifying sequence also consists of type qualifiers (const , volatile, restrict, _Atomic.) as well as modifiers (signed , unsigned , long , short.) both of which are optional.</p><p>Base type and Compound type:</p><p>C data types come in two flavours, one is the base type (also called the primitive type), such as int, float, char etc. And other is the compound type(also called composite type) such as structures, pointers, unions, enums and arrays. Wikipedia defines compound data types as the one that can be constructed from primitive type.</p><p><em>Only the Base type are part of Declaration Specifying Sequence</em>. While declarations like int *ptr; are valid C declaration only int is considered to be a part of the Declaration Specifying Sequence, Compound Type (such as *(pointer) in this example) are not considered to be part of it. We will see it’s effect after reading about List of Declarators.</p><h4>List of Declarators</h4><p>A list of declarators is simply a list of comma separated of identifiers. For ex, int a, b; declares two integer type variables a and b. Here int is in the <em>Declaration Specifying Sequence </em>and a and b are in the <em>List of Declarators</em>.</p><p><em>Only Base type effects the entire List of Declarators<br></em>That is Compound type do not effect the declaration of every identifier in the List of Declarators but only the declaration of immediate identifier conjugated to it. That is int a, *b; declares an integer and a pointer to an integer because a Compound type like * only effects the immediate identifier with which it is conjugated(here it is b). Further for the same reason int *a, b; and int* a, b; both declare a as a pointer to an integer and b as an integer.</p><p>Enough of the basics. We are now in position to learn how to read and construct complicated declarations. And to be honest, it is not as hard as it seems; it just requires following some simple rules.</p><h4>Rule 1 : Reading declaration of a function, an array and a pointer type identifier</h4><ul><li>* should be read as pointer to.</li><li>[n] should be read as array of n.</li><li>(T1, T2)should be read as function accepting arguments T1 and T2 and returning.</li></ul><p>Also, note that while writing, they are always written as follows:</p><ul><li>* always comes to the left of an identifier.</li><li>(T1, T2, …) always comes to the right of an identifier.</li><li>[n]always comes to the right of an identifier.</li></ul><p>That is, int * func(int)[5]; is <em>a valid</em> declaration but</p><p>int func*; , int func*(int); , int func*(int)[5] , etc <em>are not</em>. Also, int ar[5](int); <em>is valid</em> butint func(int)[5]; <em>is although valid but can not be used as one can not return an array in C(as arrays are not assignable)</em>.</p><h4>Rule 2 : Always start reading from the name of the identifier</h4><p>That is, to read a declaration like int a; , you must start reading it as <em>a is …</em></p><h4>Rule 3 : Direction of reading</h4><p>Always start from the name of the identifier(as stated in the second rule) and <em>keep moving to the right</em> till you either:-<br>1. Reach the end of the declaration. That is you either hit a comma(,) in which case the declaration of next identifier starts or you hit a semicolon(;) in which case the entire List of Declarator ends. Once you reach the end of the declaration, start moving to the left, read *(as pointer to) if present and then finally the Declaration Specifying Sequence(remember it contains only the base type, modifiers and qualifiers and not compound types). Let’s take a very basic example:-<br>int a; <br>The above declaration will be read as:</p><ul><li>Start with the name of the identifier.</li><li>Hit semicolon and start reading the Declaration Specifying Sequence. <br><em>So, it will be read as “a is an integer.”</em></li></ul><p>Let’s take another example, Read int *ptr;</p><ul><li>Start with the name of the identifier.</li><li>Hit semicolon.</li><li>Read * as pointer to .</li><li>Read the Declaration Specifying Sequence.<br>So, it will be read as <em>“ptr is pointer to an integer.”</em></li></ul><p>One more example, read int ar[5];</p><ul><li>Start with the name of the identifier.</li><li>Read [5] as array of 5 .</li><li>Hit semicolon, start moving to the left and finally read the Declaration Specifying Sequence.<br>so it will be read as “<em>ar is an array of 5 integers.</em>”</li></ul><p>Another example:-</p><p>read int ar[5](int);</p><ul><li>Start with the name of the identifier.</li><li>Read [5] as array of 5 .</li><li>Read (int) as function accepting integer argument and returning.</li><li>Hit semicolon and read rest of the declaration(Declaration Specifying Sequence).<br>so, it will be read as <em>“ar is an array of 5 functions accepting integer argument and returning an integer.”<br></em>Remember a declaration like int func(int)[5]; <em>is valid but you can not use it</em>.</li></ul><p>2. Hit a closing parenthesis. This is used to override the priority of reading order mentioned in the first rule(we will see how)<br>In which case start moving to the left till you hit the corresponding opening parenthesis. Once you reach the opening parenthesis, continue from where you left, that is start reading from just after the closing parenthesis(and follow the same rule of reversing direction once the declaration ends(you hit either , or ;)). Let’s take an example to sum up:-</p><p>read int (*ptr)[5];</p><ul><li>Start with the name of the identifier.</li><li>Hit the closing parenthesis, and keep moving to the left till you hit the corresponding opening parenthesis. That is read * as pointer to and then hit the opening parenthesis.</li><li>Start reading from where you left (that is, just after the closing parenthesis) and then follow the usual rule of reading, i.e., read [5] as array of 5, hit the semicolon, reverse direction, and read the renaming part including the Declaration Specifying Sequence.<br>so, this will be read as: <em>“ptr is a </em><strong><em>pointer to</em></strong><em> an </em><strong><em>array of</em></strong><em> 5 integers.”</em></li></ul><p>Another example:-</p><p>read int (*ptr)(int);</p><ul><li>Start with the name of the identifier.</li><li>Hit the closing parenthesis, and keep moving to the left till you hit the corresponding opening parenthesis. That is read * as pointer to and then hit the opening parenthesis.</li><li>Start reading from where you left (that is, just after the closing parenthesis) and then follow the usual rule of reading, i.e., read (int) function accepting int and returning, hit the semicolon, reverse direction, and read the renaming part, including the Declaration Specifying Sequence.<br>So, this will be read as <em>“ptr is a </em><strong><em>pointer to</em></strong><em> a</em><strong><em> function accepting integer argument and returning</em></strong><em> an integer.”</em></li></ul><p>Finally, a declaration like int (*ptr)[5](int); will be read as:-<br><em>“ptr is a pointer to an array of 5 functions accepting an integer argument returning an integer.”</em></p><p>And that’s pretty much all of it. So, if you have read this far, then pat yourself on the back for grasping a topic which is not known to many.</p><p>At the starting of this article, we were left with a few questions to which you now have the answers.</p><ul><li>read int *(*ptr[5])[5]; which is <em>“ptr is an array of 5 pointers to an array of 5 pointers to an integer”.</em></li><li>read void(*func(char, int(*)(int)))(float); which is <em>“func is a function accepting a character and a pointer to function accepting int as arguments and returning an integer and returning a pointer to a function accepting a float as an argument and returning void.”</em></li><li>Declare a function pointer, which is cakewalk for you with all the knowledge you have just acquired. For instance int (*ptr) (); declares a variable ptr <em>“pointer to a function returning int”.</em></li></ul><p>I hope you liked this article. If you have any questions, you can leave them in the comment section below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1818d9f8819d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/learn-programming-the-easy-way/reading-and-constructing-complex-declarations-in-c-1818d9f8819d">Reading and Constructing complex declarations in C</a> was originally published in <a href="https://medium.com/learn-programming-the-easy-way">Learn Programming The Easy Way</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>