I recenly read this piece of code:

heap.push((Edge){x, 0});

where Edge is something like

struct Edge
  int a, b;

I wonder how standard this syntax is? do all compilers understand this and is it ok to write such code?

It's only properly available in C99

5.19 Compound Literals
ISO C99 supports compound literals. A compound literal looks like a cast containing an initializer. Its value is an object of the type specified in the cast, containing the elements specified in the initializer; it is an lvalue. As an extension, GCC supports compound literals in C89 mode and in C++.
Usually, the specified type is a structure. Assume that struct foo and structure are declared as shown: struct foo {int a; char b[2];} structure; Here is an example of constructing a struct foo with a compound literal: structure = ((struct foo) {x + y, 'a', 0}); This is equivalent to writing the following:

       struct foo temp = {x + y, 'a', 0};
       structure = temp;


I saw seen similar constructs several years ago in a VC++ 6.0 MFC program. The parameter to push() mearly creates a temporary instance of the Edge structure and passes that to push().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.