Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing LCV in a For loop

Status
Not open for further replies.

Strogian

Technical User
Nov 11, 2000
36
US
I am learning C now (using a book), after learning Pascal and Visual BASIC. (by taking classes on them) I'm wondering, is it normal to change the loop control variable of a for loop (in C) inside the loop? Is it generally considered bad style? Or, are there just certain situations where it is accepted? I can't seem to find this in any "style guides" around the Internet. Thanks in advance.
 
Here's an example of something that I wrote, to convert escape sequences in a string into the characters they represent. In the for loop, I stop whenever t[ti] != '\0'. Usually it will just increment ti by one at the end of an iteration, but if there is an escape sequence ("\t" or "\n") in the string, I would want to convert those two characters into the single character that they represent, so I increment ti by one more.


/* unescape: copy string t to s, changing newline and tab escape sequences into
newlines and tabs, returns index of string-terminating '\0' in s[] */
int unescape(char s[], char t[])
{
int si = 0, ti; /* indices for s and t, respectively */

for (ti = 0; t[ti] != '\0'; ++ti)
if (t[ti] == '\\')
switch (t[ti + 1]) {
case 't':
++ti;
s[si++] = '\t';
break;
case 'n':
++ti;
s[si++] = '\n';
break;
default:
s[si++] = '\\';
break;
}
else
s[si++] = t[ti];

s[si] = '\0';
return si;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top