Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C Question (Define Vs Const)

Options
  • 24-09-2010 9:00pm
    #1
    Registered Users Posts: 8,004 ✭✭✭


    Hi Folks,

    Can anyone help me with this problem. I have a iPhone App that upon loading takes a few ints works out a few int values and later in the program tests these values.

    However, I want to make these values available throughout the view and .m file so that other methods can use them. The only way I know how to do this is with define. However, every time the view loads there is a chance the int values could change. It has to be dynamic.

    Is there any way to assign a value to a define? Or is there some other way to do this?

    Many thanks.


Comments

  • Registered Users Posts: 981 ✭✭✭fasty


    You can't assign a value to a define, a define is just a macro the compiler will use wherever you use it.

    I'm guessing you keep the defines in a header? If so, do this instead:
    extern int MY_INTEGER;
    

    and in the .m file
    int MY_INTEGER = 1; // or whatever number you want
    

    Then just include the header as normal.

    Just so you know, declaring a global variables like this is bad practice and you should rethink your code. It's sort of okay if they're const, since you're just making the equivilent of defines with proper compile time type checking. Furthermore, if you used the const keyword in the declaration, you wouldn't be able to change it at runtime.


Advertisement