From -564418090617296406
X-Google-Thread: f78e5,1d1109cd86668825,start
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news4.google.com!news.glorb.com!news.alt.net!comp-std-cpp-robomod!not-for-mail
From: "dstevel" <google@lakepage.com>
Newsgroups: comp.std.c++
Subject: strtol const-ness problem
Date: Sat, 12 Aug 2006 15:38:54 CST
Organization: http://groups.google.com
Lines: 64
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <1155392893.026182.121030@h48g2000cwc.googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Trace: posting.google.com 1155392896 12286 127.0.0.1 (12 Aug 2006 14:28:16 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Sat, 12 Aug 2006 14:28:16 +0000 (UTC)
Return-Path: <devnull@stump.algebra.com>
X-Authentication-Warning: mulga.csse.unimelb.edu.au: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Original-To: std-c++@mailman.ucar.edu
Delivered-To: std-c++@mailman.ucar.edu
User-Agent: G2/0.2
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6,gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: h48g2000cwc.googlegroups.com; posting-host=71.193.233.44;
   posting-account=MidBUg0AAAA0r3VZXx8ZSe9LraA2fVzN
X-Virus-Scanned: amavisd-new at ucar.edu
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
Xref: g2news2.google.com comp.std.c++:3253

Note: This was originally posted in comp.lang.c++ and it was
recommended that I post it to std so here it is.
=======================

The signature for strtol is:

strtol( const char*, char**, int)

So.. if we start with a passed "const char*" (pointer to const char),
then we can't create a non-const char pointer pointer to that const
char pointer as in:

void func( const char* a )
{
    // ERROR: invalid conversion from `const char**' to `char**'
    char** pa = &a;
    int i = strtol( a, pa, 10 );

}

And here is another way of saying the same thing without temporary
variables.

void func( const char* a )
{
    // ERROR: invalid conversion from `const char**' to `char**'
    // ERROR: initializing argument 2 of `long int strtol...
    int i = strtol( a, &a, 10 );

}

But you CAN do:

void func( const char* a )
{
    int i = strtol( a, (char**)&a, 10 ); // OK

}

AND you can also do this:

void func( const char* a )
{
    char* tmp;
    int i = strtol( a, &tmp, 10 ); // OK
    a = tmp;

}

This indirectly allows us to modify the original const char* a through
the new pointer tmp since tmp will point into the character array a. It
doesn't involve a hard cast, but it seems just as dangerous or even
more so because it's not obvious what just happened. The calling
function could see a change in  the string pointed to by a, even though
it's passed as pointer to const.

Can someone help me figure out why this is OK?

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]



