1

I'm trying to test if the value stored in a particular memory address is NULL, I have this and in debug it's showing the char value in that address as '\0'. Yet it's always skipping over this IF statement.

Is this the right syntax to use? I have ensured that the address is set to null.

if (test->address + length == NULL)
{
    ..code..
}
3
  • 3
    NULL is a null pointer, '\0' is a null character. Commented Apr 14, 2015 at 13:39
  • What is the type of address? Commented Apr 14, 2015 at 13:42
  • 6
    What about using a std::string if you are in C++ ? Commented Apr 14, 2015 at 13:44

3 Answers 3

6

Assuming address is a char* pointer (or a char[] array), you need to dereference it to access the char values. That's

*(test->address + length)

or equivalently

test->address[length]
Sign up to request clarification or add additional context in comments.

Comments

4

If I have understood you correctly then the valid statement will look like

if ( *( test->address + length ) == '\0' )
{
    ..code..
}

Or

if ( test->address[length] == '\0' )
{
    ..code..
}

provided that type of object test->address either defined as a character array or a pointer of type char *

Comments

1

Of course it skips this if. That's because pointers arithmetic rules.

  • test->address is a pointer
  • test->address + length is test->address + sizeof(T) * length, where T is the type of address.

Change:

if (test->address + length == NULL)
{
    ..code..
}

to

if (test->address[length] == 0)
{
    ..code..
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.