My study guide says:
It is possible to declare and initialize the pointer variable for a string in one go:
No problem. I (sort of) understand that.
Then it says: "Once you have initialized the variable, i.e. favoriteComputer, you may give the variable another value, but you cannot change the string itself" and gives the following code:
This is sort of confusing for two reasons:
(1) Is the second line "favoriteComputer = @"MacBook Pro";" the "giving the variable another value" that the book mentions? I get the impression that when you give it another variable like that, it wipes away the first variable given, right?
(2) What does the book mean by "you cannot change the string itself"? Very confused on this one. favoriteComputer is a variable of type "pointer" that points to an address. iBook and MacBook Pro are values that it took (except that doesn't make sense, unless those two things are themselves variables defined somewhere else). What am I missing here?
Any explanation(s) greatly appreciated! Thanks..
It is possible to declare and initialize the pointer variable for a string in one go:
Code:
NSString *favoriteActress = @"Julia";
No problem. I (sort of) understand that.
Then it says: "Once you have initialized the variable, i.e. favoriteComputer, you may give the variable another value, but you cannot change the string itself" and gives the following code:
Code:
#import <foundation/foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *favoriteComputer;
favoriteComputer = @"iBook";
favoriteComputer = @"MacBook Pro";
NSLog(@"%@", favoriteComputer);
[pool release];
return 0;
}
This is sort of confusing for two reasons:
(1) Is the second line "favoriteComputer = @"MacBook Pro";" the "giving the variable another value" that the book mentions? I get the impression that when you give it another variable like that, it wipes away the first variable given, right?
(2) What does the book mean by "you cannot change the string itself"? Very confused on this one. favoriteComputer is a variable of type "pointer" that points to an address. iBook and MacBook Pro are values that it took (except that doesn't make sense, unless those two things are themselves variables defined somewhere else). What am I missing here?
Any explanation(s) greatly appreciated! Thanks..