Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:44:40+00:00 2026-06-09T17:44:40+00:00

Code declaring anonymous structs in a for loop worked fine in gcc with -std=c99/gnu99

  • 0

Code declaring anonymous structs in a for loop worked fine in gcc with -std=c99/gnu99

for (struct {int foo; int bar;} i = {0}; i.foo < 10; i.foo++);

However when I switch to clang instead I got the error:

error: declaration of non-local variable in 'for' loop

Why is this an error? Why would it allow some types (e.g. “int”) but not others (e.g. struct {int foo;}) ? This seems inconsistent. Does clang fail to implement c99 correctly or is that code invalid c99 and gcc just happens to support it?

Does anyone know of a way to declare more than one type of variable in a for loop that is supported by clang? (This is useful for macros.)

EDIT:

Since people asked why this is useful I will paste some example code:

#define TREE_EACH(head, node, field, iterator) for ( \
    /* initialize */ \
    struct { \
        node* cur; \
        node* stack[((head)->th_root == 0? 0: (head)->th_root->field.avl_height) + 1]; \
        uint32_t stack_size; \
    } iterator = {.cur = (head)->th_root, .stack_size = 0}; \
    /* while */ \
    iterator.cur != 0; \
    /* iterate */ \
    (iterator.stack_size += (iterator.cur->field.avl_right != 0) \
        ? (iterator.stack[iterator.stack_size] = avl_right, 1) \
        : 0), \
    (iterator.cur = (iterator.cur->field.avl_left == 0) \
        ? iterator.cur->field.avl_left \
        : (iterator.stack_size > 0? (iterator.stack_size--, iterator.stack[iterator.stack_size]): 0)) \
)

This is a really convenient macro that I wrote which iterates over an AVL tree in depth-first order on the stack. Since declaring anonymous structs in the for loop is not allowed though I have to make the macro less intuitive to use. I could not possible out-source the declaration to the rest of the tree since it uses a variable length array.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:44:42+00:00Added an answer on June 9, 2026 at 5:44 pm

    I’m respectfully unconvinced by the previous answers. It builds successfully with gcc (with -Wall -pedantic), only not with clang nor Visual Studio.

    Microsoft have acknowledged as a bug an extremely similar issue with Visual Studio at this Microsoft Connect bug item.

    6.8.5 is saying that declarations of identifiers inside the for-init-expression cannot be typedef, extern or static (the only storage class specifiers other than auto and register).

    The storage class specifier auto in C99 is default and is implicit. The struct type and identifier i are then auto (have scope within that code block). Surely the code is then valid? I don’t see how 6.8.5 is forbidding the declaration of a type.

    I suggest that gcc is correct, and it’s a bug with the implementation by clang and Visual Studio.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

or, Declaring multiple variables in a for loop ist verboten ?! My original code
have the following javascript code // note: declaring i in this loop for( var
Is declaring an header file essential? This code: main() { int i=100; printf(%dn,i); }
I am having problem in declaring StringStream in QT. Here is my code: std::stringstream
I'm declaring a struct inside my code and then trying to insert it into
I am successfully declaring a data template in a code behind as follows: private
This is the code that I have to dynamically declaring an array inside a
In my application i'm declaring a string variable near the top of my code
I have seen that declaring a bidimensional array as a jagged array is fine
Code: DirectoryInfo[] d2 = new DirectoryInfo[400]; d2 = moreDirect.GetDirectories(); //Declaring FileInfo array FileInfo[] f

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.