Reputation: 3293
I'm reading a book to learn Objective-C and this program is suppose to show key concepts in dealing with pointers, and I'm really lost.
Is there some kind of conversion happening in the function's arguments that turn p1
, p2
, &il
, and &i2
to the value (*
) of a pointer? Like p1
turns into *p1
?
I thought a copy of the variable was passed into the function instead of the actual variable, so why was the value of the passed in variable changed after the function?
Also why am I getting a warning on the 3rd line that says: No previous prototype for function 'exchangeValues'
?
Thank you!!
#import <Foundation/Foundation.h>
void exchangeValues (int *pint1, int *pint2) {
int temp;
temp = *pint1;
*pint1 = *pint2;
*pint2 = temp;
}
int main (int argc, char *argv[]) {
@autoreleasepool {
void exchangeValues (int *pint1, int *pint2);
int il = -5, i2 = 66, *p1 = &il, *p2 = &i2;
NSLog(@"il = %i, i2 = %i", il, i2);
exchangeValues(p1, p2);
NSLog(@"il = %i, i2 = %i", il, i2);
exchangeValues(&il, &i2);
NSLog(@"il = %i, i2 = %i", il, i2);
}
return 0;
}
Output:
2012-08-02 11:13:38.569 Test[381:707] il = -5, i2 = 66
2012-08-02 11:13:38.571 Test[381:707] il = 66, i2 = -5
2012-08-02 11:13:38.572 Test[381:707] il = -5, i2 = 66
Upvotes: 0
Views: 123
Reputation: 11268
its a little confusing how the variables have been declared and initialised all in a row like that but basically you have:
i1
is an int
set to -5
p1
is a pointer to an int
set to the address of i1
same goes for i2
and p2
No conversion is taking place. You're effectively 'swapping' the values that those pointers point to in the function.
Pointers are confusing things but stick with it and it will become clear with enough parctice and example code like this...
Upvotes: 0
Reputation: 122391
I would say that's a complex example if you are being taught about pointers!
Is there some kind of conversion happening in the function's arguments that turn p1, p2, &il, and &i2 to the value (*) of a pointer? Like p1 turns into *p1?
p1
and p2
are declared as int *
(pointer to int
) and are initialised with the address of i1
and i2
(using the &
operator).
I thought a copy of the variable was passed into the function instead of the actual variable, so why was the value of the passed in variable changed after the function?
A copy of the variable is passed to the function, however in this case the variable of type int *
(pointer to int
). The reason the value is changing is because the exchangeValues()
function is dereferencing those pointers and swapping the values. This is the only way (in C/Objective-C) a function can modify a variable outside of its own scope, other than the variable being assigned as the return value from a function.
Also why am I getting a warning on the 3rd line that says: No previous prototype for function 'exchangeValues'?
You seem to have typed it in wrong; remove the line below @autoreleasepool
:
@autoreleasepool {
void exchangeValues (int *pint1, int *pint2); <-- delete this line
Upvotes: 2
Reputation: 183908
I thought a copy of the variable was passed into the function instead of the actual variable, so why was the value of the passed in variable changed after the function?
A copy of the pointer is passed to the function here. So what the function has points to the memory locations the variables l1
and l2
are stored at. So
void exchangeValues (int *pint1, int *pint2) {
int temp;
temp = *pint1; // store the value that pint1 points to in temp
*pint1 = *pint2; // store the value pint2 points to where pint1 points to
*pint2 = temp; // store the value saved in temp where pint2 points to
}
Upvotes: 1
Reputation: 2778
If you pass a pointer into the function, it indeed passes a copy of that pointer- but it still refers to the same address in memory. So de-referencing that pointer will still point to a variable that's outside of the function scope.
Upvotes: 1