Thread: Compare structure name to #defined value

  1. #1
    Registered User
    Join Date
    Feb 2010

    Unhappy Compare structure name to #defined value

    Hi. Ive just started on C, and so that I dont have to double my code in a small test programme I've made I decided to use
    #define curplay play1
    where play1 is the name of a structure variable.

    So the first time it goes through my programme it runs it as play1 whenever i type curplay, but before it restarts I want to change curplay to play2.

    I tried typing

    if (curplay=play1) curplay=play2;

    but I get this error for that line:
    "invalid operands to binary !="

    Is there anyway i can switch curplay over to play2 at the end of my code?

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    That is not an appropriate use of a macro.

    You could use a pointer instead, or turn play1 and play2 into the elements of an array and use an array index.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    Quote Originally Posted by ItsAaron View Post
    Is there anyway i can switch curplay over to play2 at the end of my code?
    Yeah, you have to use a global variable instead of a define. Of course, then you can't use it to substitute for a different variable name (the struct).

    The reason you can use a #define to do that is the same reason it CANNOT be changed. #defines are "preprocessor" events. Before the code is compiled, the preprocessor goes through it and literally replaces all instances of the define label with it's definition. So when the code is compiled, "curplay" has been replaced with curplay1. So your code would look like:
    if (play1==play1) play1=play2;
    Nb. you also used = instead of ==.

    That will work if play1 and play2 are pointers to structs, but it will not work if they are actually structs.

    Really, you should just be using a global pointer (and not a define) for this, as laserlight says.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2010
    tbh I've not been able to use a pointer at all yet. Looks like I'll have to learn how to use them properly. Thanks for the replies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 08-18-2009, 10:21 AM
  2. Reference to a Structure
    By boschow in forum C Programming
    Replies: 3
    Last Post: 03-28-2008, 02:37 PM
  3. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 02:39 PM
  4. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  5. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM

Tags for this Thread