Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4627502
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:34:36+00:00 2026-05-22T03:34:36+00:00

I am trying to pass a pointer by reference to an object from class

  • 0

I am trying to pass a pointer by reference to an object from class A to class B. In class B I want to assign this pointer to a ivar and read and write to it.

This is the code that gives me errors (does not matter what errors). This is my first try with pointers so please correct my understanding.

Class A

//This is the parameter I would like to pass as a pointer and be able to manipulate from class B
NSString *name = @"Cyprian";

-(void)passAParameter{

   ClassB *classB = [[ClassB alloc] initWithAPointer:&name];
   ...
}


Class B

// ClassB.h
@interface ClassB{

    NSString **nameFromClassA;
}
@property(nonatomic,assign)NSString **nameFromClassA;

-(id)initWithAPointer:(NSString **)name;
// ClassB.m


@implementation ClassB

@synthesize nameFromClassA;

-(id)initWithAPointer:(NSString **)name{
   *nameFromClassA = *name;
}

//Print the name
-(void)printName{
   NSLog(@"Name: %@", *nameFromClassA);
}

//Will this change the name in class A?
-(void)changeNameInClassA:(NSString* newName){
   *nameFromClassA = newName;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:34:37+00:00Added an answer on May 22, 2026 at 3:34 am

    Please, do not use double pointers here. You shouldn’t handle things like that.
    This is a simpler approach:

    In the ClassA instance:

    -(void)passAParameter{
       NSString *name = @"Cyprian";
       ClassB *classB = [[ClassB alloc] initWithAPointer:name];
       ...
    }
    

    While you define ClassB this way:
    ClassB.h:

    @interface ClassB{    
    
        NSString *nameFromClassA;
    }
    @property(nonatomic,retain) NSString *nameFromClassA; // Retaining it will give you less headaches
    
    -(id)initWithAPointer:(NSString *)name;
    @end
    

    ClassB.m:

    @implementation ClassB
    
    @synthesize nameFromClassA;
    
    // You should release all retained object when deallocating self
    - (void) dealloc {
        [nameFromClassA release];
        nameFromClassA = nil;
        [super dealloc];
    }
    
    -(id)initWithAPointer:(NSString *)name{
        if ((self = [super init])) {     // Always init the object from super!
            self.nameFromClassA = name;  // Retain the object calling self.
        }
        return self;
    }
    
    //Print the name
    -(void)printName{
       NSLog(@"Name: %@", nameFromClassA);
    }
    
    //Will this change the name in class A?
    -(void)changeNameInClassA:(NSString* newName){
        self.nameFromClassA = newName; // Retain it calling self.
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pass a complex object (that can be serialized, if that helps)
I am currently trying to call a method from a utility class that will
Okay, so I'm trying to pass a pointer to a argument like this. void
I am trying to pass a member function within a class to a function
I'm currently trying to pass a mono threaded program to multithread. This software do
I'm trying to pass one method to another in elisp, and then have that
I'm trying to pass a null value for the first parameter in the code
I'm trying to pass an instance of a class between two separate classes, like
I am trying to create a general method for disposing an object that implements
C++: Using and returning character arrays from functions, return type or reference? I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.