1

I was looking for some c++ code, and was confused by if(ptr && *ptr), what does it do in this case?

// Process the data here.  We just break the data into separate pieces and 
// display it for the sake of simplicity.
char * a_pszBreak = NULL;
char * a_pszDataItem = (char*)s_aucDataBuffer;
do
{
    // The poll data is terminated by either a Carriage Return alone, or a
    // Carriage Return/Line Feed pair.
    a_pszBreak = strpbrk(a_pszDataItem, "\n\r");
    if (a_pszBreak && *a_pszBreak)
    {
        *a_pszBreak = 0;
        a_pszBreak++;
        LogPollData((const char *)a_pszDataItem);
    }
    a_pszDataItem = a_pszBreak;
} while (a_pszBreak && *a_pszBreak);
lorond
  • 3,856
  • 2
  • 37
  • 52
Brandon
  • 21
  • 6
  • It's a method to prevent incorrectly dereferencing a null pointer. The && short circuits, so if `ptr` is NULL `*ptr` is never attempted (it would segfault if it did). – William Pursell Jul 11 '16 at 13:03
  • 1
    You do know that you can dereference (with the dereference operator `*`) a `NULL` pointer? And you do know that the logical operators `&&` and `||` do [short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation)? And you know that a `NULL` pointers is considered as "false"? And you know that strings are terminated with a zero? And that any non-zero thing is considered "true"? If you know all those five things then put that knowledge together and think a little bit more. – Some programmer dude Jul 11 '16 at 13:04
  • Don't use pointers in C++. It's not 1995 any more. The code you are reading is highly antiquated. – Lightness Races in Orbit Jul 11 '16 at 13:56

3 Answers3

6

it means that the pointer should point to something and in addition that something must be different from 0.

It's like (a_pszBreak != nullptr && a_pszBrealk[0] != '\0')

Jack
  • 131,802
  • 30
  • 241
  • 343
0
if (a_pszBreak && *a_pszBreak)

In C++, anything that can be interpreted as 0 is false and anything else is considered true for the purpose of statements.

So this ensures that the pointer is not NULL and that the contents of the first element the pointer points to is not null or 0 either.

Due to short circuiting, if the pointer is NULL, the second part will not be checked.

So in plain english, this if-statement checks if the pointer is not a nullptr and if the pointer does not point to an empty string.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

In if (a_pszBreak && *a_pszBreak) a_pszBreak is used to check whether a_pszBreak points to valid memory area i.e a_pszBreak is not NULL and *a_pszBreak is used to check whether memory pointed by a_pszBreak does not start with NULL character i.e \0.

In short if (a_pszBreak && *a_pszBreak) is used to check whether a_pszBreak points to a string of length atleast 1.

sameerkn
  • 2,209
  • 1
  • 12
  • 13
  • Actually `if (a_pszBreak)` doesn't give you any guarantees that pointer is valid, only that it is not `nullptr` so that actually holds an address. Whether that address is valid or not is a different story. – Jack Jul 11 '16 at 16:45