Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C Question (Define Vs Const)

  • 24-09-2010 09:00PM
    #1
    Registered Users, Registered Users 2 Posts: 7,994 ✭✭✭


    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, Registered Users 2 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