Trouble with references and sscanf.

A

aj

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
 
R

Robert Bauck Hamar

aj said:
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.
 
A

aj

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



aj said:
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.
 
R

red floyd

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top