    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: char* p vs char *p</title>
        <link>https://members.accu.org/index.php/articles/1445</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #4 - Feb 1994</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c233/">04</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-233/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+233/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;char* p vs char *p</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 01 February 1994 08:52:00 +00:00 or Tue, 01 February 1994 08:52:00 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<p>In the epilogue of issue 3 I asked if anyone knew why we could not
have char* x, y; to mean both x and y were char pointers and char *x,
y; to mean x is a char pointer and y is only a char.</p>
<p>Sean Corfield (UK ISO C++ representative) replies: </p>
<p style="margin-left: 40px;">Mike,</p>
<p style="margin-left: 40px;">Just glancing through issue 3 again and
saw your comment in the epilogue about int * x, y; If you mean 'what is
the technical reason' it's this:</p>
<p style="margin-left: 40px;">A declaration is a sequence of specifiers
followed by a comma-separated list of declarators. Specifiers are
things like int, const, typedef etc.</p>
<p style="margin-left: 40px;">Declarators include identifiers, but also
include *, &amp;, [], () etc.</p>
<p style="margin-left: 40px;">Because whitespace is not significant in
C+ + (or C), your declaration looks like this:</p>
<p style="margin-left: 40px; font-family: monospace;">int&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *x&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;<br>
specifiers&nbsp;&nbsp; declarator
,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
declarator&nbsp;&nbsp;&nbsp;&nbsp; ;</p>
<p style="margin-left: 40px;">Could we change this? Technically, yes,
change the grammar so that pointer operators are part of the
specifier-list:</p>
<p style="margin-left: 40px; font-family: monospace;">int*&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;<br>
specifiers&nbsp;&nbsp;&nbsp; declarator
,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; declarator&nbsp;&nbsp;&nbsp; ;</p>
<p style="margin-left: 40px;">Unfortunately, C++ has to stay (fairly)
compatible with C and that would break too much code.</p>
<p style="margin-left: 40px;">As an aside, this example is precisely
why I recommend that people always put declare each identifier in a
separate declaration on a separate line:</p>
<p style="margin-left: 40px; font-family: monospace;">int*&nbsp;&nbsp;
&nbsp;x;<br>
int &nbsp;&nbsp;&nbsp; y;</p>
<p style="margin-left: 40px;">That's much clearer.<br>
</p>
<p style="margin-left: 40px;">Regards,<br>
Sean Corfield</p>
<p>By coincidence, Rick Stones (who is monitoring the comp.lang.C++
newsgroup for interesting threads) has just send me a thread on the
same subject.</p>
<p><span style="text-decoration: underline; font-weight: bold;">&lt;Craig
Cockburn&gt;</span> Is there any difference between the declaration</p>
<pre>char&nbsp; *x and&nbsp; char*&nbsp;&nbsp; x</pre>
<p><span style="text-decoration: underline; font-weight: bold;">&lt;Joe
Foster&gt;</span> Not to the compiler. I find char* x clearer, but this
can trip you up.</p>
<p>Say you have the following definition:</p>
<pre>char*&nbsp;&nbsp; x,&nbsp;&nbsp; y,&nbsp;&nbsp;&nbsp; z () ;</pre>
<p>It looks at first glance like y is a char* and z() returns char*,
right?</p>
<p>Wrong. If you prefer char* x to char *x, make sure to declare or
define everything separately, like so:</p>
<pre>char* x; char* y; char* z();</pre>
<p><span style="text-decoration: underline; font-weight: bold;">&lt;Gerhard
Thallinger&gt;</span> From a compilers point of view there is no
difference. Its only a question of style how you declare a pointer.</p>
<p>I would like to know what the reason is, that a majority of C++
programmers (as far as I can say that from looking at sources from
public domain class libraries) use the notation 'char* x'?</p>
<p>Does anybody know why Bjarne Stroustrup is using this notation in
the ARM ?</p>
<span style="text-decoration: underline; font-weight: bold;">&lt;Thaddeus
L. Olczvk&gt;</span><br>
<p>Since he occasionally read this group, he might actually tell us
what he was smoking when he did this.</p>
<p>My best guess is that it makes prototyping a bit easier.</p>
<p>In</p>
<pre>void&nbsp; foo(char&nbsp;&nbsp; *pc)&nbsp;&nbsp; //&nbsp; yes&nbsp;&nbsp; I'm&nbsp; PC&nbsp;&nbsp; </pre>
<p>... if you move this line from the .cc to .hh file to make the
declaration, it is easier to accidentally delete the * this way. Of
course if you pay attention.</p>
<span style="text-decoration: underline; font-weight: bold;">&lt;Michael
Utech&gt;</span><br>
<p>Syntactically? No.</p>
<p>I prefer 'char* x', simply because it looks better to me.</p>
<p>Several style guides encourage the use of 'char *x' because of
reasons I forgot. It's a matter of taste.</p>
<p style="font-weight: bold; text-decoration: underline;">&lt;Reto J.
Hermann&gt;</p>
<p>I believe the reason why style guides encourage 'char *x' is for the
cases where you have a list of variables to declare, i.e.,</p>
<pre>char* x, y, z;&nbsp;&nbsp;&nbsp; // might suggest all of x, y, z are of (char*)<br>   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // in fact, this is equivalent to char* x, char y, char z <br>char *x, y, z;&nbsp;&nbsp;&nbsp; // brings the actual meaning out a bit crisper</pre>
<p style="text-decoration: underline; font-weight: bold;">&lt;Paul J
Lucas&gt;</p>
<p>Syntactically? Yes. One is space star, the other is star space.
Semantically, there is no difference.</p>
<p>I prefer 'char* x', simply because it looks better to me.</p>
<p>To each, his own...but I don't have to read your code, <br>
</p>
<pre>char* p, q; // char *p; char q; - not what it looks like!</pre>
<p>God (otherwise known in this context as Dennis Ritchie) intended the
* to go next to the variable name.</p>
<p style="font-weight: bold;">&lt;Charles William Hubbard&gt;</p>
<p>Yes, exactly right. In the book &quot;Code Complete&quot; the author actually
encourages the use of the &quot;char* x&quot; format because the '*' changes the
type (now a pointer to a char -- not a char) and he feels it should go
with the type, not the variable. Normally this causes the problem you
describe. However, the author also discourages declaring more than one
variable per line. He says this provides room for a comment description
of each variable. It also solves the problem you mention.</p>
<p>I'm not sure which of these points of view I favour. I prefer the
&quot;char* x&quot; syntax over &quot;char *x&quot; and I also think a comment line for
each variable is a nice thing (especially for globals or module
variables).</p>
<p>However, there are many times when I'd rather declare multiple
variables per line (i.e. &quot;int i,j,k;&quot; or &quot;double minimum, maximum,
mean;&quot;).</p>
<p style="text-decoration: underline; font-weight: bold;">&lt;Nigel
Backhurst&gt;</p>
<p>This can be very important, I have recently looked at incidents
involving safety critical software. In three, the mistake had been made
of presuming that in the declaration:</p>
<pre>char* x, y, z;</pre>
<p>y was a pointer to a character. There is from the safety point of
view a strong argument for the view that multiple declarations on one
line should not be allowed. They would say the above should be declared
as:</p>
<pre>char *x; char y; char z;</pre>
<p>I feel that for most software this is going over the top, but if I
was defining a style guide for safety critical software I would insist
on it.</p>
<p style="text-decoration: underline; font-weight: bold;">&lt;Thomas M.
Breuel&gt;</p>
<p>It also has to do with what C/C++ type declarations actually are
intended to express. Many people seem to think of them as &quot;&lt;type&gt;
&lt;variable_name&gt;&quot; and get very confused. A better way of thinking
of them is as &quot;&lt;type&gt; &lt;expression&gt;&quot;. That is, you declare
the type of the &quot;expression&quot; on the right:</p>
<pre>char *x;</pre>
<p>This means that the type of the expression &quot;*x&quot; is &quot;char&quot;.</p>
<p>RE: safety critical software. - I agree. <br>
</p>
<p style="text-decoration: underline; font-weight: bold;">&lt;Robin
Rowe&gt;</p>
<p>If safety-critical software is the issue, then there is something
wrong with your example. It should be one of these:</p>
<pre>const char* const x; <br>const char* x; <br>char* const x;</pre>
<p>It is unlikely that the programmer actually needed a pointer that
could be modified both by contents and location.</p>
<p style="text-decoration: underline; font-weight: bold;">&lt;Obi
Thomas&gt;</p>
<p>But God++ (aka Bjarne Stroustrup) favours char* p over char *p.</p>
<p style="text-decoration: underline; font-weight: bold;">&lt;Jerzy
Sobczyk&gt;</p>
<p>RE: Gerhard Thallinger</p>
<p>Making prototypes I never delete variable names (unless forced by
stupid compilers).</p>
<p>According to the grammar it is not necessary. (Only semicolon on the
end is needed).</p>
<p>What's more - it helps to locate the point when you make an error in
function call like invalid type of a parameter -then compiler can use
the name of a parameter in question in error message pointing exactly
what's wrong.</p>
<p style="font-weight: bold;"><span style="text-decoration: underline;">&lt;Robert
Schmidt&gt;</span> </p>
<p>RE: Obi Thomas</p>
<p>And if he does, that's his problem. I and many others feel most
comfortable seeing declarations like:</p>
<pre>char *s1, s2[], s3;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // as I like it</pre>
<p>instead of</p>
<pre>char* s1, s2[], s3;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // as I do not like it</pre>
<p>If you (any of you) disagree with this, that's fine with me, as long
as I am not forced to clean up your code.</p>
<p>Anyway, it's just a matter of replacing for &quot;char* &quot; with &quot;char *&quot;
with some standard utility. As Lucas points out above, &quot;char *&quot; was the
original intent, and even Stroustrup doesn't have the power to push his
personal style onto everybody else.</p>
<p>Just my 2 cents, nuthin more.</p>
<p style="text-decoration: underline; font-weight: bold;">&lt;Paul J
Lucas&gt;</p>
<p>This is one of the few places where I quibble with Bjarne.</p>
<p>Dennis got it right and it is eloquently explained in the C
Programming Language:</p>
<pre>int *p;</pre>
<p>The *p is what is an int.</p>
<p>I think Bjarne unnecessarily continues to confuse new C++
programmers. The fact that this question has been posted a number of
times is proof. I can only hope that he corrects this in his 3rd
edition. (And goes back to calling arrays &quot;vectors&quot; like he did in his
first edition...but that's a different quibble.)</p>
<p>No offence, Bjarne. </p>
<p style="text-decoration: underline; font-weight: bold;">&lt;Tony
Coates&gt;</p>
<p>I must say that while I prefer to type</p>
<pre>int* p;</pre>
<p>myself, it's too easy to end up writing</p>
<pre>int* p,q;</pre>
<p>which gives you a pointer and an int instead of what you probably
wanted.</p>
<p>At least if you write</p>
<pre>int *p;</pre>
<p>then you will probably write</p>
<pre>int *p,*q;</pre>
<p>to give you the two pointers you probably wanted.</p>
<p>Otherwise, I would do</p>
<pre>typedef int *pint; pint p;</pre>
<p>which at least circumvents the entire problem, as the risk of
cluttering the name-space up a bit.</p>
<p>Anyway, just my thoughts. </p>
<p style="font-weight: bold;">&lt;david.finch&gt;</p>
<p>The C++ spec should be changed so that</p>
<pre>char*  a, b, c: <br></pre>
<p>is</p>
<pre>char*  a; <br>char*  b; <br>char*  c;</pre>
<p>and</p>
<pre>char&nbsp;  *a, b, c;</pre>
<p>is the old way.</p>
<p>I think you are wrong new C and C++ programmers are more confused why</p>
<pre>char   *a, b, c;</pre>
<p>is not</p>
<pre>char*  a; <br>char*  b; <br>char*&nbsp; c;</pre>
<p>I know that everyone I have known that has learn C has found this
difficult including myself (6 years of C now). I think that it is such
a pain that I convert all code to have declaration in a line so people
can not be confused in anyway of its type.</p>
<p>P.S.</p>
<p>thanks to the poster that informed us that it should be read</p>
<pre>type&nbsp;&nbsp; &nbsp;expresion</pre>
<p>but this is not what the beginning programmer expects a language to
have as its declarations statements.</p>
<p style="text-decoration: underline; font-weight: bold;">&lt;JoeFoster&gt;</p>
<p>Keep in mind that God++ also doesn't often define or declare two or
more variables, arrays, or functions in the same statement.</p>
<p>It's very easy to get the wrong idea of y's type, for example:</p>
<pre>char* x, y();</pre>
<p>Remember that we are mere mortals and that mortals have limits,
unlike Gods and God++'s.:-)</p>
<p style="text-decoration: underline; font-weight: bold;">&lt;Bjarne
Stroustrup&gt;</p>
<p>Naturally &quot;*p is an int,&quot; and also &quot;the type of p is int*.&quot;</p>
<p>Which kind of statement do you focus on? <br>
</p>
<pre>int* p; vs int *p;</pre>
<p>is not a question of right and wrong, but one of style and emphasis.</p>
<p>C - especially early C - emphasised expressions; declarations were
often considered little more than a necessary evil. C++, on the other
hand, has a heavy emphasis on types.</p>
<p>RE: Bjarne unnecessarily continues to confuse new C++ programmers.</p>
<p>Actually, I consider the C declarator syntax an experiment that
failed.</p>
<p>Having both prefix and postfix operators in declarations is a bad
idea that has caused and will continue to cause endless confusion.</p>
<p>This particular source of confusion predates even Dennis. The best
we can do is to try to minimise confusion through style.</p>
<p>The critical confusion comes only when people insist on declaring
several variables with a single declaration:</p>
<pre>int* p, a;</pre>
<p>and in particularly when they fail to initialise those variables. I
have consistently recommended against both practices. To contrast:</p>
<pre>int* p = &amp;i; <br>int a = 7;</pre>
<p>is not likely to confuse anybody, and the compiler will catch many
mistakes:</p>
<pre>int* p = 7; // error: int* initialized with int <br>int a = &amp;i; // error: int initialized with int*</pre>
<p>RE: The fact that this question has been posted a number of times is
proof.</p>
<p>No. Whenever something can be done in two ways someone will be
confused.</p>
<p>Stick to one pointer per declaration and to initialized variables
and the source of confusion disappears.</p>
<p>RE: I can only hope that he corrects this in his 3rd edition.</p>
<p>There in no 3rd edition on its way, and none is needed so far.</p>
<p>Anyway, I don't think you should hold your breath for a style change
anyway. I prefer the style I use, I think I have good reasons for my
preference, I encourage people to consider that style, and people who
have different preferences are free to indulge them.</p>
<p>RE: No offence, Bjarne.</p>
<p>I'm sure none was meant and none was taken.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
