Compare structure name to #defined value

This is a discussion on Compare structure name to #defined value within the C Programming forums, part of the General Programming Boards category; Hi. Ive just started on C, and so that I dont have to double my code in a small test ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    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
    Location
    Singapore
    Posts
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Location
    segmentation fault
    Posts
    8,300
    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:
    Code:
    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
    Posts
    2
    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, 01:39 PM
  4. Replies: 13
    Last Post: 12-14-2007, 02:34 PM
  5. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 05:17 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21