Simple copying pointers with Objective C

后端 未结 3 758
忘掉有多难
忘掉有多难 2020-12-07 06:11

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

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 06:41

    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" 
    

提交回复
热议问题