Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `&` (ampersand) put in front of some method parameters?

I'ver wondered, why is it that in front of an NSError, such as below, do we put: &error and not error?

E.g.

NSArray *result = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 

Hope you can explain, also is this always the way or only in certain situations this is needed? Thanks.

like image 926
Josh Kahane Avatar asked Apr 07 '12 23:04

Josh Kahane


People also ask

Why is the sky blue short answer?

The Short Answer: Sunlight reaches Earth's atmosphere and is scattered in all directions by all the gases and particles in the air. Blue light is scattered more than the other colors because it travels as shorter, smaller waves. This is why we see a blue sky most of the time.

How is the sky blue?

Violet and blue light have the shortest wavelengths and red light has the longest. Therefore, blue light is scattered more than red light and the sky appears blue during the day. When the Sun is low in the sky during sunrise and sunset, the light has to travel further through the Earth's atmosphere.

Why is the sky blue and white?

Closer to the horizon, the sky fades to a lighter blue or white. The sunlight reaching us from the horizon has passed through even more air than the sunlight reaching us from overhead. The molecules of gas have rescattered the blue light in so many directions so many times that less blue light reaches us.

What chemical makes the sky blue?

Atmoshpere – Blue Sky When light enters the atmosphere it hits these small molecules such as oxygen (21%) and nitrogen (78%) (gases). Gases then tend to absorb not all of the light but only part of it. By “part of the light” I mean that they absorb only one out of 7 colors. It so happens that this color is blue.


2 Answers

You need to take the address of error because the function needs to modify it. error is passed by pointer, so you need the "take address" operator & for it.

C and Objective-C pass parameters by value. If you pass error without an ampersand and the method that you call modifies it, your function that made the call would not see any changes, because the method would operate on its local copy of NSError*.

You know that you need an ampersand in front of the corresponding parameter if you look at the signature of the method and see ** there:

- (NSArray *)executeFetchRequest:(NSFetchRequest *)request error:(NSError **)error                                             //   ^ one                    ^^ two 
like image 193
Sergey Kalinichenko Avatar answered Oct 05 '22 14:10

Sergey Kalinichenko


The error parameter's type is (NSError **), that is a pointer to a pointer to an NSError. The error variable that you use as the argument is probably declared as NSError *, so in order to get the types to match properly, you have to use the address of operator to get a pointer to a pointer (&error). The reason the method needs a pointer to a pointer in the first place is so that it can modify the value of error and have that new value be available to you, the caller of the method.

like image 44
UIAdam Avatar answered Oct 05 '22 14:10

UIAdam