First thing, note that the word "null" is overloaded. You can have null pointers and null (empty) strings, and there's the null character ('\0', equal to 0 when converted to an int:((int)'\0') == 0). There are also uninitialized variables, which may or may not be null. I'm guessing you're talking about an uninitialized character array, used as a c-string.
Most likely, hName is being allocated on the stack (I can't tell without seeing more of the source code), which means it's not zero-initialized. Practically speaking, hName will hold whatever data was last stored in the region of memory that hName occupies. You'll need to initialize it yourself.
char hName[255] = {0};
// or
memset(hName, 0, sizeof(hName));
// or, if you have bzero
bzero(hName, sizeof(hName));
Also note that since hName is declared as an array rather than a pointer, sizeof(hName) is the number of characters it stores.
void test() {
char *name1 = "";
char name2[255];
// All the following lines will be true
strlen(name1) == 0;
sizeof(name2) == 255
0 <= strlen(name2) && strlen(name2) < 255;
// pointers are 4 or 8 bytes on most machines these days
sizeof(name1) == 4 || sizeof(name1) == 8;
}
Edit (addressing code sample):
The length of str in getHost is 255 because you tell it to have that length when you copy from tTemp.hName. NSStrings can contain nulls, though you may have difficulty printing them and any characters following.
It's not clear from the code sample if hName is a global (globals are bad) or a property. Similarly, the scope of the other variables, such as haddr and tTemp, is unclear. Some of those should be parameters to the methods.
The name "setHost" should be reserved for a setter–one of a pair of methods ("accessors", in Objective-C parlance) that get and set a property. They return and take (respectively) a type that's notionally the type of the property. In this case, NSString* makes the most sense; best to use an NSString in your code and switch to (via NSString's cStringUsingEncoding: or UTF8String). The partner to -(void)setHost:(NSString*) would be -(NSString*)host.
Once you make the switch to NSString (and use stringFromCString:withEncoding:), you can simply examine its length or compare it to @"" to check for an empty string.
@interface MyHost : NSObject {
NSString *name_;
...
}
/* post ObjC 2.0 */
@property(retain) NSString* name;
/* pre ObjC 2.0 */
-(NSString*)name;
-(void)setName:(NSString*);
/* any ObjC version */
-(int)setHostFromAddress:(MyAddress*)addr;
...
@end
@implementation MyHost
/* post ObjC 2.0 */
@synthesize name = name_;
-(int)setHostFromAddress:(MyAddress*)addr {
struct hostent *phost;
phost = gethostbyaddr(addr.address, addr.length, addr.type);
if (phost) {
self.name = [NSString stringWithCString:phost->h_hname encoding:NSASCIIStringEncoding];
}
return h_errno;
}
/* pre ObjC 2.0 */
-(NSString*)name {
return name_;
}
-(NSString*)setName:(NSString*)nom {
[name_ release];
name_ = [nom retain];
}
-(int)setHostFromAddress:(MyAddress*)addr {
struct hostent *phost;
phost = gethostbyaddr([addr address], [addr length], [addr type]);
if (phost) {
[self setName:[NSString stringWithCString:phost->h_hname encoding:NSASCIIStringEncoding]];
}
return h_errno;
}
...
@end