cfunctionstructc89ansi-c

How can the initialization of a struct be used as variable in a function call. ANSI-C Version


I am writing C89, C90, Ansi-C Code. One of my functions requires a struct as a parameter. I want to call the function with the initialization of that struct rather than creating a struct first then passing it to the function.

Here are some snippets which work.

typedef struct {
    char* EventName;
    char* Message; 
} Event;

Event myEvent = {
    .EventName = "infomessage", 
    .Message = "Testmessage"
};

Notify(myEvent);        

and here is what I would like to write but which doesn't work:

Notify({.EventName = "infomessage", .Message = "Testmessage"});

or even better

Notify({"infomessage", "Testmessage"});

EDIT: LabCVI is using the ISO 9899:1990 standard.


Solution

  • Use the compound literal (Event){"infomessage", "Testmessage"}, ie

    Notify((Event){"infomessage", "Testmessage"});