I'm new at this - trying desperately to pick up the basics for the company I work for while we locate bona fied developers, so please bear with me:
I'm learning rudimentary Obj-C and am stuck. Here is the section from "Become an Xcoder" by By Bert Altenberg, Alex Clarke
and Philippe Mougin.
In order to practice and thoroughly understand, I'm trying to use the same button in my GUI that I'm currently using to set the integer value in a text field to a given number. Instead, I want that button to continually add 1 (+1) to that integer, as long as the user chooses to press it. Sort of like, the most basic calculation known to man. Yet, I still can't grasp this
I'm thinking if I can slowly write my own calculator, I can get a better grasp on all this to go off and start writing my own basic apps.
Any help on getting me to understand this increment function?
I'm learning rudimentary Obj-C and am stuck. Here is the section from "Become an Xcoder" by By Bert Altenberg, Alex Clarke
and Philippe Mougin.
Pointers are useful because sometimes you don't want to refer to the value of a variable, but to the address of that vari-
able. For instance, you may want to program a function that adds 1 to a variable. Well, can't you just do it like this?
Actually, no. If you call this function from a program, you won't get the results you were expecting:Code://[4] void increment(int x) { x = x + 1; }
This code would display 6 on your screen. Why? Didn't you increase myValue by calling the increment function? No,Code://[5] int myValue = 6; increment(myValue); NSLog(@"%d:\n", myValue);
you actually didn't. You see, the function in [4] just took the value of myValue (i.e. the number 6), increased it by one,
and... basically, threw it away. Functions only work with the values you pass to them, not the variables that carry these
values. Even if you modify the x (as you can see it in [4]), you're only modifying the value that the function received.
Any such modification will be lost when the function returns. Besides, that x isn't necessarily even a variable: if you call
increment(5);, what would you expect to increment?
If you want to write a version of the increment function that actually works, i.e. accepts a variable as its argument and
permanently increases the value of that variable, you need to pass it the address of a variable. That way, you can modify
what is stored in this variable, not just use its current value. Thus, you use a pointer as argument:
You can then call it like this:Code://[6] void increment(int *y) { *y = *y + 1; }
Code:[7] int myValue = 6; increment(&myValue); // passing the address // now myValue is equal to 7
In order to practice and thoroughly understand, I'm trying to use the same button in my GUI that I'm currently using to set the integer value in a text field to a given number. Instead, I want that button to continually add 1 (+1) to that integer, as long as the user chooses to press it. Sort of like, the most basic calculation known to man. Yet, I still can't grasp this
I'm thinking if I can slowly write my own calculator, I can get a better grasp on all this to go off and start writing my own basic apps.
Any help on getting me to understand this increment function?