const char ** syntax question

  • Follow


What is wrong with line 4 of the program shown below?
Visual Studio 7.1 compiles it without incident.  gcc 4.1.0 reports an
error.

int main()
{
char * p = "abc";
const char ** paddr = &p;
return 0;
}

Dinkum Exam? page results:

Your code has been compiled with the EDG compiler using
the Dinkum C++ library from the Dinkum Compleat Libraries package.

--------------------

"sourceFile.cpp", line 4: warning:
       a value of type "char **" cannot be used to initialize an
entity of
       type "const char **"
  const char ** paddr = &p;
                        ^

Ken

{ Please see FAQ [18.17] at http://www.parashift.com/c++-faq-lite/ -mod/sk }

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply salter (1) 10/10/2007 5:10:56 PM

<salter@icdc.com> wrote in message
news:1192030356.354157.20440@22g2000hsm.googlegroups.com...
> What is wrong with line 4 of the program shown below?
> Visual Studio 7.1 compiles it without incident.  gcc 4.1.0 reports an
> error.
>
> int main()
> {
> char * p = "abc";
> const char ** paddr = &p;
> return 0;
> }

It's the old "Just what is constant?" question.  Change it to

char* const * paddr = &p;

and it will compile.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Jim 10/11/2007 12:01:55 AM


salter@icdc.com wrote:
> char * p = "abc";
> const char ** paddr = &p;

There are two independant errors here, one is explained by the FAQ, as the
moderator already suggested.

Firstly, you shouldn't assign a string literal ("abc") to a pointer to
non-const char. This is only supported for backward compatibility with old
C dialects. Writing through the pointer is illegal anyway, but without the
const the responsibility to assure that is with you.

Secondly, and that is the reason that the compiler is complaining, is that
you can't convert a 'char**' to a 'char const**', but that part is
explained by the FAQ.

Uli


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Ulrich 10/12/2007 3:14:32 AM

On Oct 11, 2:01 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> <sal...@icdc.com> wrote in message
>
> news:1192030356.354157.20440@22g2000hsm.googlegroups.com...
>
> > What is wrong with line 4 of the program shown below?
> > Visual Studio 7.1 compiles it without incident.  gcc 4.1.0 reports an
> > error.
>
> > int main()
> > {
> > char * p = "abc";
> > const char ** paddr = &p;
> > return 0;
> > }
>
> It's the old "Just what is constant?" question.  Change it to
>
> char* const * paddr = &p;
>
> and it will compile.
>
Our software includes several open source libraries,
and runs on at least six platforms.  An interface to
one open source library takes
(in an early version)	      char ** p
(in a  later version)	const char ** p
I would like a way to write code that operates with
either version.  (Running the same software on all
machines is as difficult as synchronizing clocks.)

I will send a note, with of copy of FAQ 18.17, to the open
source developers.

Taking terminology from the FAQ, I wish the conversion
were merely immoral, not illegal.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply salter 10/12/2007 3:23:07 AM

Hi

salter@icdc.com wrote:

> Our software includes several open source libraries,
> and runs on at least six platforms.  An interface to
> one open source library takes
> (in an early version)       char ** p
> (in a  later version) const char ** p

Looks like they fixed the API. Maybe they want to set the parameter to some
static strings...

> I would like a way to write code that operates with
> either version.  (Running the same software on all
> machines is as difficult as synchronizing clocks.)

Write your own wrapper that switches depending on the library version
(either dynamically or through a macro)

void wrapper(char **p)
{
#if defined(OLDAPI)
   api_func(p);
#else
   static char *buffer = 0;
   const char *cp = *p;
   api_func(&cp);
   free(buffer);
   buffer = static_cast<char*>( malloc(strlen(cp)) );
   *p = buffer;
#endif
}

Of course, I have just guessed api_func's behavior.

Markus


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Markus 10/12/2007 11:47:04 AM

On Oct 12, 2:23 am, sal...@icdc.com wrote:
> On Oct 11, 2:01 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
>
> > <sal...@icdc.com> wrote in message
>
> >news:1192030356.354157.20440@22g2000hsm.googlegroups.com...
>
> > > What is wrong with line 4 of the program shown below?
> > > Visual Studio 7.1 compiles it without incident.  gcc 4.1.0 reports an
> > > error.
>
> > > int main()
> > > {
> > > char * p = "abc";
> > > const char ** paddr = &p;
> > > return 0;
> > > }
>
> > It's the old "Just what is constant?" question.  Change it to
>
> > char* const * paddr = &p;
>
> > and it will compile.
>
> Our software includes several open source libraries,
> and runs on at least six platforms.  An interface to
> one open source library takes
> (in an early version)         char ** p
> (in a  later version)   const char ** p
> I would like a way to write code that operates with
> either version.  (Running the same software on all
> machines is as difficult as synchronizing clocks.)

You could write a wrapper class that would match either parameter
declaration - if you really wanted to go to such lengths:

     struct CPtrPtr
     {
         CPtrPtr(char **p) : p_(p) {}

         operator char **() { return p_; }
         operator const char **()
         {
             return (const char **) p_;
         }

     private:
         char **p_;
     };

and sample usage:

     void f1(char **p) {}
     void f2(const char **p) {}

     int main()
     {
         char *p;

         CPtrPtr paddr = &p;

         f1(paddr);
         f2(paddr);

         // or

         f1( CPtrPtr(&p));
         f2( CPtrPtr(&p));
     }

Greg



-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Greg 10/13/2007 10:25:43 PM

5 Replies
299 Views

(page loaded in 0.471 seconds)

Similiar Articles:













7/23/2012 1:38:56 PM


Reply: