I\'m trying to modify the contents of one pointer using another. I want string2 to point to what string1 points to, so that when I modify string2, it modifies string1. How c
You need a pointer to an instance of NSString
.
Example:
NSString *string1 = @"Hello!";
NSString **string2 = &string1; // get the address of string1
*string2 = @"changed"; // set what string2 points to be 'changed'
NSLog(@"string1:%@, string2:%@", string1, *string2); // should print "string1: changed, string2: changed"
As an alternative to Richard J. Ross III's answer, since they're already pointers — buying you a level of indirection — just make sure that you change what's at the address they're pointing to while leaving it at that address. So, e.g.
NSMutableString *string1 = [[NSMutableString alloc] initWithString:@"hello"];
NSMutableString *string2 = string1;
[string2 setString:@"changed"];
NSLog(@"string1: %@, string2: %@"); // "string1: changed, string2: changed"
It's not really a workable general solution because it goes too much against the flow of normal Cocoa practices, but understanding what's going on with pointers and how they communicate objects is worthwhile.
When you make a second assignment to your string2
variable you are not modifying the contents of what the variable currently refers to, in this case the same NSString
as variable string1
refers to, but rather your are modifying to what string2
refers.
A picture (code) is worth a thousand words:
Every object and variable has an internal name, usually called its address. Addresses are usually written as hexadecimal numbers, e.g. 0xdeadbeef
, the value of the number is not important to you - it is just a label.
When you write:
NSString *string1 = @"Hello!";
the right-hand side asks Objective-C to create an NSString
with the value @"Hello"
, the result of this is an address, say 0xfeeddeaf
. The lhs-side asks for a variable capable of holding an NSString *
in it - a reference to an NSString
; to call this variable string1
; and to store the result of the rhs, 0xfeeddeaf
, into it.
When you now write:
NSString *string2 = string1;
this results in a variable string2
also containing 0xfeeddeaf
. There is no linkage between the contents of string1
and string2
, assignment just copies a value (which is an address in the case).
Finally when you write:
string2 = @"changed";
this creates an NSString
with the value of @"changed"
with an address, say, of 0xbeedcafe
and stores that in string2
. So now string1
contains 0xfeeddeaf
which is the address of @Hello
, and string2
contains 0xbeedcafe
which is the address of @"changed"
.
I'm guessing when you write that you're "trying to modify the contents of one pointer using another" what you want is for string1
and string2
to refer to the same object, whose value you can modify. You can't modify the value of an NSString
, they are non-modifiable (immutable) once created. Instead you have to use an NSMutableString
and you need to use a method to change the value of an NSMutableString
object - as the above shows if you use assignment you change which object is referred to, not the contents of the object.
Assuming you are using ARC[*] then changing your code to:
NSMutableString *string1 = [NSMutableString stringWithString:@"Hello"];
NSMutableString *string2 = string1;
[string2 setString:@"changed"];
will accomplish what I think you intend. The first two lines execute similarly to above and you end up with string1
and string2
containing the same address. The third line changes what is at the address they both refer to - you have the linkage you intended.
[*] if you are using GC or retain
/release
change the rhs of the first line to [[NSMutableString alloc] initWithString:"@Hello"]