Question on pointers

Discussion in 'Mac Programming' started by cyatic, Jan 31, 2010.

  1. cyatic macrumors newbie

    Joined:
    Jan 31, 2010
    #1
    hello everyone,
    I'm learning objective-c at the moment and am having a little bit of trouble. I think I'm grasping it a little but am confused because of two examples. The first has the pointer on the left side. The second example has the pointer on the right side. Does having * on either side make a difference, or does it mean somthing else when on each side respectively? Is it on the right side when used with types and left side when used on identifyers? By the way, the book I'm using is "Objective-c for Dummies." I wasn't sure if it was a typo, but after looking on the internet I've seen the * used on both sides. Thanks in advance.

    Example 1:
    int anInterger = 42;
    int *anIntergerPointer = &anInterger;




    Example 2:

    typedef struct {
    float exchangeRate;
    double budget;
    double exchangeTransaction;
    } budget;

    budget vacationBudgetEurope;
    budget vacationBudgetEngland;

    void spendDollars(budget* theBudget, double dollars);
    void chargeForeignCurrency(budget* theBudget, double foreignCurrency);

    int main (int argc, const char * argv[]) {

    vacationBudgetEurope.exchangeRate = 1.2500;
    vacationBudgetEurope.budget = 1000.00;
    double numberDollarsInEuroland = 100;
    double numberEuros = 100;

    vacationBudgetEngland.exchangeRate = 1.5000;
    vacationBudgetEngland.budget = 2000.00;
    double numberDollarsInPoundland = 100;
    double numberPounds = 100;

    spendDollars(&vacationBudgetEurope, numberDollarsInEuroland);
    chargeForeignCurrency(&vacationBudgetEurope, numberEuros);

    spendDollars(&vacationBudgetEngland, numberDollarsInPoundland);
    chargeForeignCurrency(&vacationBudgetEngland, numberPounds);

    return 0;


    void spendDollars(budget* theBudget, double dollars) {
    theBudget->budget -= dollars;
    }

    void chargeForeignCurrency(budget* theBudget, double foreignCurrency) {
    theBudget->exchangeTransaction = foreignCurrency*theBudget->exchangeRate;
    theBudget->budget -= theBudget->exchangeTransaction;
    }


    Thanks again,
    Cyatic
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    They mean the same thing. I would always try to stick to the * next to the variable, though.
    Code:
    int* x,y,z;
    Means that x is an int *, and y and z are ints. If you stick the * next to the variable it makes this clearer.

    -Lee
     
  3. cyatic thread starter macrumors newbie

    Joined:
    Jan 31, 2010
    #3
    Hi Lee,
    Thanks for the quick reply.

    Code:
    int *x,y,z; 
    So this is the same as what you wrote? Also a better way of doing it?

    -Cyatic
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Yes, that is the same. It is, IMO, a better way. Even better might be:
    Code:
    int *x,*w;
    int y,z;
    Grouping your *s together and your plain ints together.

    -Lee
     
  5. cyatic thread starter macrumors newbie

    Joined:
    Jan 31, 2010
    #5
    Awesome! Thank you very much for your help. I appreciate you taking the time to help me. I appreciate it.


    -Cyatic
     

Share This Page