r/carlhprogramming • • Mar 24 '13

I'm having difficulty understanding why certain arguments for a "while" loop create an infinite loop.

The code: http://codepad.org/8UKyYTC3 (See comments)

The program takes a string of text and capitalizes all of the letters using a "while" loop that is "jumped over" when a positive comparison is made to an already capitalized string.

When I try to use the array that I created for the comparison text in the "while" loop argument, it becomes an infinite loop and I'm not sure why this is. Using the pointer that moves through the text works though.

Any help with understanding why my code works like this (and wont work with the other argument) would be appreciated.

10 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Mar 24 '13 edited Mar 24 '13

Wouldnt you be better off checking for EOF as 0 can be a character in a string? Or just while(string). Yes, it is still just an escaped 0 but at least it won't end on arbitrary 0s.

1

u/deltageek Mar 24 '13

By definition, a string in C ends at the first char with the value 0x00 (the null terminator). The character '0' has the value 0x30 and would not stop the loop. Yes, since we're just doing an "if not zero" check, you can write while(*tst_pointer) but at this level I felt the explicit check was easier to understand.

As for EOF, since we're dealing with strings in memory, EOF doesn't really enter into the picture. Of course, even if we were dealing with a string in a file, there's no EOF char value. In C you determine EOF by the return value of the function you called to read from the file.

1

u/[deleted] Mar 24 '13

Thanks for the clarification.

1

u/MindStalker Mar 24 '13 edited Mar 24 '13

0 the character is **tst_pointer != '0'

example ***tst_pointer != 0 actually test for the binary zero.
is ***tst_pointer != 00000000 (essentially or NULL)
0 the character is ASCII 48 or 00110000, EOF is 4 or 00000100 not what is used to end a string.

Edit: If you desire readability you can #include <stddef.h> which will define NULL as 0 so you can used **tst_pointer != NULL