Topic: const source of confusion
Author: "Bradd W. Szonye" <bradds@concentric.net>
Date: 1997/12/15 Raw View
Olle Bjurstam wrote in message <3490FE67.4B8C@senet.abb.se>...
>
>I expect to
>be able to call function f() with a char* [] just as I'm able
>to call a function taking a const char* with an argument of
>type char*.
The problem:
char * * is convertible to char const * const *, but
char * * is not convertible to char const * *
The reason:
char const a[] = "This is const.";
char * pn = NULL; // safe
char * * pnn = &pn; // safe
char const * * pcn = pnn; // safe?
*pcn = a; // "safe" by type rules, but assigns pn = a;
*pn = '\0'; // wham
Roughly, allowing the conversion that intuitively looks reasonable allows
you to point a non-const pointer at const data without ever casting. The
declaration you want for your "names" function is:
void f(char const * const names[], size_t n);
It works fine for your purpose, since you don't intend to modify the names
or the pointers to the names in the array.
To jump on the "standard library bandwagon," the best answer of course is
not to play with pointers or arrays, but to use (std::) strings and vectors
if possible. While the library classes aren't always entirely sane, for
everyday purposes they're less likely to bite you.
void f(vector<string> const & names)
{
typedef vector<string>::const_iterator iter;
for (iter it = names.begin(); it != names.end(); ++it)
cout << *it << endl;
}
---
Bradd W. Szonye
bradds@concentric.net
http://www.concentric.net/~Bradds
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1997/12/16 Raw View
Olle Bjurstam wrote:
>
> Could someone please explain this to me:
>
> I have this function f() taking an array of pointers to const
> char. (Or?)
To be exact, you have a pointer to a pointer to const char.
In function declarations, xxx[] is translated to *xxx by the compiler.
This is usually summarized as "arrays are broken in C++".
>
> void f( const char* names[], size_t n )
> {
> size_t ii;
> for( ii = 0; ii < n; ++ii )
> cout << names[ii] << endl;
> }
>
> When I call the function f() an error is provoked unless I also
> declare the argument variable as const char* []. I expect to
> be able to call function f() with a char* [] just as I'm able
> to call a function taking a const char* with an argument of
> type char*.
>
> void main()
> {
> char* n1[] = { "Olle", "Joel", "Stina" };
> const char* n2[] = { "Olle", "Joel", "Stina" };
>
> f( n1, sizeof( n1 )/sizeof( n1[0] ) ); // Error!
> f( n2, sizeof( n2 )/sizeof( n2[0] ) ); // Fine
> }
>
> This behaviour is consistent between two compilers
> (Visual C++ 5.0) and a DEC C++ compiler of fairly recent date.
>
> Clearly there is something I don't understand here, but what?
> Why do I have to declare char* n1[] to be const in the caller
> of f()?
>
Try to change the definition of f to
void f(char const* const names[], size_t n)
(you really don't alter the names pointer itself),
then your code should work.
AFAIK consts can only be applied "from right to left" (with exception of
the
last item):
char** -> char*const* -> char const*const*
Leaving out the right const doesn't work:
char** -/-> char const**
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Olle Bjurstam <olle.bjurstam@senet.abb.se>
Date: 1997/12/13 Raw View
Could someone please explain this to me:
I have this function f() taking an array of pointers to const
char. (Or?)
void f( const char* names[], size_t n )
{
size_t ii;
for( ii = 0; ii < n; ++ii )
cout << names[ii] << endl;
}
When I call the function f() an error is provoked unless I also
declare the argument variable as const char* []. I expect to
be able to call function f() with a char* [] just as I'm able
to call a function taking a const char* with an argument of
type char*.
void main()
{
char* n1[] = { "Olle", "Joel", "Stina" };
const char* n2[] = { "Olle", "Joel", "Stina" };
f( n1, sizeof( n1 )/sizeof( n1[0] ) ); // Error!
f( n2, sizeof( n2 )/sizeof( n2[0] ) ); // Fine
}
This behaviour is consistent between two compilers
(Visual C++ 5.0) and a DEC C++ compiler of fairly recent date.
Clearly there is something I don't understand here, but what?
Why do I have to declare char* n1[] to be const in the caller
of f()?
Thanx in advance.
,,,
--------------ooO-(. .)-Ooo--------------------------------------
Olle Bjurstam (_) | Email: olle.bjurstam@senet.abb.se
Dept. TA | Phone: +46 21 324147
ABB Network Partner AB | Fax : +46 21 188291
S-721 71 VASTERAS, SWEDEN | Telex: 40849 ABBNET S
"You can make it foolproof, but you can't make it damnfoolproof."
-----------------------------------------------------------------
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]