Problem: if you do
if ( ptr = memchr( str1, '4', sizeof(str1) ) )
{
// do stuff using ptr
}
then you’ll enter and do “stuff” every time.
But if you just do
if ( memchr( str1, '4', sizeof(str1) ) )
{
// would do stuff but don't have ptr!
}
then you’ll enter but you won’t have the pointer to where ‘4’ is inside str1.
I need to evaluate and do stuff for several possibilities ie if ‘4’, else if ‘7’, else if ‘1’, etc.
So what’s the most efficient method to both evaluate if that character exists, and use the pointer returned if it does? Surely it’s not
if ( memchr( str1, '4', sizeof(str1) ) )
{
ptr = memchr( str1, '4', sizeof(str1) )
// do stuff using ptr
}
The assignment operator returns the value being assigned, which (along with right associativity) is why
a=b=c;works. So your first example will work just fine.