Consider below program in C.
2 4 6
The above example is an example of compound literals. Compound literals were introduced in C99 standard of C. Compound literals feature allows us to create unnamed objects with given list of initialized values. In the above example, an array is created without any name. Address of first element of array is assigned to pointer p.
What is the use of it?
Compound literals are mainly used with structures and are particularly useful when passing structures variables to functions. We can pass a structure object without defining it
For example, consider the below code.
This article is contributed by Shivam Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- How to avoid Structure Padding in C?
- Difference between Structure and Union in C
- Anonymous Union and Structure in C
- C Program to print numbers from 1 to N without using semicolon?
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- What are near, far and huge pointers?
- Enumeration (or enum) in C
- Struct Hack
- How to deallocate memory without using free() in C?
- Memory Layout of C Programs
- Operations on struct variables in C
- calloc() versus malloc()
- What is return type of getchar(), fgetc() and getc() ?
- Return values of printf() and scanf() in C/C++
- Write a C macro PRINT(x) which prints x