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

  • Home
  • SEARCH
  • 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 6747585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:27:42+00:00 2026-05-26T12:27:42+00:00

Given this simple C code: struct { struct a { int foo; }; struct

  • 0

Given this simple C code:

struct { 
    struct a {
        int foo;
    };
    struct b {
        char *bar;
    };
} s;

I am wondering whether there is a way to access a variable in one of the nested structures in a more compact way than s.a.foo = 5, for instance.

  • 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-05-26T12:27:43+00:00Added an answer on May 26, 2026 at 12:27 pm

    First, notice that your example is not standard C89 (but it is acceptable by some compilers when you ask for some language extensions. With GCC you’ll need to extend the accepted C dialect with the -fms-extensions flag to the compiler). You are using unnamed fields. A more standard way of coding would be:

    struct a {
       int foo;
    };
    struct b {
       char* bar;
    };
    struct {
        struct a aa;
        struct b bb;
    } s;
    

    Back to your question, no, there is no other way. However, you might use preprocessor macros, whcih could help. For instance, assuming the above declarations, you could

    #define afoo aa.foo
    #define bbar bb.bar
    

    and then you can code s.afoo instead of s.aa.foo

    You might also define macros like

    #define AFOO(X) (X).aa.foo
    

    and then code AFOO(s)

    Using such preprocessor macros does have some annoyance: with my example, you cannot declare anymore a variable (or formal argument, or field, or function) named afoo

    But I am not sure you should bother. My personal advice & habit is to give longer and often unique names to fields (and also to name struct a_st my struct-ures). Take advantage of the auto-completion abilities of your editor. Don’t forget that your code is more often read than written, so use meaningful names in it.

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

Sidebar

Related Questions

Given the following: struct Foo { int bar() const; }; struct IsEqual : public
Given the following: struct Foo { int bar() const; }; struct IsEqual : public
This very simple code gives me tons of errors: #include <iostream> #include <string> int
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
This is probably pretty simple. I want to select all elements of a given
This should be a simple one, but I'm a beginner with C#. Given a
This is a purely theoretical question. Given three simple classes: class Base { }
Given this XML, what XPath returns all elements whose prop attribute contains Foo (the
Given this class class Foo { // Want to find _bar with reflection [SomeAttribute]
I'm learning about deadlocks in Java, and there's this sample code from Sun's official

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.