 |
BOOK: Beginning iOS 4 Application Development
 | This is the forum to discuss the Wrox book Beginning iOS 4 Application Development by Wei-Meng Lee; ISBN: 978-0-470-91802-9 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning iOS 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 13th, 2011, 06:59 AM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
understanding pointers (ch.3 page control)
ok, in an effort not to further dilute the other threads, I am opening a new one looking for help in understanding pointers. I did read the relevant chapters in several books and seem to understand the underlying concept. however, it seems to me that this isn't consistently applied? to teach myself, I wrote this little program based on 'chapter 11:pointers' in 'becoming an xcoder':
Code:
void increment(int x) {x=x+1;}
void incrementPointerStyle(int *y) {*y=*y+1;}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
int myValue=10;
int myOtherValue=10;
int yetAnotherValue=10;
increment(myValue);
NSLog(@"my value incremented by function is:%d", myValue);
// this displays 'my value incremented by function is:10'
myOtherValue=myOtherValue+1;
NSLog(@"my other value incremented within routine is:%d", myOtherValue);
// this displays 'my other value incremented within routine is:11'
incrementPointerStyle(&yetAnotherValue);
NSLog(@"yet another value incremented by pointerstyle function is:%d", yetAnotherValue);
// this displays 'yet another value incremented by pointerstyle function is:11'
}
questions regarding this:
1.) why does incrementing the 'bad' way work inside the routine?
2.) why do I need an &-sign for the pointerstyle incrementation, I would have thought that way I increment the address, leading to unpredictable results?
|
|

June 13th, 2011, 10:03 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
|
|
It is helpful to clarify what the "English" translations of the symbols * and & are in case it is not clear
int *pointerToInt; - a pointer to an int - 4 bytes of memory at an address, the value is undetermined;
&pointerToInt - the address of pointerToInt
*pointerToInt - the value of pointerToInt
Try this and see if the following results make sense:
Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
int aValue = 10;
NSLog(@"aValue is %d\n aValue's address is %p\n aValue's value is %d\n\n\n",aValue, &aValue,*(&aValue));
int *pointerToInt;
int *pointerToInt2;
int *pointerToInt3;
NSLog(@"pointerToInt address is %p, size %lu, value %d",pointerToInt, sizeof(pointerToInt), *pointerToInt);
NSLog(@"pointertoInt2 is %p size %lu", pointerToInt2, sizeof(pointerToInt2));
NSLog(@"pointertoInt3 is %p size %lu\n\n\n", pointerToInt3, sizeof(pointerToInt3));
pointerToInt = malloc(sizeof(int)); //Cannot assign a value without allocating memory.
*pointerToInt = 7;
NSLog(@"After malloc and assign pointerToInt address is %p, size %lu, value %d\n\n",pointerToInt, sizeof(pointerToInt), *pointerToInt);
int x = 10;
int y = 20;
int z = 30;
pointerToInt = &x;
pointerToInt2 = &y;
pointerToInt3 = &z;
NSLog(@"x address is %p, size %lu, value %d",&x, sizeof(x), x);
NSLog(@"y address is %p, size %lu, value %d",&y, sizeof(y), y);
NSLog(@"z address is %p, size %lu, value %d\n\n\n",&z, sizeof(z), z);
NSLog(@"After assigning the address of x to pointerToInt address is %p, size %lu, value %d",pointerToInt, sizeof(pointerToInt), *pointerToInt);
NSLog(@"After assigning the address of y to pointertoInt2 address is %p size %lu, value %d", pointerToInt2, sizeof(pointerToInt2), *pointerToInt2);
NSLog(@"After assigning the address of z to pointertoInt3 address is %p size %lu value %d\n\n\n", pointerToInt3, sizeof(pointerToInt3), *pointerToInt3);
NSLog(@"pointerToInt address is %p, size %lu, value %d",pointerToInt, sizeof(pointerToInt), *pointerToInt);
NSLog(@"After adding 1 to pointerToInt address is %p (incremented by 4 bytes, the size of an int), size %lu, value %d\n\n",++pointerToInt, sizeof(pointerToInt), *pointerToInt);
NSLog(@"pointertoInt2 address is %p size %lu, value %d", pointerToInt2, sizeof(pointerToInt2), *pointerToInt2);
NSLog(@"After adding 1 to the value of pointertoInt2 address is %p size %lu, value %d\n\n", pointerToInt2, sizeof(pointerToInt2), ++*pointerToInt2);
NSLog(@"pointertoInt3 is %p size %lu, value %d", pointerToInt3, sizeof(pointerToInt3),*pointerToInt3);
NSLog(@"After adding 1 to the value of pointerToInt3 address is %p size %lu, value %d", pointerToInt3, sizeof(pointerToInt3), ++*pointerToInt3);
NSLog(@"Assigning the value of y to pointerToInt");
*pointerToInt = y;
NSLog(@"y address is %p, size %lu, value %d",&y, sizeof(y), y);
NSLog(@"After assigning the value of y pointerToInt address is %p, size %lu, value %d",pointerToInt, sizeof(pointerToInt), *pointerToInt);
[self.window makeKeyAndVisible];
return YES;
}
Looking at your example:
Code:
void increment(int x) {x=x+1;}
void incrementPointerStyle(int *y) {*y=*y+1;}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
int myValue=10;
int myOtherValue=10;
int yetAnotherValue=10;
increment(myValue);
NSLog(@"my value incremented by function is:%d", myValue);
// this displays 'my value incremented by function is:10'
myOtherValue=myOtherValue+1;
NSLog(@"my other value incremented within routine is:%d", myOtherValue);
// this displays 'my other value incremented within routine is:11'
incrementPointerStyle(&yetAnotherValue);
NSLog(@"yet another value incremented by pointerstyle function is:%d", yetAnotherValue);
/* this displays 'yet another value incremented by pointerstyle function is:11'
What your are doing here is passing the address. In the function:
*y(the value of y) = *y(the value of y) + 1; You are adding 1 to the value of y, not 1 to the pointer y.
*/
}
1. What do you mean by incrementing the bad way?
2. & passes the address, but in the function you dereferenced the address (asked it for its value) and acted on the value. Since in your function 'y' is a pointer to an int it is an address plus 4 bytes of memory. in the function y = y+1 would have incremented the pointer 'y' by 4 bytes. Since this is a local copy this would have no impact on the original value (yetAnotherValue).
Bob
|
|

June 13th, 2011, 12:14 PM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
|
1. What do you mean by incrementing the bad way?
|
By this I mean, that I
Code:
myOtherValue=myOtherValue+1
seems to work fine, I don't need to say
Code:
*myOtherValue=*myOtherValue+1
and I don't understand why in that case it's ok to talk straight to the variable 'myOtherValue' without using a pointer.
I need to get back to you about the pointerToInt routine you gave me....
|
|

June 13th, 2011, 01:24 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
|
|
An int is not a pointer. An int is a data type.
You defined
int myOtherValue =10; //not a pointer, just an integer
Math operations affect the value of the int, pointers are not used. The scope is local to the function where the variable is defined.
A pointer is basically a memory address. The amount of memory that is attached to it depends on the size of the data type, structure, object or whatever it points to. An int has a memory address, and a size of 4 bytes in Xcode. This could be assigned to a pointer.
Code:
int *pointerToInt; //pointerToInt is a pointer to an int
pointerToInt = malloc(sizeof(int));// Setting the size - if you don't, program will potentially crash. If you need more or less you must allocate the amount you need.
pointerToInt = &myOtherValue; // Assigning the address of myOtherValue to pointerToInt; It now knows the location in memory of myOtherValue and that, since it is an int, it is 4 bytes long
now
*pointerToInt is equal to 10 - Read as the value of pointerToInt is equal to 10.
It may be a little confusing since the '*' symbol is used to both define something as a pointer, and also to dereference the value of the pointer. Think of deference as "ask for the value at the pointer's address". The size (number of bytes) is determined by who asks. If the size is wrong, you will probably get gibberish. In an NSLog statement %d will read 4 bytes, %f will read 8 bytes, for example. Xcode will warn you if there are mismatches between tokens and arguments.
Changes to myOtherValue will be reflected in *pointerToInt (again the value of pointerToInt)
e.g.
myOtherValue = 200;
makes
*pointerToInt equal to 200;
Changes to *pointerToInt will be reflected in myOtherValue
e.g.
*pointerToInt = 300;
makes
myOtherValue equal to 300
Bob
Last edited by thepianoguy; June 13th, 2011 at 01:33 PM..
|
|

June 14th, 2011, 03:59 PM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Bob, thanks for your explanations and patience. the more I get into this, the more I get the feeling I walked into the wrong class here, I don't know any C or variant of C and I have only been grabbing the bits of objective-c necessary to borrow snippets of iOS code from various sources and pasting them together in the hope to come up with a running App. 
I have since found a book 'Objective-C for absolute beginners', which, for once, does not expect the reader to have "some grasp of C". I don't think that it makes sense for me to go through your 'pointerToInt'-program right now as I will have a question about absolutely every single term.
What would be nice would be a source of a list of those %p, %i bits are that I can put into strings to visualise values. this turned out impossible to google, as they ignore the % sign 
|
|

June 15th, 2011, 12:08 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
|
|
I think it is wise to have a grasp of C and Objective C before going to deep into iOS. Trying to come to grips with the frameworks, IDE, Object-oriented concepts et. al. without some background will be quite overwhelming. Stephen Kochan's Objective C 2.0 book is coming out in a 3rd edition shortly. The author very actively supports the book on his forum. The pace is gradual, and it promises to be very up to date. If you are looking for something comprehensive, I suggest you check it out.
%p in NSLog statements prints out the memory address of the argument.
%i in NSLog prints out a long integer
Google NSLog tokens for info.
Good luck.
Bob
|
|
 |
|