473,395 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

strtol const-ness problem

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?

Aug 12 '06 #1
3 3197
"dstevel" <go****@lakepage.comschrieb im Newsbeitrag
news:11**********************@m73g2000cwd.googlegr oups.com...
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 ...
In my opinion, this to be a bug in the standard. For many other functions
inherited from C, the standard has defined two functions with const and
non-const arguements, but strtol seems to be missing in that group. There
should better be two different overloads:

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

Perhaps you should post your message to comp.std.c++.

[...]
Can someone help me figure out why this is OK?
I'm curious to see if anyone could...

Heinz

Aug 12 '06 #2
not a direct answer, but I would consider what the faq says about
converting a string to a number:
http://www.parashift.com/c++-faq-lit....html#faq-39.3

dstevel wrote:
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?
Aug 12 '06 #3
Thanks for the link. The nice part about strtol compared with all of
those convert functions is that strtol only converts the number at the
first portion of the string, then returnsa pointer to the rest of the
string in the char** you provide to it.

I'm using it to pick out the numbers in a long string that contains a
mix of letters and numbers like:

"ABC1234/CDEF44/XYZ/275B"

and my program picks out 1234, 44, 275

It goes through a string 1 character at a time, and if it finds a digit
it called strtol on the current character to get the full number. Then
it continues examining the non-digits at the point where strtol
indicates the number ended. I don't know that any of those convert
functions do that. They seem to expect the full string to be a valid
number like "12345" = 12345.


wi******@hotmail.com wrote:
not a direct answer, but I would consider what the faq says about
converting a string to a number:
http://www.parashift.com/c++-faq-lit....html#faq-39.3

dstevel wrote:
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?
Aug 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Pavel Krcmar | last post by:
Hi, I'am a little bit confused. I tried this snippet (below) and ( endptr != NULL ) is true everytime. I was looking to http://www.mkssoftware.com/docs/man3/strtol.3.asp and then to...
6
by: Amadeus W.M. | last post by:
Does strtol raise any exceptions? Thanks!
13
by: Matthias Kluwe | last post by:
Hi! In C, my everyday usage of strtol looked like using char *end; strtol( text, &end, 10 ); to read an int/long and then checking end == ( text + strlen( text ) );
5
by: William Payne | last post by:
Hello, I am in the process of converting a C++ program to a C program. The user of the program is supposed to supply an integer on the command line and in the C++ version of the program I was using...
11
by: nrk | last post by:
Isn't: char s = "--4"; char *endptr; strtol(s, &endptr, 0); supposed to return 0 and set endptr to s? I have run into an implementation (not gcc, gcc does what I expect) that is returning...
10
by: Peter Dunker | last post by:
Hi, I will check a String which should contain a HEX value. I know that strtol is the right function for this job. But what happens when I will check a hex string with 8 bytes? That I can...
16
by: David Scarlett | last post by:
Another two questions... Is behaviour defined when the first argument of strtol is NULL? And if the string contains only digits, is the 2nd argument (assuming it wasn't NULL) guaranteed to be...
2
by: Marlene Stebbins | last post by:
Suppose I'm using strtol() to convert a command line string to a number and I want to check that the input to strtol() is not non-numeric. strtol() returns zero if input is non-numeric, so I can...
14
by: Kristo | last post by:
Since there's no strtoi function in standard C, I've been searching the clc archives for the proper way to store the result of strtol to an int. My search has yielded conflicting results. About...
8
by: lovecreatesbea... | last post by:
Does this part of C code call and check strtol() correctly? port = strtol(argv, &endptr, 10); if (argv == endptr){ fprintf(stderr, "%s\n", "Invalid port number form"); return 1; } if (port ==...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.