for loop, two different types

V

vippstar

Hello comp.lang.c

I had to write a for() loop with two temporary variables for it.
What I *usually* do is define the variables at the top so it works in
C89 as well.
But I realized that it just can't be done in the C99 'for' loop!

Assume I want two variables, int i and size_t size.

for(int i, size_t size; ...; ...)

is a syntax error

Is there a way to do this? I'm not talking about putting these two
types in a struct.

Assuming this can't be done, isn't the addition of defining variables
in the first part of the for loop in C99 useless, or at least
"incomplete"?
 
S

santosh

Hello comp.lang.c

I had to write a for() loop with two temporary variables for it.
What I *usually* do is define the variables at the top so it works in
C89 as well.
But I realized that it just can't be done in the C99 'for' loop!

Assume I want two variables, int i and size_t size.

for(int i, size_t size; ...; ...)

is a syntax error

Is there a way to do this? I'm not talking about putting these two
types in a struct.

Assuming this can't be done, isn't the addition of defining variables
in the first part of the for loop in C99 useless, or at least
"incomplete"?

Well you can't do:

int i, size_t size;

anywhere else either.
 
V

vippstar

<Shrug.> Define "complete," and then we can all have fun
mocking you for the things you overlooked. ("That silly vippstar!
His so-called `complete for' doesn't permit function definitions
inside the increment portion. What a shortsighted moron, tee-hee!")
I guess I'm just looking for things I found in other languages that in
C wouldn't make sense :)
(I'm talking about the common lisp 'LOOP' macro)

Regardless - this particular problem wasn't important to me, I was
just curious why such thing wasn't allowed and now I believe I
understand.
 
S

santosh

I guess I'm just looking for things I found in other languages that in
C wouldn't make sense :)
(I'm talking about the common lisp 'LOOP' macro)

Regardless - this particular problem wasn't important to me, I was
just curious why such thing wasn't allowed and now I believe I
understand.

I think C99 allows a single declaration in the initialisation part of
the for loop. What you wanted were multiple declarations, which would
require multiple statements, and that is a syntax error in a for loop.
It would have been nice if it were legal though - no need for ugly
blocks.
 
R

Ron Ford

Hello comp.lang.c

I had to write a for() loop with two temporary variables for it.
What I *usually* do is define the variables at the top so it works in
C89 as well.
But I realized that it just can't be done in the C99 'for' loop!

Assume I want two variables, int i and size_t size.

for(int i, size_t size; ...; ...)

What happens in the body of the loop with i and size such that your syntax
would be different than garden-variety nested loops?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top