for loop inquiry

This is a discussion on for loop inquiry within the C Programming forums, part of the General Programming Boards category; I'll have to sleep on that one....

  1. #31
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    I'll have to sleep on that one.

  2. #32
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    What's happening with your original code is that you are using the variable i to iterate through your string, so i goes 0,1,2,3,4,5,6,7,8,9,10,11,12,... until it hits the 0 byte of the null terminated string at s[9]. The problem is, however, that you were also using i to store the temporary value when you checked the modulus: i = s[i] - '0';, so you are clobbering i with this.

  3. #33
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Finally, I have reached enlightenment.

    Thanks a million.

  4. #34
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    This is just strange for me, cause just yesterday I had to zero out all my array elements in my loop to make my program work, now it appears in this program, the array is automatically zeroed out.
    Sent from my iPadŽ

  5. #35
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by SlyMaelstrom
    This is just strange for me, cause just yesterday I had to zero out all my array elements in my loop to make my program work, now it appears in this program, the array is automatically zeroed out.
    No, UB is UB. Unitialized data is just that. Sometimes it may be zero, sometimes it may not.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #36
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    As I said above, when you declare an automatic variable, uninitialised, the value could be anything, all zeroes, the fibonacci sequence, etc.

    In reality, when you first start a program on some systems, the stack starts off with zeroes, but once you call a few functions, the values are left over from previous calls, because the stack is never "re-initialised", so in a simple example with a single first function call, you might be "lucky" and get all zeroes, but then when you put another function call before it, the garbage from that call is left on the stack ready for the next function.

    Never, EVER rely on undefined behaviour. In fact, don't even bother to see what actually happens, because you're gaining useless and dangerous knowledge. It doesn't cost much to initialise:
    Code:
    char s[256] = { 0 };
    will initialise the whole array to zeroes.

  7. #37
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Ok, that works for me.
    Sent from my iPadŽ

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GNU strip inquiry
    By Chris87 in forum Tech Board
    Replies: 2
    Last Post: 07-05-2009, 08:04 PM
  2. Bitflags inquiry
    By Chris87 in forum C Programming
    Replies: 4
    Last Post: 01-03-2009, 05:22 PM
  3. Teaching myself C, quick inquiry
    By Iconate in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 02:19 PM
  4. Loops: The 'any' inquiry
    By chubbs1900 in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2007, 09:35 AM
  5. inquiry from a hungry mac os x user
    By terabyter in forum C Programming
    Replies: 3
    Last Post: 06-23-2006, 09:04 AM

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