1

I can think I can vaguely remember seeing a few years ago a way to essentially enter a variable Structure and work on its members without calling the variable name every time. similar to how you can us "using namespace bla". It may have even been from pascal are some left field API that I have used. e.g

typedef struct time{
    uint8_t hour;
    uint8_t minute;
    uint8_t second;
    uint8_t period;

}time;

void clockFunct(){
    static time clock{0,0,0,am};

    /*SOME SYNTAX THAT I CANT FIND/REMEMBER*/ clock{

        hour = 2;
        minute = 30;
        second = 20;
        period = pm

    };
}

I could create some local variables and pass it back but i'm working on micro-controllers so space is a bit of a premium... or just not be lazy and write out the structure name. Just thought I would put it out there and see if anyone knew what I was talking about

Angus
  • 21
  • 4
  • I think you're referring to the Pascal `with` keyword here. Have a look at https://stackoverflow.com/questions/2279180/does-c-have-with-keyword-like-pascal – fvu Oct 10 '17 at 06:35
  • You should decide for one language. C doesn't have such a syntax. I'm pretty sure C++ doesn't, either. I've seen something like that in vb.net. –  Oct 10 '17 at 06:35
  • @fvu One of the most horrible features in Delphi or Basic. Obfuscates the code like hell. – user0042 Oct 10 '17 at 06:39
  • Regarding your code, in C you *can* assign the whole struct at once using a compound literal (looks like an initializer with a cast) –  Oct 10 '17 at 06:39
  • 1
    @user0042: that is only your opinion. This feature can easily be abused, but it allows clean and neat code when properly used. – Serge Ballesta Oct 10 '17 at 08:02
  • as of c++, what's wrong with using a reference-in-a-block ? { auto& _ = clock; _.hour = .... } – Massimiliano Janes Oct 10 '17 at 08:05
  • @user0042 ..and has the nasty habit of accessing an inherited field of a parent/container class that happens to have the same name instead of the one you intended. – Martin James Oct 10 '17 at 08:10

4 Answers4

3

Since you did tag C++, you can kind of do what you want in the near future (though the advisability of it is questionable). sees the introduction of structured bindings to the language:

static time clock{0,0,0,am};

{
    auto& [hour, minute, second, period] = clock;
    hour = 2;
    minute = 30;
    second = 20;
    period = pm;
};

Inside the block scope, those identifiers will refer (because of the reference qualifier) to the members of clock. And you don't need the block scope at all, actually. I just limited it to simulate the semantics you wanted more closely.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
2

There's no such syntax, you've probably seen it in other languages. Just write the correct member access operators, the code is normally more readable that way anyways.

That said, with your example code, you can assign the whole struct at once in C (not in C++) using a compound literal:

clock = (time){2, 30, 20, pm};

or, with designated initializers for more clarity (which also helps to avoid bugs):

clock = (time){ .hour=2, .second=30, .minute=20, .period=pm };
  • from c++11 on, you can do that in c++ as well ( for aggregates, without the 'cast' and with no type narrowing ... ) – Massimiliano Janes Oct 10 '17 at 07:29
  • @MassimilianoJanes [this question](https://stackoverflow.com/questions/9269825/how-to-assign-multiple-values-into-a-struct-at-once) suggests this isn't the case? –  Oct 10 '17 at 07:33
  • Some designated initializers will bring this closer to the OP's desired syntax. (And personally I think they will add clarity). – StoryTeller - Unslander Monica Oct 10 '17 at 07:35
  • @StoryTeller I left them out as OP seems to favor brevity in the code, but I agree ... adding it as an alternative. –  Oct 10 '17 at 07:36
  • @FelixPalmen, that question is either wrong, refers to prior c++11 or asks about a non aggregate type. I stress again that the syntax and requirements of the c++ version are somewhat different from the C one. – Massimiliano Janes Oct 10 '17 at 07:42
  • Thanks, I usually assign as above (although without the designated initializers, which do help with readability a bit which is nice) assigning values in a function was just the simplest thing I could think of doing to demonstrate what I was asking. Turns out I was thinking of Pascal. – Angus Oct 11 '17 at 08:15
2

In C99, you can specify a designator in struct initialization

static time clock = { .hour =2, .minute =30, .second = 20, .period =pm };
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

The language you're thinking of is C++, not C. And specifically, the behavior you're thinking of is seen in member functions.

We can change the function definition to void time::clockFunct() {. We'd first need to drop the typedef, though. That's actively problematic in C++ as it changes the meaning of time.

Note that it wasn't good C either - C has a function time() in the Standard Library.

MSalters
  • 173,980
  • 10
  • 155
  • 350