Happy New Year everyone!
Hi there! I'm hoping that someone can help me out with the question below. I'm trying to understand pointers in Objective-C and am having a little difficulty in understanding the following. I'll be most grateful if someone can help. Many thanks in advance.
Olya.
From what I understand (and please correct me if I'm wrong):
int x, count = 10;
int *hello;
hello = &count;
x = *hello;
Here the variables x and count are declared to be of type integer. Additionally, the variable count is assigned the value of 10.
hello is a pointer to type integer. hello is then assigned the address of count.
In order to access the value of count, hello must have an asterisk in front of it, ie, *hello.
So, x is assigned the value of whatever is in count and in this case, 10.
However...
Fraction *myFraction = [[Fraction alloc] init];
[myFraction someMethod];
Here, if I understand correctly, myFraction is a pointer to an instance of Fraction class.
myFraction is pointing to (or rather assigned the address of) an object which has been assigned memory and initialised.
Surely, in order to access the object that myFraction points to, I ought to write:
[*myFraction someMethod];
Given the way in which x accessed the value of count, surely in order to access the object, one ought to write this and not:
[myFraction someMethod];
In addition, if I have
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *str = @"Programming can be a headache";
NSLog (@"%@\n", str);
Why is str being treated as an object above? Or is str an object and in which case, why would I want to make it point to an instance of NSString class? Surely, I ought to be able to just assign an object to str?
Thank you again in advance.
Happy New Year.
Hi there! I'm hoping that someone can help me out with the question below. I'm trying to understand pointers in Objective-C and am having a little difficulty in understanding the following. I'll be most grateful if someone can help. Many thanks in advance.
Olya.
From what I understand (and please correct me if I'm wrong):
int x, count = 10;
int *hello;
hello = &count;
x = *hello;
Here the variables x and count are declared to be of type integer. Additionally, the variable count is assigned the value of 10.
hello is a pointer to type integer. hello is then assigned the address of count.
In order to access the value of count, hello must have an asterisk in front of it, ie, *hello.
So, x is assigned the value of whatever is in count and in this case, 10.
However...
Fraction *myFraction = [[Fraction alloc] init];
[myFraction someMethod];
Here, if I understand correctly, myFraction is a pointer to an instance of Fraction class.
myFraction is pointing to (or rather assigned the address of) an object which has been assigned memory and initialised.
Surely, in order to access the object that myFraction points to, I ought to write:
[*myFraction someMethod];
Given the way in which x accessed the value of count, surely in order to access the object, one ought to write this and not:
[myFraction someMethod];
In addition, if I have
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *str = @"Programming can be a headache";
NSLog (@"%@\n", str);
Why is str being treated as an object above? Or is str an object and in which case, why would I want to make it point to an instance of NSString class? Surely, I ought to be able to just assign an object to str?
Thank you again in advance.
Happy New Year.