This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Type promotion bug?

Specific pointer -> generic pointer -> boolean (i.e. implicit compare to 0) seems not to work as expected.

void main()
{
  char* p;
  char* q;
  char xdata* xp = NULL;

  bit fail = FALSE;

  // This fails.
  if ( p = xp )
  {
    fail = TRUE;
  }

  // This is ok.
  if ( ( q = xp ) == NULL )
  {
    fail = TRUE;
  }
}

Parents
  • Jon,

    Thanks so much for this posting. It really hit home regarding the NULL value problem even after I had read the section on generic pointers and conversion.
    The issue to me is that during the comparison I am looking at the memory-type, which I do not care about. Here is a suggestion that might work.

    char * var;
    if ((unsigned short) var == (unsigned short) NULL) {}
    
    This will strip out the memory types on both sides and then a valid comparison can be made. What do you think?
    I am interested in getting feedback as my pointers could be any memory-type and I would like a generic means of checking for NULL.

    Robert

Reply
  • Jon,

    Thanks so much for this posting. It really hit home regarding the NULL value problem even after I had read the section on generic pointers and conversion.
    The issue to me is that during the comparison I am looking at the memory-type, which I do not care about. Here is a suggestion that might work.

    char * var;
    if ((unsigned short) var == (unsigned short) NULL) {}
    
    This will strip out the memory types on both sides and then a valid comparison can be made. What do you think?
    I am interested in getting feedback as my pointers could be any memory-type and I would like a generic means of checking for NULL.

    Robert

Children