4

I could not find an answer to this on the Internet, so here is my question: Can I define a struct instance without assigning it to a local or global variable in C? E.g.:

struct A {
  int b;
}

struct A foo() {
  return struct A { .b = 42 };
}

If this is not possible: why?

Bastian
  • 4,638
  • 6
  • 36
  • 55
  • 1
    http://ideone.com/Txc81n – BLUEPIXY Apr 25 '14 at 13:41
  • https://nickdesaulniers.github.io/blog/2013/07/25/designated-initialization-with-pointers-in-c/ – Engineer2021 Apr 25 '14 at 13:42
  • BTW, functions taking no arguments should have `(void)` as the parameters - otherwise a prototype isn't formed, and the compiler will let you call it with arguments (causing undefined behaviour) – M.M Apr 25 '14 at 13:54
  • @Matt MCNabb Thanks for the hint, I was definitely not aware of that! – Bastian Apr 25 '14 at 14:01
  • possible duplicate of [How to initialize a struct in ANSI C](http://stackoverflow.com/questions/330793/how-to-initialize-a-struct-in-ansi-c) – Deduplicator Apr 25 '14 at 14:04

3 Answers3

3

Yes C99 provides compound literals for this (see it live):

return (struct A) {  42 } ;

which is covered in the draft C99 standard section 6.5.2.5 Compound literals and says:

A postfix expression that consists of a parenthesized type name followed by a brace enclosed list of initializers is a compound literal. It provides an unnamed object whose value is given by the initializer list.84)

and:

The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound literal occurs outside the body of a function, the object has static storage duration; otherwise, it has automatic storage duration associated with the enclosing block.

and provides several examples including:

EXAMPLE 3 Initializers with designations can be combined with compound literals. Structure objects created using compound literals can be passed to functions without depending on member order:

drawline((struct point){.x=1, .y=1}, (struct point){.x=3, .y=4});

gcc also has a nice document on this in it's extension section since it supports this feature outside of C99 as well as clang.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
2

Yes, you can use compound literals in C99 and later.

return (struct A) { .b = 42 };

You can even point to them:

struct A *a = &(struct A) { .b = 42 };
a->b = 43;

These literals are "better" than string literals in that they are writable. The compiler may pool them if and only if you include const in the literal's type .

M.M
  • 138,810
  • 21
  • 208
  • 365
0

Yes, it is possible since C99. That is a compound literal.

Still, yours has the wrong syntax. Use:

(struct A){.b=42}

or

(struct A){42}

Though, go for constant literals if it does not matter:

(const struct A){.b=42}

All constant literals are subject to constant pooling (including string literals which have type char[] for historical reasons).
Constant compound literals and compound literals outside any function have static storage duration,
the rest have automatic storage duration (beware returning a pointer, also they must be initialised each time).

In summary, prefer constant literals where possible.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118