Is there a way to initialize two different types of variables in a
for loop? For example:
for (int i = 0, n = strlen(oldStr), char a, b; i < n; i++)
So the variables
n are ints while
b are chars.
CS50 Stack Exchange is a question and answer site for students of Harvard University's CS50. It only takes a minute to sign up.Sign up to join this community
Short answer is no. There's no way to do it the way you want because declaring variables of two different types requires a semicolon between the two declarations and the for loop can't have more than three sections.
A quick fix would be declaring them before the for loop as follows
char a, b; for (int i = 0, n = strlen(oldStr), a = 'a', b = 'b'; i < n; i++) // some code
However, I don't recommend that because compacting things that way doesn't really make your code look good specially that these variables are of different types.
So I'd go with something like
char a = 'a', b = 'b'; for (int i = 0, n = strlen(oldStr); i < n; i++) // some code