Topic: const pointers


Author: Francois Bailly <baillyf@gatwick.Geco-Prakla.slb.com>
Date: 2000/03/10
Raw View
Could someone tell me why this is an error:

void foo(const double **ppArray)
{
    return;
}

int main()
{
    double **ppMyArray;
    // initialize...
    foo(ppMyArray);
    return 0;
}

the compiler (UNIX compiler) says it is an error to pass a double** to a
const double** argument. why is that ?

of course, it is always possible to do an explicit cast

Thanks for your help
Francois B.


======================================= MODERATOR'S COMMENT:

Your question is answered in the FAQ for this newsgroup.
 http://reality.sgi.com/austern_mti/std-c++/faq.html#C1

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: roger_orr@my-deja.com
Date: 2000/03/11
Raw View
In article <38C78ECC.685A6E84@gatwick.geco-prakla.slb.com>,
  Francois Bailly <baillyf@gatwick.Geco-Prakla.slb.com> wrote:
> Could someone tell me why this is an error:
>
> void foo(const double **ppArray)
> {
>     return;
> }
>
> int main()
> {
>     double **ppMyArray;
>     // initialize...
>     foo(ppMyArray);
>     return 0;
> }
>
> the compiler (UNIX compiler) says it is an error to pass a double** to
a
> const double** argument. why is that ?
>
> of course, it is always possible to do an explicit cast
>
> Thanks for your help
> Francois B.
>
> ======================================= MODERATOR'S COMMENT:
>
> Your question is answered in the FAQ for this newsgroup.
>  http://reality.sgi.com/austern_mti/std-c++/faq.html#C1
>
> ---

However the FAQ _doesn't_ tell you what to do about the problem!
Most often foo() is only reading from the array, and in this case
it should be re-specified as:-

void foo(const double * const *ppArray)

Roger Orr.


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: David R Tribble <david@tribble.com>
Date: 2000/03/15
Raw View
Francois Bailly <baillyf@gatwick.Geco-Prakla.slb.com> wrote:
>> the compiler (UNIX compiler) says it is an error to pass a
>> double** to a const double** argument. why is that?

MODERATOR'S COMMENT:
>> Your question is answered in the FAQ for this newsgroup.
>>      http://reality.sgi.com/austern_mti/std-c++/faq.html#C1

roger_orr@my-deja.com wrote:
> However the FAQ _doesn't_ tell you what to do about the problem!
> Most often foo() is only reading from the array, and in this case
> it should be re-specified as:
>
> void foo(const double * const *ppArray)

Indeed.  A related issue is that it is unspecified whether the
contents of the argv pointers to ::main() and the strings they point
to can be modified or not.  Which means some systems will allow it
while others won't, which could lead to portability problems.

My solution is to assume that the argv pointers and strings can't
be modified, and to declare ::main() accordingly:

    int main(int argc, const char *const *argv) { ... }

Unfortunately, there is no guarantee that this is conforming.

To resolve this, I use a very small ::main() which simply invokes
the "real" main function of my program, which is usually in a class
named 'Program':

    int Program::main(int argc, const char *const *argv)
    { ... }

    int main(int argc, char **argv)
    {
        return Program::main(argc, (const char *const *) argv);
    }

The explicit cast is necessary, of course.  The resulting code is
conforming and portable.  (It also has the benefit of being a little
more object-oriented, by placing the main function within a class
that embodies the execution of the whole program.)

-- David R. Tribble, david@tribble.com, http://david.tribble.com --

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]