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 i and n are ints while a and b are chars.


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .