0

I have a C array declared like char* arrayOfVarNames[3];

This array is populated by some api and the strings in this array are again used by an objective-C method.

I want to know if there are any null strings in the arrayOfVarNames[3] before passing to the objective-C method. NSString *tempString = [NSString stringWithCString:arrayOfVarNames[i] encoding:NSUTF8StringEncoding]; Anyway to check for null strings?

2
  • If you mean empty strings, then strlen(the_char_ptr)==0. If you mean null pointers, then the_char_ptr==NULL. Commented Sep 16, 2014 at 15:36
  • Easier still !*the_char_ptr resp. !the_char_ptr. Commented Sep 16, 2014 at 15:44

3 Answers 3

2

A simple if statement:

NSString *tempString = nil;
if (arrayOfVarNames[i]) {
    // It's not NULL
    tempString = [NSString stringWithCString:arrayOfVarNames[i] encoding:NSUTF8StringEncoding];
} else {
    tempString = @""; // or some other appropriate action
}
Sign up to request clarification or add additional context in comments.

Comments

0

One line code solution:

NSString *tempString = NULL == arrayOfVarNames[i] ? nil : [NSString stringWithCString:arrayOfVarNames[i] encoding:NSUTF8StringEncoding];

Comments

0

Depending on what you mean with a null string, you test for:

  • The pointer being NULL: !p
  • The pointer pointing to an empty string: !*p

Beware that some interfaces handle those cases as interchangeable, so that you must accept both as an empty string.

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.