Thread: clear int m?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    27

    clear int m?

    my program is looping because
    int m = (any integer)
    stops it from pausing because a condition is being satisfied.

    How do I clear m so that
    m = (nothing)

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by mikeyp View Post
    How do I clear m so that
    m = (nothing)
    ????

    A variable always contains something!!! It's not a pointer!
    But you could set it to 0 in order to be casted to false when checked:

    Code:
    if (!m) {/*...*/} // true when m = 0, false otherwise
    Devoted my life to programming...

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    So you have a value that consists of 32 bits (I'm going to assume 32 bits anyway) which are either one or zero and you want it to equal "nothing"?
    Well you'll just have to invent some combination of 32 ones and zeros that you'd like to mean "nothing" and assign that. Well you could set all 32 bits to zero I guess, in other words assign the value zero to m.

    Perhaps you can just assign a value that stops the condition from being satisfied.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Actually, zero is (nothing), so it's safe and meaningful to set m to zero and do what i told you ( some times i guess )
    Devoted my life to programming...

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You could also use an int pointer, and than can be set to NULL, which is not the same as having the pointer contain 0.
    Code:
    int n = 0, *x = &n;
    x = NULL;
    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

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Sipher View Post
    Actually, zero is (nothing), so it's safe and meaningful to set m to zero and do what i told you ( some times i guess )
    0 is not nothing, it's an integer value just like 1 and 12345 are. There is no possible way to have an int containing nothing.
    It is often used to represent nothing, but that's not the same thing as being nothing.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    So does that mean that when I create

    int m;

    its value is 0 anyway?

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mikeyp View Post
    So does that mean that when I create

    int m;

    its value is 0 anyway?
    No. Its value is undefined. That doesn't mean it has no value, but it means the value is unpredictable.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could also create a class that represents an int, but also lets you set the state of the class to 'nothing'. Personally, I think that's overkill. Just use 0, or a NULL pointer.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    yep, brewbuck is right, int m; gives an unpredictable number. E.G it may contain
    10 by default and 49 on another computer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM