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? 3 3219
"dstevel" <go****@lakepag e.comschrieb im Newsbeitrag
news:11******** **************@ m73g2000cwd.goo glegroups.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
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?
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******@hotmai l.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?
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
http://www.cplusplus.com/ref/cstdlib/strtol.html
The second www specifies things like they are done - endptr is not NULL.
Is something wrong?
Best Regards,
|
by: Amadeus W.M. |
last post by:
Does strtol raise any exceptions?
Thanks!
|
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 ) );
|
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 something called stringstreams
to do the conversion. Here's my C version, can I leave it as it is or does
it need to be robustified or changed in any manner, regarding error
checking?
char* endptr;
errno = 0;
|
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 LONG_MIN!! I am afraid
to see what it did to endptr :-)
| |
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 check that the string is correct i know.
long value;
char src="0x1234567812345678"
|
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 set to a pointer to a pointer to '\0'?
Thanks.
|
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 write something like this:
if((x = strtol(argv, NULL, 10))==0)
exit(EXIT_FAILURE);
That catches non-numeric input alright, but zero is a perfectly
good number that might be input to my program. How can I use
|
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 half of the
results say to cast the result to an int. The other half say the cast
isn't necessary. And the third half say to compare the result to
INT_MAX and INT_MIN before assigning. Which is correct?
Kristo
|
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 && errno == EINVAL){
perror("strtol()");
return 1;
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |