Can someone explain why this example works:
bool SomeFunction(const char * ipIpAddress, int &opOct1, int &opOct2,
int &opOct3, int
&opOct4)
{
int b1, b2, b3, b4;
unsigned char c;
if (sscanf(ipIpAddress, "%3i.%3i.%3i.%3i%c", &b1, &b2, &b3, &b4,
&c) != 4)
{
return false;
}
if ((b1 | b2 | b3 | b4) 255)
{
return false;
}
if (strspn(ipIpAddress, "0123456789.") < strlen(ipIpAddress))
{
return false;
}
opOct1 = b1;
opOct2 = b2;
opOct3 = b3;
opOct4 = b4;
return true;
}
but this one does not? (Segmentation Fault)
bool SomeFunction(const char * ipIpAddress, int &opOct1, int &opOct2,
int &opOct3, int
&opOct4)
{
unsigned char c;
if (sscanf(ipIpAddress, "%3i.%3i.%3i.%3i%c", opOct1, opOct2,
opOct3, opOct4, &c) != 4)
{
return false;
}
if ((b1 | b2 | b3 | b4) 255)
{
return false;
}
if (strspn(ipIpAddress, "0123456789.") < strlen(ipIpAddress))
{
return false;
}
return true;
}
In each case, I am passing references to int's to the sscanf
function.
Thanks,
AJ 4 1606
aj wrote:
Can someone explain why this example works:
bool SomeFunction(const char * ipIpAddress, int &opOct1, int &opOct2,
int &opOct3, int
&opOct4)
{
int b1, b2, b3, b4;
unsigned char c;
if (sscanf(ipIpAddress, "%3i.%3i.%3i.%3i%c", &b1, &b2, &b3, &b4,
&c) != 4)
OK. sscanf expects pointers.
[...]
but this one does not? (Segmentation Fault)
bool SomeFunction(const char * ipIpAddress, int &opOct1, int &opOct2,
int &opOct3, int
&opOct4)
{
unsigned char c;
if (sscanf(ipIpAddress, "%3i.%3i.%3i.%3i%c", opOct1, opOct2,
opOct3, opOct4, &c) != 4)
Error: sscanf expects pointers, but opOctN are ints. This should be:
if (sscanf(ipIpAddress, "%3i.%3i.%3i.%3i%c", &opOct1, &opOct2, &opOct3,
&opOct4, &c) != 4)
In each case, I am passing references to int's to the sscanf
function.
In some situations, pointers are called references. But in C++, pointers and
references are distinct creatures. In C++ a reference is an alias of
another object, and once initialised, it behaves exactly as if it is the
referenced object. As you can see, if you need a pointer, you can take the
address of the reference variable.
--
rbh
Thanks Robert. You are very correct. I figured the compiler would
recognize that I was passing references (pointers) to the function,
but it was not. It seems unusual that I need to get the pointer of a
reference, when I figured the original reference would do, but I will
live with it :)
Thanks,
AJ
On Jun 25, 3:00 pm, Robert Bauck Hamar <roberth+n...@ifi.uio.no>
wrote:
aj wrote:
Can someone explain why this example works:
bool SomeFunction(const char * ipIpAddress, int &opOct1, int &opOct2,
int &opOct3, int
&opOct4)
{
int b1, b2, b3, b4;
unsigned char c;
if (sscanf(ipIpAddress, "%3i.%3i.%3i.%3i%c", &b1, &b2, &b3, &b4,
&c) != 4)
OK. sscanf expects pointers.
[...]
but this one does not? (Segmentation Fault)
bool SomeFunction(const char * ipIpAddress, int &opOct1, int &opOct2,
int &opOct3, int
&opOct4)
{
unsigned char c;
if (sscanf(ipIpAddress, "%3i.%3i.%3i.%3i%c", opOct1, opOct2,
opOct3, opOct4, &c) != 4)
Error: sscanf expects pointers, but opOctN are ints. This should be:
if (sscanf(ipIpAddress, "%3i.%3i.%3i.%3i%c", &opOct1, &opOct2, &opOct3,
&opOct4, &c) != 4)
In each case, I am passing references to int's to the sscanf
function.
In some situations, pointers are called references. But in C++, pointers and
references are distinct creatures. In C++ a reference is an alias of
another object, and once initialised, it behaves exactly as if it is the
referenced object. As you can see, if you need a pointer, you can take the
address of the reference variable.
--
rbh
aj wrote:
Thanks Robert.
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
aj wrote:
1. Do not top post.
Thanks Robert. You are very correct. I figured the compiler would
recognize that I was passing references (pointers) to the function,
but it was not. It seems unusual that I need to get the pointer of a
reference, when I figured the original reference would do,
2. Repeat after me. A reference is *NOT* a pointer. A reference is
*NOT* a pointer.
An implementation may choose to implement them as such, but that is
irrelevant. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Aric |
last post by:
Hi,
I'm a bit new to programming in general, really I've just picked it up
as a hobby, and so I've started by reading "Practical C Programming"
by Steve Oualline. Things have been going fine in...
|
by: baumann |
last post by:
hi,
1) first test program code
#include <stdio.h>
int main(void)
{
char * file = "aaa 23 32 m 2.23 ammasd";
int i2,i3;
|
by: baumann |
last post by:
hi all
there has 2 program
1) the first test program code
#include <stdio.h>
int main(void)
{
|
by: jchludzinski |
last post by:
I'm using strtok() to parse thru a line and read different numbers:
float value;
char *token;
token = strtok( line, " " );
....
sscanf( token, "%f", &value );
These results are less...
|
by: Superfox il Volpone |
last post by:
Hello
I have some problem with sscanf, I tryed this code but it doesn't works
:
char* stringa = "18/2005"
char mese; char anno;
int i_letture;
i_letture = sscanf(stringa, "%2s/%4s",...
|
by: suzee_q00 |
last post by:
I will admit that lots of times the obvious eludes me and this is most likely one of those times but if anyone has any ideas on what I can do to make this code work, I would greatly appreciate it....
|
by: Artemio |
last post by:
Dear folks,
I need some help with using the sscanf() function. I need to parse a
string which has several parameters given in a "A=... B=... C=..." way,
and each has a different type (one is a...
|
by: AMP |
last post by:
Hello,
Anybody know if anything exists like sscanf in c.
I found a few things OL but most were pretty old. Maybe something has
come along since 2004?
Thanks
Mike
|
by: jlm699 |
last post by:
I've looked at the other articles about maps of maps and am still stuck on this!
I'm trying to basically make an enumeration of a data monitoring app. Instead of displaying numbers for certain...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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,...
| |