Two Variables in one for loop

http://www.cplusplus.com/forum/beginner/59662/

I tried
 
for (char **list = (char**)pa, int i = 0 ; *list && i<5; ++list, i++)

but get error
error: expected unqualified-id before ‘int’
I'm pretty sure that the problem is the declaration of a char** and an int without splitting them with a coma. In your exemple both variables are int.
so we can declare only variables with the same type as first one?

in some example show this method
int i;
double f;
for ( i=0, f=0.0;

in the link from first post
is shown that we can use

for (int i=0, y=0;

so is it possible to declare two different vars type inside for or not
if yes , why I get error?
Last edited on
C++17:
1
2
3
4
5
6
7
8
#include <iostream>
#include <tuple>

int main()
{
    for( auto [i,j,d] = std::tuple{ 0, 1, 2.3 } /* int, int, double */ ; i < 5 && j < 20 && d < 100 ; ++i, ++++j, d += 1.5 )
        std::cout << i << ' ' << j << ' ' << d << '\n' ;
}

http://coliru.stacked-crooked.com/a/7b0ae136ed78b934
you should be able to do it with pointers (void *s to existing items, cast back to true type inside loop) or unions/variants as well. None of which is terribly clean. Or you can just declare the variables outside the loop, and use them inside (cleaner). Above is cleanest, if your compiler supports and you are allowed to use (school stuff). These alternatives are more to open your eyes than to suggest an approach.

you are allowed to {} scope without constraint if you want them to die after the loop (not useful normally, but if the type were a large container of much memory that you want to drop asap).

{ //scope i and pa
int i = 0;
thing ** pa = list;
for(...) {;}
} //end of scope

Last edited on
There are a number of coding standards which stipulate against mixing tests and variable declarations or increments in loops (both while and for). Most have good reasoning, with the main point being that for clarity the variables used inside the "for" loop should be limited to those which control the loop.

@jonnin's suggestion works along these lines by moving the declarations outside the loop.

Clearly, by the OP's test clause, either a limit of 5 or a nullptr in the list terminates the loop. Compliant with the coding standards I mention, one or the other should suffice, where alternative tests are often placed inside the code block the loop controls (using break, usually).

That said, there's an "out" here.

If one fashions an end pointer related to "pa", the use of "i" is obviated, like this:

1
2
3
4
char **p_end = (char **)pa; // not sure why this must be cast, but I echo that blindly

for( char **list = (char **) pa; *list && list < p_end; ++list ).....


If one considers "list" to be an iterator, this is in line with iterator usage elsewhere.



truly my interest only in initialization two type in first block of the for loop

1
2
for (int i=0, const char **pp=pa; ;)
{if (i++> 5) break; }

but I get error

I understand that I can outside of for initialize it but how/why its work in the link in the 1st post
Last edited on
how/why its work in the link in the 1st post

This has already been explained: because, in that link, both variables are the same type.
The syntax is described in https://en.cppreference.com/w/cpp/language/for

Comma operator: https://en.cppreference.com/w/cpp/language/operator_other
In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded (although if it has class type, it won't be destroyed until the end of the containing full expression), and its side effects are completed before evaluation of the expression E2 begins.
1
2
3
int i;
double f;
i=0, f=0.0 ; // E1 is i=0, E2 is f=0.0 

The E1 and E2 must be expressions. A declaration is not an expression.

A declaration https://en.cppreference.com/w/cpp/language/declarations
can contain a "comma-separated list of declarators with optional initializers".
In int x=7, y=3; the x and and y are declarators within one declaration.
The int is the decl-specifier-seq of the declaration.
The comma in the declaration is not the comma operator.
Last edited on
Topic archived. No new replies allowed.