    <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  :: Questions and Answers</title>
        <link>https://members.accu.org/index.php/articles/1014</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 + CVu Journal Vol 12, #3 - May 2000</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/c77/">CVu</a>

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

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+126/">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;Questions and Answers</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 07 May 2000 13:15:36 +01:00 or Sun, 07 May 2000 13:15:36 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a></h2>
</div>
<p>Before I get on with this issue's collection of questions and
answers I want to appeal for two things. First, I can do with a
steady supply of questions. Of course I can mine Internet
newsgroups for extras but many of you must come across puzzling
questions while you are programming. Even if you think you have the
answer, please donate the question. If you want I will hide your
name, but send the question. Note that even very simple questions
can have layers of problem hidden within (see the answers to
question 2 below)</p>
<p>My second request is for someone else to take over collating
this column. It is a simple well-defined task that should take a
couple of hours per issue. Yes, I can do it, but the more that such
tasks can be distributed the easier it will be to replace me as
editor. Or if you like to think of it in a different light, the
less likely I will be just to stop.</p>
<p>By the way, please consider extra answers to earlier questions.
This column is in the section headed Dialogue because it is
intended to be just that; a dialogue between readers moderated by a
columnist.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e28" id="d0e28"></a>This Issue's
Answers</h2>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e31" id="d0e31"></a>Qustions from C Vu
Volume 12 Number 2</h3>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e34" id="d0e34"></a>Question 1:
Clipping Trailing Zeros</h4>
</div>
<p>I am trying to output a double to a string using sprintf. The
code I am using right now is</p>
<pre class="programlisting">
sprintf(#, &quot;%f &quot;,d);
</pre>
<p>where <tt class="varname">d</tt> is a value of type <tt class=
"type">double</tt>. What this does is automatically output to six
decimal places. So if <tt class="literal">d = 1.0</tt> <tt class=
"varname">buf</tt> becomes '1.000000.' A more relevant example
would be <tt class="literal">d = 3.142</tt> resulting in generating
'3.142000' in <tt class="varname">buf</tt>. Is it possible to clip
all the trailing zeroes from the output of a <tt class=
"type">double</tt>?</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e64" id="d0e64"></a>Answer from Colin
Paul Gloster</h4>
</div>
<p>The formatted output functions of <tt class=
"filename">&lt;stdio.h&gt;</tt> use the conversions characters
<tt class="literal">g</tt> and <tt class="literal">G</tt> to output
floating point numbers without trailing zeroes. So <tt class=
"literal">%f</tt> in <tt class="literal">sprintf(buf, &quot;%f
&quot;,d);</tt> should be replaced with <tt class="literal">%g</tt> (or
alternatively <tt class="literal">%G</tt>) yielding <tt class=
"literal">sprintf(buf, &quot;%g &quot;,d);</tt>.</p>
<p>The table on page 244 of <i class="citetitle">The C Programming
Language</i> by Brian W. Kernighan and Denis M. Ritchie, 2nd
edition, Prentice Hall Software Series, ISBN 0-13-11-0362-8, is
very handy for deciding which conversion characters to use if you
are unsure. There is a similar table for formatted input from
<tt class="filename">&lt;stdio.h&gt;</tt> functions on page
246.</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e101" id="d0e101"></a>Answer from
Colin Hersom <tt class="email">&lt;<a href=
"mailto:colin@hedgehog.cix.co.uk">colin@hedgehog.cix.co.uk</a>&gt;</tt></h4>
</div>
<p>Both <tt class="literal">%f</tt> and <tt class="literal">%e</tt>
formats to the <tt class="function">printf</tt> family of functions
have a default precision of 6. This means that unless you override
this there will be six digits printed after the decimal point. The
<tt class="literal">%g</tt> format will suppress trailing zeros,
but will use the exponent format when the number is too small (less
than 0.001) and there is no way to prevent this.</p>
<p class="c2"><span class="remark">[But not everyone agrees with
that last statement]</span></p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e123" id="d0e123"></a>Answer from
Graham Patterson <tt class="email">&lt;<a href=
"mailto:grahamp@econ.Berkeley.EDU">grahamp@econ.Berkeley.EDU</a>&gt;</tt></h4>
</div>
<p>There is a little used format specifier that can be used to
control trailing zero production. The <tt class="literal">g</tt> or
<tt class="literal">G</tt> specifier will only output non-zero
characters after the decimal point, and will usually not include
the decimal point if the value has no fractional part. The
<tt class="literal">#</tt> flag can be used to include the decimal
point, but this usually results in trailing zeros again. The exact
behaviour depends on the field width and precision specified.</p>
<p>To write the value into a buffer using <tt class=
"function">sprintf()</tt>, I would suggest a format of <tt class=
"literal">%-1.9G</tt>, where the '<tt class="literal">-</tt>'
signifies left justification, the '<tt class="literal">1</tt>' is a
minimum field width that will be expanded automatically to the
minimum required for the value, '<tt class="literal">9</tt>' is the
precision for your significant decimal digits to be. The value you
use will depend on your needs. The <tt class="literal">G</tt>
specifier will degrade to the <tt class="literal">E+00</tt> form of
exponent if the value exceeds the format width. The <tt class=
"literal">g</tt> specifier can be used if you prefer lowercase
exponents. Leading zero and space can also be applied. The
<tt class="filename">trailzero.c</tt> file demonstrates some of the
variations.</p>
<p>Most people writing mathematical or scientific software will
recognise the need to report values to a suitable precision, either
in terms of decimal places (where the magnitude of the result is
generally known and the error in the calculation governs the
reporting precision), or as significant digits (where the magnitude
is variable). Essentially it's the difference between a zero as a
space filler and as a quantity.</p>
<pre class="programlisting">
/* Simple demonstration of floating point formatting using the %G specifier.
Graham Patterson April 2000
Trailing zero truncation  */
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
enum {fieldlength = 15};
int main(void) {
  const double test_value = 0.03141590000;
  char *formats[] = {&quot;%#1.9G&quot;, &quot;%#-1.9G&quot;, 
            &quot;%-1.9G&quot;, &quot;%-9.9G&quot;, NULL};
  char **ptr = formats;
  long places;
  while(*ptr)   {
    for(places = 1000000000L; places &gt; 0L;
             places /= 10L) {
      int magnitude =
         (int)log10(fabs(test_value*places));
      printf(&quot;[%s]\t[&quot;, *ptr);
      printf(*ptr, test_value * places);
      printf(&quot;]\tMag. %d&quot;, magnitude);
      printf(&quot;\t[%%-1.%dG]\t[%-1.*G]&quot;,
          magnitude + 1, magnitude + 1,
              test_value * places);
      printf(&quot;\n&quot;);
    }
    ptr++;
  }
  {  /* create new scope for variable */
    double test = 0.1;
    int i = 0;
    while(test &gt; 0.0) {
      i++;
      test /= 10.0;
    }
    printf(&quot;%d places\n&quot;, i);
  }
}
</pre></div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e172" id="d0e172"></a>Question 2:
Pointer Problem</h4>
</div>
<p>Why doesn't the following change the pointer it is passed?</p>
<pre class="programlisting">
void findspace(int * points_at, char * text) {
  int i;
  for (i=0; i&lt;strlen(text); i++)
    if (text[i] = ' ') points_at = text+i;
  return;
}
</pre></div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e179" id="d0e179"></a>Answer from
Brett Fishburne:</h4>
</div>
<p>The pass-by-reference allows a programmer to pass a variable to
a function that in turn is able to modify the variable. Within the
function, the passed parameter is de-referenced using the
<tt class="literal">*</tt> operator before it can be changed. Thus,
a good check for a function that uses pass-by-reference is to see
if the parameter is being de-referenced. In your example,
<tt class="varname">points_at</tt> is not de-referenced, so the
variable being passed by reference is not being modified. To modify
the <tt class="varname">points_at</tt> parameter, the code should
be rewritten as follows:</p>
<pre class="programlisting">
void findspace(int **points_at, char *text) {
  int i;
  for (i=0; i&lt;strlen(text); i++)
    if (text[i] = ' ') *points_at = text+i;
  return;
}
</pre>
<p>I suggest, however, that you rewrite the function as follows to
reduce the risk of receiving a pointer which points past the end of
the string (in case the '\0' character was inadvertently
omitted)?</p>
<pre class="programlisting">
#include &lt;ctype.h&gt;
char *findspace(int buffersize, char *text) {
  int i;
  for (i=0; i&lt;buffersize &amp;&amp; text[i] != '\0'; i++)
    if (isspace(text[i]) return (text[i+1]);
  return((char *)NULL);
}
</pre>
<p>Alternatively, you may find it useful to research the <tt class=
"function">strchr()</tt> function which performs the same exercise
as that in your question.</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e204" id="d0e204"></a>Answer from
Colin Hersom <tt class="email">&lt;<a href=
"mailto:colin@hedgehog.cix.co.uk">colin@hedgehog.cix.co.uk</a>&gt;</tt>:</h4>
</div>
<p>C always passes arguments by value and changing its value in a
function has no effect on the caller. If you want to change the
value that is being passed in, then you must take the address of
the value and pass that in (this is than a pointer argument).
Dereferencing that pointer in the function reveals the value and
allows you to change it. So if the function is declared:</p>
<pre class="programlisting">
void f(int *ap)
</pre>
<p>you can get the integer value like:</p>
<pre class="programlisting">
int i = *ap;
</pre>
<p>or change it by:</p>
<pre class="programlisting">
*ap = i+1;
</pre>
<p>Looking at the types in an expression usually helps to determine
what are sensible operations and what are not. In the <tt class=
"function">findspace</tt> function, <tt class=
"varname">points_at</tt> is a pointer to an integer, and <tt class=
"varname">text</tt> is a pointer to a <tt class="type">char</tt>.
This means that <tt class="literal">text+i</tt> (<tt class=
"varname">i</tt> an integer) is also a pointer to a <tt class=
"type">char</tt>, so assigning it to <tt class=
"varname">points_at</tt> is not a sensible operation. A decent
compiler ought to provide a warning about this. In order to change
the value that <tt class="varname">points_at</tt> points to, you
have to defererence it:</p>
<pre class="programlisting">
*points_at = &lt;something&gt;
</pre>
<p>Now since <tt class="varname">points_at</tt> is an <tt class=
"type">int*</tt>, <tt class="varname">*points_at</tt> is an
<tt class="type">int</tt>, therefore <tt class=
"varname">&lt;something&gt;</tt> must be an <tt class=
"type">int</tt> also. The only other <tt class="type">int</tt> you
have in the function is the variable <tt class="varname">i</tt>, so
unless you are really perverse, <tt class=
"varname">&lt;something&gt;</tt> is likely to be <tt class=
"varname">i</tt>. This would mean that the definition of <tt class=
"function">findspace</tt> is that it returns with the index of the
first/last space in <i class="parameter"><tt>text</tt></i> in the
address pointed to by first parameter. You need to find out what
this value ought to be when there is no space in <i class=
"parameter"><tt>text</tt></i> - currently it does not get changed
at all.</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e294" id="d0e294"></a>Answer from the
Harpist:</h4>
</div>
<p>I think this question raises several points that should be
addressed. As we will see this simple four line function is riddled
with bugs.</p>
<p>The first is the explicit question. The question arises from
confusion as to what a parameter is, and a pointer parameter in
particular. Many people find this difficult. The only distinction
between a parameter and a local variable where it is initialised. A
parameter is initialised with a value provided by the calling
function, other local variables have to be initialised from within
the scope of the block in which they are defined.</p>
<p>All local variables die with their current values when you exit
the block in which they were defined. In the case of a parameter
that is effectively at the point of return from the function. This
means that whatever you do to a parameter, even if it is a pointer,
is purely local to the function. Look at your code and you will see
that you make changes to <tt class="varname">points_at</tt>, this
is useless. What you need is to be able to use the address in a
pointer to get at the storage of an object that has existence
outside the local scope. You will know you are doing this because
you will use the <tt class="literal">*</tt> operator to dereference
the pointer to give you the object addressed. In your case you want
to get storage for a <tt class="type">char*</tt> so the parameter
must be initialised with the address of a <tt class=
"type">char*</tt>. Note that, not a <tt class="type">char*</tt> but
the address of one. This tells us that you will need to have a
<tt class="type">char**</tt> parameter which will have to be
dereferenced to get the required storage.</p>
<p>Things like this can be profoundly confusing even to fairly
experienced programmers. One mechanism that can help is to use a
<tt class="literal">typedef</tt> to name the type you are playing
with. We could rewrite the code you wrote as:</p>
<pre class="programlisting">
typedef char * mystring_ptr;
void findspace(mystring_ptr points_at,
               char * text) {
  int i;
  for (i=0; i&lt;strlen(text); i++)
    if (text[i] = ' ') points_at = text+i;
  return;
}
</pre>
<p>In making that change I have not only highlighted the problem by
making it apparent that <tt class="varname">points_at</tt> is
entirely local but I have also fixed another bug that is lurking in
your code. <tt class="literal">text+i</tt> is a <tt class=
"type">char*</tt> which is being silently converted to a <tt class=
"type">int *</tt>. Even in C, I think your compiler should be
giving you a very loud warning. The conversion may change the value
from the that of the <tt class="type">char*</tt> because a pointer
to <tt class="type">char</tt> can be significantly different to a
pointer to <tt class="type">int</tt>. For example some 32-bit
hardware does not provide byte addressing and a <tt class=
"type">char*</tt> consists of an <tt class="type">int</tt> address
together with an offset. When you convert a <tt class=
"type">char*</tt> to an <tt class="type">int*</tt> on such a
machine you lose the offset information.</p>
<p>The fully corrected function is:</p>
<pre class="programlisting">
void findspace(mystring_ptr * points_at,
           char * text) {
  int i;
  for (i=0; i&lt;strlen(text); i++)
    if (text[i] = ' ') *points_at = text+i;
  return;
}
</pre>
<p>Note those two extra <tt class="literal">*</tt>s. They make all
the difference. Before we go on, please study the above until you
are sure you understand it.</p>
<p>Now to the second problem; why are you writing this function?
More precisely, why are you using a write-back parameter (one where
you change the state of the object pointed to by a parameter)
rather than a return value. And why are you returning the address
of the last space in the string pointed to by <tt class=
"varname">text</tt>? And why is <tt class="varname">text</tt> a
<tt class="type">char *</tt> and not a <tt class="type">char const
*</tt>? I cannot answer these questions but unless a code reviewer
asks them the code review has not been done correctly. I think your
code should be:</p>
<pre class="programlisting">
char * findspace(char const * text) {
  int i;
  for (i=0; i&lt;strlen(text); i++)
    if (text[i] = ' ') return (text + i);
  return 0;
}
</pre>
<p>Note that this version will handle a read-only string, and one
without a space in it. But it will not compile in C++ because it is
not <tt class="literal">const</tt> correct. That is another
story.</p>
<p>My final question is why you did not use <tt class=
"function">strchr()</tt> from the Standard C Library? Go and look
it up if you are not already familiar with it.</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e398" id="d0e398"></a>Question 3: What
is Koenig Lookup and what use is it?</h4>
</div>
<p>Can someone explain what on Earth Koenig lookup is? I used to
think I understood overloading in C++ but this seems to be no
longer true.</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e403" id="d0e403"></a>Answer from
Brett Fishburne</h4>
</div>
<p>Koenig lookup functionality is explained in sections 8.2.6 and
11.2.4 of <i class="citetitle">The C++ Programming Language</i>,
3rd Edition . The basic idea is that a lookup for a function (or
operator) is done within the namespace of the arguments if the
function cannot be found within the context of its use, then the
namespace(s) of the arguments are checked.</p>
<p>The algorithm for looking up functions based on argument
namespaces was generated by Andrew Koenig. The intent is that
programmers can take advantage of multiple, diverse namespaces
without having to constantly, explicitly state the namespace.
Unfortunately, I stumbled across a large number of compilers that
do not support Koenig lookup despite its being part of the
standard.</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e413" id="d0e413"></a>Answer from the
Harpist:</h4>
</div>
<p>I am not going to write very much about this because while the
fundamental idea is fine specifying it precisely is extremely
difficult. If you doubt that you should know that there is at least
one defect report registered on this subject against the C++
Standard. In other words the C++ Standards Committees accept that
despite all their years of effort they failed to precisely specify
Koenig lookup so that it met their intent.</p>
<p>The fundamental idea is that the overload set constructed should
include functions that can be found in the namespaces of the
argument types used in a function call. For example:</p>
<pre class="programlisting">
void fn(){
  mytype mt;
  yourtype yt;
  gn(mt, yt);
}
</pre>
<p>When looking for a <tt class="function">gn()</tt> not only
should the current scope be searched but so should namespaces
associated with <tt class="type">mytype</tt> and <tt class=
"type">yourtype</tt>. When it works it is great but many current
compilers only partially support it. In addition more work has to
be done tying down the exact specification.</p>
</div>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e433" id="d0e433"></a>New
Questions:</h2>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e436" id="d0e436"></a>Question 1 from
Colin Hersom <tt class="email">&lt;<a href=
"mailto:colin@hedgehog.cix.co.uk">colin@hedgehog.cix.co.uk</a>&gt;</tt></h3>
</div>
<p>Looking at the fused problem (see the Q&amp;A column in C Vu
12.2), I wondered about how I might try to patch such a problem if
it occurred. Assuming that the reference is necessary in the Fused
class, the solution has to be in the usage of the class. This means
that the value passed into the constructor must not be a temporary,
and the simplest way to change a temporary to something permanent
is to put the word <tt class="literal">static</tt> before the
declaration:</p>
<pre class="programlisting">
Fused *foo() {
  static int const anInt = 4;
  return new Fused(anInt);
}
</pre>
<p>Should be OK now. Strangely, it isn't, at least not using GCC.
If I remove the <tt class="literal">const</tt> keyword, all is OK,
but with <tt class="literal">const</tt> present then the compiler
seems to generate a temporary whether or not <tt class=
"varname">anInt</tt> has function, file, namespace or global
scope.</p>
<p>Is this correct or is GCC in error?</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e461" id="d0e461"></a>Question 2 from:
Claus Wagner <tt class="email">&lt;<a href=
"mailto:Claus.Wagner@ascom.ch">Claus.Wagner@ascom.ch</a>&gt;</tt></h3>
</div>
<p>I need some help in converting legacy C code (<tt class=
"literal">#define</tt> macros) into C++ template classes/functions.
The situation is as follows:</p>
<p>The legacy code has a lot of <tt class="literal">#define</tt>'s
like the following:</p>
<pre class="programlisting">
#define Macro16(X, OPER, Y) ( (*(u16 *)(X))\ 
              OPER (*(u16*)(Y)) ).
</pre>
<p>The macro is called as follows:</p>
<pre class="programlisting">
typedef unsigned char u8;
typedef unsigned short u16;
u16 var1;
u8 var2[2];
Macro16(&amp;var1, =, var2);
</pre>
<p>This copies 2 bytes from <tt class="varname">var2</tt> to
<tt class="varname">var1</tt>. But there may be also other
self-defined data types, such as <tt class="literal">struct</tt>s
containing a <tt class="type">u8</tt> <tt class=
"varname">var3[2]</tt> array etc. Sometimes the <tt class=
"type">u16</tt> is on the left-hand side, sometimes on the
right-hand side. Moreover, there are also 32-bit variants for all
these combinations (i.e. 32-bit integers and 4-byte arrays,
structures containing 4-byte arrays...)</p>
<p>The problem I have is that the macro can also be called with
different operators, e.g., Macro16(&amp;var1, ==, var2). This is
then a check for equality. I found a total of 7 operators, this
macro can be called with: =, ==, !=, &amp;=, |=, &lt;, and
&gt;.</p>
<p>The problem is that this worked fine on a 16-bit architecture,
but on a 32-bit one it crashes. The reason is that an array of four
characters is not necessarily 32-bit aligned as the 32bit integer
is. During the casting and assignment the system segfaults.</p>
<i><span class="remark">[At this point Claus posted his own
temporary fix. I have left that out because I think that a fresh
start might be more helpful. What ideas have you got for solving
this problem. Actually I think I full treatment might be an entire
article, in which case it should be submitted to John Merrells
<tt class="email">&lt;<a href=
"mailto:overload@accu.org">overload@accu.org</a>&gt;</tt> as it is
clearly more appropriate to his readership. ]</span></i></div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e511" id="d0e511"></a>Question 3
posted to comp.lang.c++.moderated</h3>
</div>
<p>Can someone please explain in detail what the -&gt; (arrow
symbol) does in C++</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e516" id="d0e516"></a>Question 4
anon</h3>
</div>
<p>I recently saw the following prototype for a function:</p>
<pre class="programlisting">
matrix * multiply(matrix * restrict m1, 
            matrix * restrict m2);
</pre>
<p>I have looked in all my reference books for both C and C++ and
none of them mention <tt class="literal">restrict</tt> as a
keyword. Can someone tell me whether this is some special dialect
of C or C++ and if so what does <tt class="literal">restrict</tt>
do?</p>
<i><span class="remark">I think this is a good question and not
just for inexperienced programmers. I am not going to give a full
answer here (else there would be little point in publishing the
question here) but I will give a partial answer. restrict is one of
a number of new keywords that were introduced into C in the latest
Standard (often called C99). I leave it to readers to write about
what it is for, what it does and why the UK C Standards panel
insisted on modifications before accepting it as part of the new
standard for C.</span></i></div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e533" id="d0e533"></a>Question 5
anon</h3>
</div>
<p>When I learnt C I was taught that there was no purpose in
qualifying a value with <tt class="literal">const</tt> because
values are always immutable. Now I understand that there can be
some benefit from declaring a parameter as <tt class=
"literal">const</tt> because that allows the compiler to check that
you do not change it. That is a purely local (to the definition)
issue. However I have recently seen C++ member functions declare
their return types as <tt class="literal">const</tt>. Why?</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
