Hello all,
I have been playing with an issue for hours now and can't figure it out. Basically, I am writing a class, and trying to initialize an instance variable that has a user-defined type (C struct). If I declare it as a direct variable (without a "*"), I can assign a value to it in init. But if I make it a pointer, my app compiles fine, but freezes when I try to assign a value. Here is a sample that reproduces the error:
This code compiles just fine. But it crashes where the comments indicate.
Can anyone out there help me assign a value to "yesPointer->subVar" without it crashing?
(and I'd also love to know why I need to use a "." for the noPointer variable, and a "->" for the yesPointer variable )
I have been playing with an issue for hours now and can't figure it out. Basically, I am writing a class, and trying to initialize an instance variable that has a user-defined type (C struct). If I declare it as a direct variable (without a "*"), I can assign a value to it in init. But if I make it a pointer, my app compiles fine, but freezes when I try to assign a value. Here is a sample that reproduces the error:
Code:
typedef struct Mystruct_s {
int subVar;
} Mystruct_t;
@interface MyClass : NSObject {
Mystruct_t noPointer;
Mystruct_t *yesPointer;
}
-(MyClass*)init;
@end
@implementation MyClass
-(MyClass*) init {
self = [super init];
if ( self ) {
noPointer.subVar = 123; // works just fine
yesPointer->subVar = 123; // compiles, but program chrashes here. mem not allocated?
}
return self;
}
@end
This code compiles just fine. But it crashes where the comments indicate.
Can anyone out there help me assign a value to "yesPointer->subVar" without it crashing?
(and I'd also love to know why I need to use a "." for the noPointer variable, and a "->" for the yesPointer variable )