I'm actually trying to do this in objective c, but I belive the theory probarbly are the same for C++.
Heres an example of what I want to do with a simple, working, example using an integer:

void func2(int *i) {
    *i = 2;
}

void func1() {
    int i = 1;
    func2(&i)
    // i now equals 2
}

I want to be able to do this with an objec aswell, but when I try with the example under I get the following warning: "Passing argument from incompatible pointer type".

void func2((NSScanner *)aScanner) {
    //aScanner is used and updated here.
}

void func1 {
    NSScanner *aScanner = initiate new Scanner-object.
    
    [func2 &aScanner];

    // Changes done in func2 to the scanner object also applies here
}

I know I would achieve this with a global variable, but from what I understand it's preferred to avoid them when possible.
So anyone who can help me understand why this doesn't work and how to do it properly?

Recommended Answers

All 2 Replies

I'm not familiar with the objective-c syntax, but in C++ it would be something like

void func2(NSScanner * myNSScanner)
{
   //yada
}

void func1()
{
   NSScanner * aScanner = new NSScanner(); //or your parameters 
   func2(aScanner);  

}

It's different from the integer example because your aScanner is already a pointer

(You could also pass around a reference to your object (using the & operator instead of * in func2 -- just pass in a NSScanner object to the function directly you won't need the pointer))

Thanks, you helped me figure it out.
I had to use one of these two methods, both work:

-(void) test2:(NSScanner **)aScanner) {
    // Using the & operator instead of *
    [&aScanner setScanLocation:20];
}

-(void) test1 {
    NSScanner *aScanner = initiate new Scanner-object.
    
    [test2 &aScanner];

     // Changes done in func2 to the scanner object also applies here
}
-(void) test2:(NSScanner *)aScanner {
    // No need to use & or * here
    [aScanner setScanLocation:20];
}

-(void) test1 {
    NSScanner *aScanner = initierer ny scannerobjekt her.
    
    [test2 aScanner];

     // Changes done in func2 to the scanner object also applies here
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.