Newbie Question about Increment Function

Discussion in 'Mac Programming' started by kkachurak, Jul 27, 2008.

  1. kkachurak macrumors regular

    Joined:
    Jun 26, 2007
    Location:
    Orlando, FL
    #1
    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 :confused::rolleyes:

    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?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    This is more of a C question than Objective-C, but it is certainly applicable to Objective-C as well.

    Variables in C hold some value, and in a normal case when you use the variable name, you just get the value that variable has been set to. But that variable is being held somewhere. Every variable has a memory location. So if you say:
    Code:
    int x = 5;
    x = x + 1;
    printf("X is: %d\n",x);
    
    You will get a line of output that reads:
    X is: 6

    This is fine while you're in the same function, but under normal circumstances if you are passing by value, the function cannot modify the variable in the calling function. So:

    Code:
    void testFunction(int);
    
    int main(int argc, char *argv[]) {
      int x = 7;
      testFunction(x);
      printf("Back in main, x is: %d\n",x);
    }
    
    void testFunction(int input) {
      input = input + 1;
      printf("After incrementing, input is: %d\n",input);
    }
    
    The output would be:
    After incrementing, input is: 8
    Back in main, x is: 7

    The value 7 was passed to testFunction. testFunction had its own variable that stored this value, but it is completely separate from x.

    So the next step is to be able to pass around the memory address that holds the value for a particular variable. There are special operators to deal with referencing and dereferencing memory addresses (called pointers). There is the address-of/reference operator & and the dereference operator *. These operators are used in front of the variable that they are operating on. So:

    Code:
    #include <stdlib.h>
    
    void testFunction(int *);
    int main(int argc, char *argv[]) {
      int *intPtr = NULL;
      int x = 6;
      intPtr = &x;
      printf("Before calls, x is: %d, intPtr is: %p\n",x,intPtr);
      testFunction(intPtr);
      printf("After first call, x is: %d\n",x);
      testFunction(&x);
      printf("After second call, x is: %d\n, &x is: %p\n",x,&x);  
    }
    
    void testFunction(int *input){
      printf("The address stored by input is: %p\n",input);
      *input = *input + 1;
      printf("The value stored at %p is now: %d\n",input,*input);
    }
    
    The output will be:
    Before calls, x is: 6, intPtr is: 0x01234567
    The address stored by input is: 0x01234567
    The value stored at 0x01234567 is now: 7
    After first call, x is: 7
    The address stored by input is: 0x01234567
    The value stored at 0x01234567 is now: 8
    After second call, x is: 8

    The value 0x01234567 will be the hexadecimal representation of the memory address stored by the pointer. It will be 16 hexits on a 64-bit system, 8 on a 32-bit system.

    In this example, instead of passing the value of x, we pass the address where x is stored to our function. Inside our function we use the dereference operator to manipulate the int value at the address we pass in.

    This is the foundation for a great deal of C and Objective-C. Every Objective-C object is accessed via pointers. Messages are passed using the pointer to the Object. If you need to, spend extra time working with this until it makes sense, because this is a crucial piece of C syntax.

    What you are trying to do might be more complicated, as you may need to pass an NSString or NSNumber to be displayed in your control. This is not too bad though, it's easy to get an NSNumber with an int or generate an NSString that stores a representation of an int using a format string.

    Good luck!

    -Lee
     
  3. JVene macrumors newbie

    Joined:
    Jul 22, 2008
    #3
    I'm going to focus on the premise behind your question, that of learning to develop software for a company while they search for a developer. I'm assuming you plan on creating simple applications for internal use, perhaps to join in with the development team formed later on.

    I don't want to discourage you with a harsh dose of reality, but I'm afraid this is going to turn out to be just that. The learning curve for C development is substantial, and a pre-requisite to development in objective-C (or C++ for that matter). It can take a person a couple of years to become proficient enough to develop applications that are stable enough for a company to actually use.

    XCode does provide a considerable help in this regard, but I have to provide these warnings in order to get you to consider the implications of your plan.

    In the 'old days' of C development, mistakes with pointers would often cause program crashes of the kind that looses work performed by the user, and a company can't tolerate that in daily business. In DOS (I know, hang with me), errors with pointers could be more catastrophic - the infamous example being that a simple mistake could reformat the hard disk. This was actually true. In a few cases, such errors could even destroy a CRT monitor!

    Thankfully that was never true of Unix, and not true of OSX (or older Mac OS to my knowledge). Still, you can crash the application, and even hang up the operating system (I know, I've done it myself).

    You may do better, and be more productive, to consider another language. If you were working in Windows I'd recommend C#. It's quite popular for business 'front end' programming under Windows. There is a C# compiler and runtime available for Unix/Linux, and I think Apple, but it isn't the first, most common or supported choice for Mac. Java would probably do better.

    While you're not going to make genuine Mac applications in Java, you'll be in safer territory where errors with pointers aren't a problem; the language doesn't use them. The learning curve is much less, and you'll be able to create usable applications much more quickly in Java than in objective-C.

    They'll be slower, and they won't be native Mac applications, but they'll work and perform simple objectives well enough to make you productive.
     
  4. kkachurak thread starter macrumors regular

    Joined:
    Jun 26, 2007
    Location:
    Orlando, FL
    #4
    Thanks, lee1210, for taking the time to explain this to me. I'm struggling no doubt about it, and even as I work this around for the third time and try to fit it into my practice GUI, the debugger is still throwing me errors.

    But I appreciate you not trying to dissuade me. I'll give this thread a shout if I have any additional questions related to my increment function.
     

Share This Page