Why passing references to sscanf doesn't work.

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
 
B

Ben Pfaff

aj said:
Can someone explain why this example works:

bool SomeFunction(const char * ipIpAddress, int &opOct1, int &opOct2,
int &opOct3, int &opOct4)

This is C++ code, so comp.lang.c++ would be a better place to ask
about it.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top